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 Prophecy\Argument; |
8
|
|
|
use ScayTrase\Api\Cruds\Controller\SearchController; |
9
|
|
|
use ScayTrase\Api\Cruds\Criteria\EntityCriteriaConfigurator; |
10
|
|
|
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent; |
11
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
12
|
|
|
use ScayTrase\Api\Cruds\PublicPropertyMapper; |
13
|
|
|
use ScayTrase\Api\Cruds\Tests\Fixtures\AbcClass; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
class SearchControllerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testSearching() |
19
|
|
|
{ |
20
|
|
|
$collection = new ArrayCollection(); |
21
|
|
|
|
22
|
|
|
$f1 = $this->createFixture(1, 'b', null); |
23
|
|
|
$f2 = $this->createFixture(2, '5', '10'); |
24
|
|
|
$f3 = $this->createFixture(3, 5, null); |
25
|
|
|
$f4 = $this->createFixture(4, null, 10); |
26
|
|
|
$collection->add($f1); |
27
|
|
|
$collection->add($f2); |
28
|
|
|
$collection->add($f3); |
29
|
|
|
$collection->add($f4); |
30
|
|
|
|
31
|
|
|
$configuration = new EntityCriteriaConfigurator(new PublicPropertyMapper()); |
32
|
|
|
|
33
|
|
|
$evmProphecy = $this->prophesize(EventDispatcherInterface::class); |
34
|
|
|
$evmProphecy->dispatch(CrudEvents::READ, Argument::type(CollectionCrudEvent::class))->shouldBeCalled(); |
35
|
|
|
/** @var EventDispatcherInterface $evm */ |
36
|
|
|
$evm = $evmProphecy->reveal(); |
37
|
|
|
|
38
|
|
|
$controller = new SearchController(AbcClass::class, $collection, $configuration, $evm); |
39
|
|
|
|
40
|
|
|
$result = $controller->findAction(['b' => 5], ['a' => Criteria::DESC], 1); |
41
|
|
|
|
42
|
|
|
self::assertCount(1, $result); |
43
|
|
|
self::assertNotContains($f1, $result); |
44
|
|
|
self::assertNotContains($f2, $result); |
45
|
|
|
self::assertContains($f3, $result); |
46
|
|
|
self::assertNotContains($f4, $result); |
47
|
|
|
|
48
|
|
|
$result = $controller->findAction(['b' => '5'], ['a' => Criteria::DESC], 1); |
49
|
|
|
|
50
|
|
|
self::assertCount(1, $result); |
51
|
|
|
self::assertNotContains($f1, $result); |
52
|
|
|
self::assertContains($f2, $result); |
53
|
|
|
self::assertNotContains($f3, $result); |
54
|
|
|
self::assertNotContains($f4, $result); |
55
|
|
|
|
56
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 1); |
57
|
|
|
|
58
|
|
|
self::assertCount(1, $result); |
59
|
|
|
self::assertContains($f1, $result); |
60
|
|
|
self::assertNotContains($f2, $result); |
61
|
|
|
self::assertNotContains($f3, $result); |
62
|
|
|
self::assertNotContains($f4, $result); |
63
|
|
|
|
64
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 2); |
65
|
|
|
|
66
|
|
|
self::assertCount(2, $result); |
67
|
|
|
self::assertContains($f1, $result); |
68
|
|
|
self::assertNotContains($f2, $result); |
69
|
|
|
self::assertContains($f3, $result); |
70
|
|
|
self::assertNotContains($f4, $result); |
71
|
|
|
|
72
|
|
|
$result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 3); |
73
|
|
|
|
74
|
|
|
self::assertCount(2, $result); |
75
|
|
|
|
76
|
|
|
$result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC], 2); |
77
|
|
|
|
78
|
|
|
self::assertCount(2, $result); |
79
|
|
|
self::assertContains($f1, $result); |
80
|
|
|
self::assertContains($f2, $result); |
81
|
|
|
self::assertNotContains($f3, $result); |
82
|
|
|
self::assertNotContains($f4, $result); |
83
|
|
|
|
84
|
|
|
$result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC]); |
85
|
|
|
|
86
|
|
|
self::assertCount(4, $result); |
87
|
|
|
self::assertContains($f1, $result); |
88
|
|
|
self::assertContains($f2, $result); |
89
|
|
|
self::assertContains($f3, $result); |
90
|
|
|
self::assertContains($f4, $result); |
91
|
|
|
self::assertSame([$f1, $f2, $f3, $f4], $result->toArray()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
private function createFixture($a, $b, $c) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$entity = new AbcClass(); |
97
|
|
|
$entity->a = $a; |
98
|
|
|
$entity->b = $b; |
99
|
|
|
$entity->c = $c; |
100
|
|
|
|
101
|
|
|
return $entity; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.