1 | <?php |
||
22 | final class EntityArgumentFactoryTest extends TestCase |
||
23 | { |
||
24 | |||
25 | private EntityArgumentFactory $factory; |
||
|
|||
26 | |||
27 | private ObjectManager $objectManager; |
||
28 | |||
29 | private ArgumentFactory $argumentFactory; |
||
30 | |||
31 | public function setUp() |
||
32 | { |
||
33 | $this->objectManager = $this->createMock(ObjectManager::class); |
||
34 | $this->argumentFactory = $this->createMock(ArgumentFactory::class); |
||
35 | |||
36 | $this->factory = new EntityArgumentFactory($this->objectManager, $this->argumentFactory); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @test |
||
41 | * @dataProvider dataProviderForShouldKnowIfUnderstandString |
||
42 | */ |
||
43 | public function shouldKnowIfUnderstandString(bool $expectedResult, string $source) |
||
44 | { |
||
45 | /** @var bool $actualResult */ |
||
46 | $actualResult = $this->factory->understandsString($source); |
||
47 | |||
48 | $this->assertEquals($expectedResult, $actualResult); |
||
49 | } |
||
50 | |||
51 | public function dataProviderForShouldKnowIfUnderstandString() |
||
52 | { |
||
53 | return array( |
||
54 | [true, 'Foo#bar'], |
||
55 | [false, 'Foo#'], |
||
56 | [false, '#bar'], |
||
57 | [false, '#'], |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @test |
||
63 | * @dataProvider dataProviderForShouldKnowIfUnderstandArray |
||
64 | */ |
||
65 | public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source) |
||
66 | { |
||
67 | /** @var bool $actualResult */ |
||
68 | $actualResult = $this->factory->understandsArray($source); |
||
69 | |||
70 | $this->assertEquals($expectedResult, $actualResult); |
||
71 | } |
||
72 | |||
73 | public function dataProviderForShouldKnowIfUnderstandArray() |
||
74 | { |
||
75 | return array( |
||
76 | [true, ['entity-class' => 'foo', 'entity-id' => 'bar']], |
||
77 | [false, ['entity-id' => 'bar']], |
||
78 | [false, ['entity-class' => 'foo']], |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @test |
||
84 | * @dataProvider dataProviderForShouldCreateCallArgumentFromString |
||
85 | */ |
||
86 | public function shouldCreateCallArgumentFromString( |
||
87 | ?EntityArgument $expectedResult, |
||
88 | string $source, |
||
89 | bool $shouldRejectCreation |
||
90 | ) { |
||
91 | if ($shouldRejectCreation) { |
||
92 | $this->expectException(InvalidArgumentException::class); |
||
93 | |||
94 | } else { |
||
95 | $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class)); |
||
96 | } |
||
97 | |||
98 | $actualResult = $this->factory->createArgumentFromString($source); |
||
99 | |||
100 | $this->assertEquals($expectedResult, $actualResult); |
||
101 | } |
||
102 | |||
103 | public function dataProviderForShouldCreateCallArgumentFromString(): array |
||
104 | { |
||
105 | $this->setUp(); |
||
106 | |||
107 | return array( |
||
108 | [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), 'Foo#bar', false], |
||
109 | [null, 'a#', true], |
||
110 | [null, '#b', true], |
||
111 | [null, '#', true], |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @test |
||
117 | * @dataProvider dataProviderForShouldCreateCallArgumentFromArray |
||
118 | */ |
||
119 | public function shouldCreateCallArgumentFromArray( |
||
120 | ?EntityArgument $expectedResult, |
||
121 | array $source, |
||
122 | bool $shouldRejectCreation |
||
123 | ) { |
||
124 | if ($shouldRejectCreation) { |
||
125 | $this->expectException(InvalidArgumentException::class); |
||
126 | |||
127 | } else { |
||
128 | $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class)); |
||
129 | $this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class)); |
||
130 | } |
||
131 | |||
132 | $actualResult = $this->factory->createArgumentFromArray($source); |
||
133 | |||
134 | $this->assertEquals($expectedResult, $actualResult); |
||
135 | } |
||
136 | |||
137 | public function dataProviderForShouldCreateCallArgumentFromArray(): array |
||
138 | { |
||
139 | $this->setUp(); |
||
140 | |||
141 | return array( |
||
142 | [null, [], true], |
||
143 | [null, ['entity-id' => 'foo'], true], |
||
144 | [null, ['entity-class' => 'bar'], true], |
||
145 | [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), [ |
||
146 | 'entity-class' => 'Foo', |
||
147 | 'entity-id' => 'bar' |
||
148 | ], false], |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | } |
||
153 |