1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Tests\Unit\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use ScayTrase\Api\Cruds\Controller\SearchController; |
10
|
|
|
use ScayTrase\Api\Cruds\Criteria\EntityCriteriaConfigurator; |
11
|
|
|
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent; |
12
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
13
|
|
|
use ScayTrase\Api\Cruds\PublicPropertyMapper; |
14
|
|
|
use ScayTrase\Api\Cruds\ReferenceProviderInterface; |
15
|
|
|
use ScayTrase\Api\Cruds\Tests\Fixtures\AbcClass; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
|
18
|
|
|
final class SearchControllerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testSearching() |
21
|
|
|
{ |
22
|
|
|
$collection = new ArrayCollection(); |
23
|
|
|
|
24
|
|
|
$f1 = $this->createFixture(1, 'b', null); |
25
|
|
|
$f2 = $this->createFixture(2, '5', '10'); |
26
|
|
|
$f3 = $this->createFixture(3, 5, null); |
27
|
|
|
$f4 = $this->createFixture(4, null, 10); |
28
|
|
|
$collection->add($f1); |
29
|
|
|
$collection->add($f2); |
30
|
|
|
$collection->add($f3); |
31
|
|
|
$collection->add($f4); |
32
|
|
|
|
33
|
|
|
$configuration = new EntityCriteriaConfigurator( |
34
|
|
|
new PublicPropertyMapper(), |
35
|
|
|
$this->getReferenceProvider() |
|
|
|
|
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$evmProphecy = $this->prophesize(EventDispatcherInterface::class); |
39
|
|
|
$evmProphecy->dispatch(CrudEvents::READ, Argument::type(CollectionCrudEvent::class))->shouldBeCalled(); |
40
|
|
|
/** @var EventDispatcherInterface $evm */ |
41
|
|
|
$evm = $evmProphecy->reveal(); |
42
|
|
|
|
43
|
|
|
$controller = new SearchController(AbcClass::class, $collection, $configuration, $evm); |
44
|
|
|
|
45
|
|
|
$result = $controller->findAction(['b' => 5], ['a' => Criteria::DESC], 1); |
46
|
|
|
|
47
|
|
|
self::assertCount(1, $result); |
48
|
|
|
self::assertNotContains($f1, $result); |
49
|
|
|
self::assertNotContains($f2, $result); |
50
|
|
|
self::assertContains($f3, $result); |
51
|
|
|
self::assertNotContains($f4, $result); |
52
|
|
|
|
53
|
|
|
$result = $controller->findAction(['b' => '5'], ['a' => Criteria::DESC], 1); |
54
|
|
|
|
55
|
|
|
self::assertCount(1, $result); |
56
|
|
|
self::assertNotContains($f1, $result); |
57
|
|
|
self::assertContains($f2, $result); |
58
|
|
|
self::assertNotContains($f3, $result); |
59
|
|
|
self::assertNotContains($f4, $result); |
60
|
|
|
|
61
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 1); |
62
|
|
|
|
63
|
|
|
self::assertCount(1, $result); |
64
|
|
|
self::assertContains($f1, $result); |
65
|
|
|
self::assertNotContains($f2, $result); |
66
|
|
|
self::assertNotContains($f3, $result); |
67
|
|
|
self::assertNotContains($f4, $result); |
68
|
|
|
|
69
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 2); |
70
|
|
|
|
71
|
|
|
self::assertCount(2, $result); |
72
|
|
|
self::assertContains($f1, $result); |
73
|
|
|
self::assertNotContains($f2, $result); |
74
|
|
|
self::assertContains($f3, $result); |
75
|
|
|
self::assertNotContains($f4, $result); |
76
|
|
|
|
77
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 3); |
78
|
|
|
|
79
|
|
|
self::assertCount(2, $result); |
80
|
|
|
|
81
|
|
|
$result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC], 2); |
82
|
|
|
|
83
|
|
|
self::assertCount(2, $result); |
84
|
|
|
self::assertContains($f1, $result); |
85
|
|
|
self::assertContains($f2, $result); |
86
|
|
|
self::assertNotContains($f3, $result); |
87
|
|
|
self::assertNotContains($f4, $result); |
88
|
|
|
|
89
|
|
|
$result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC]); |
90
|
|
|
|
91
|
|
|
self::assertCount(4, $result); |
92
|
|
|
self::assertContains($f1, $result); |
93
|
|
|
self::assertContains($f2, $result); |
94
|
|
|
self::assertContains($f3, $result); |
95
|
|
|
self::assertContains($f4, $result); |
96
|
|
|
self::assertSame([$f1, $f2, $f3, $f4], $result->toArray()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
private function createFixture($a, $b, $c) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$entity = new AbcClass(); |
102
|
|
|
$entity->a = $a; |
103
|
|
|
$entity->b = $b; |
104
|
|
|
$entity->c = $c; |
105
|
|
|
|
106
|
|
|
return $entity; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return ReferenceProviderInterface|\PHPUnit_Framework_MockObject_MockObject |
111
|
|
|
*/ |
112
|
|
|
private function getReferenceProvider() |
113
|
|
|
{ |
114
|
|
|
$mock = $this->getMockBuilder(ReferenceProviderInterface::class)->getMock(); |
115
|
|
|
|
116
|
|
|
$mock->method('getEntityReference')->willReturnArgument(2); |
117
|
|
|
|
118
|
|
|
return $mock; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.