-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactoryTest.php
More file actions
76 lines (56 loc) · 2.11 KB
/
FactoryTest.php
File metadata and controls
76 lines (56 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Chubbyphp\Tests\Container\Unit;
use Chubbyphp\Container\Factory;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @internal
*/
#[CoversClass(Factory::class)]
final class FactoryTest extends TestCase
{
public function testInvoke(): void
{
$existingFactory = static function (ContainerInterface $container): \stdClass {
$object = new \stdClass();
$object->key1 = $container->get('key1');
return $object;
};
$factory = static function (ContainerInterface $container, callable $previous): \stdClass {
$object = $previous($container);
$object->key2 = $container->get('key2');
return $object;
};
$builder = new MockObjectBuilder();
/** @var ContainerInterface $container */
$container = $builder->create(ContainerInterface::class, [
new WithReturn('get', ['key1'], 'value1'),
new WithReturn('get', ['key2'], 'value2'),
]);
$extendedFactory = new Factory($existingFactory, $factory);
self::assertIsCallable($extendedFactory);
$service = $extendedFactory($container);
self::assertInstanceOf(\stdClass::class, $service);
self::assertSame('value1', $service->key1);
self::assertSame('value2', $service->key2);
}
public function testIsLazy(): void
{
$existingFactory = static function (ContainerInterface $container): \stdClass {
$object = new \stdClass();
$object->key1 = $container->get('key1');
return $object;
};
$factory = static function (ContainerInterface $container, callable $previous): \stdClass {
$object = $previous($container);
$object->key2 = $container->get('key2');
return $object;
};
$extendedFactory = new Factory($existingFactory, $factory);
self::assertIsCallable($extendedFactory);
}
}