Completed
Pull Request — master (#15)
by Pavel
11:23
created

Tests/Unit/Controller/SearchControllerTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ReferenceProviderInterface;
14
use ScayTrase\Api\Cruds\Tests\Fixtures\AbcClass;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
class SearchControllerTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testSearching()
20
    {
21
        $collection = new ArrayCollection();
22
23
        $f1 = $this->createFixture(1, 'b', null);
24
        $f2 = $this->createFixture(2, '5', '10');
25
        $f3 = $this->createFixture(3, 5, null);
26
        $f4 = $this->createFixture(4, null, 10);
27
        $collection->add($f1);
28
        $collection->add($f2);
29
        $collection->add($f3);
30
        $collection->add($f4);
31
32
        $configuration = new EntityCriteriaConfigurator(
33
            new PublicPropertyMapper(),
34
            $this->getReferenceProvider()
0 ignored issues
show
It seems like $this->getReferenceProvider() targeting ScayTrase\Api\Cruds\Test...:getReferenceProvider() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, ScayTrase\Api\Cruds\Crit...igurator::__construct() does only seem to accept object<ScayTrase\Api\Cru...renceProviderInterface>, maybe add an additional type check?

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.

Loading history...
35
        );
36
37
        $evmProphecy = $this->prophesize(EventDispatcherInterface::class);
38
        $evmProphecy->dispatch(CrudEvents::READ, Argument::type(CollectionCrudEvent::class))->shouldBeCalled();
39
        /** @var EventDispatcherInterface $evm */
40
        $evm = $evmProphecy->reveal();
41
42
        $controller = new SearchController(AbcClass::class, $collection, $configuration, $evm);
43
44
        $result = $controller->findAction(['b' => 5], ['a' => Criteria::DESC], 1);
45
46
        self::assertCount(1, $result);
47
        self::assertNotContains($f1, $result);
48
        self::assertNotContains($f2, $result);
49
        self::assertContains($f3, $result);
50
        self::assertNotContains($f4, $result);
51
52
        $result = $controller->findAction(['b' => '5'], ['a' => Criteria::DESC], 1);
53
54
        self::assertCount(1, $result);
55
        self::assertNotContains($f1, $result);
56
        self::assertContains($f2, $result);
57
        self::assertNotContains($f3, $result);
58
        self::assertNotContains($f4, $result);
59
60
        $result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 1);
61
62
        self::assertCount(1, $result);
63
        self::assertContains($f1, $result);
64
        self::assertNotContains($f2, $result);
65
        self::assertNotContains($f3, $result);
66
        self::assertNotContains($f4, $result);
67
68
        $result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 2);
69
70
        self::assertCount(2, $result);
71
        self::assertContains($f1, $result);
72
        self::assertNotContains($f2, $result);
73
        self::assertContains($f3, $result);
74
        self::assertNotContains($f4, $result);
75
76
        $result = $controller->findAction(['c' => null], ['a' => Criteria::ASC], 3);
77
78
        self::assertCount(2, $result);
79
80
        $result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC], 2);
81
82
        self::assertCount(2, $result);
83
        self::assertContains($f1, $result);
84
        self::assertContains($f2, $result);
85
        self::assertNotContains($f3, $result);
86
        self::assertNotContains($f4, $result);
87
88
        $result = $controller->findAction(['a' => [1, 2, 3, 4]], ['a' => Criteria::ASC]);
89
90
        self::assertCount(4, $result);
91
        self::assertContains($f1, $result);
92
        self::assertContains($f2, $result);
93
        self::assertContains($f3, $result);
94
        self::assertContains($f4, $result);
95
        self::assertSame([$f1, $f2, $f3, $f4], $result->toArray());
96
    }
97
98 View Code Duplication
    private function createFixture($a, $b, $c)
99
    {
100
        $entity    = new AbcClass();
101
        $entity->a = $a;
102
        $entity->b = $b;
103
        $entity->c = $c;
104
105
        return $entity;
106
    }
107
108
    /**
109
     * @return ReferenceProviderInterface|\PHPUnit_Framework_MockObject_MockObject
110
     */
111
    private function getReferenceProvider()
112
    {
113
        $mock = $this->getMockBuilder(ReferenceProviderInterface::class)->getMock();
114
115
        $mock->method('getEntityReference')->willReturnArgument(2);
116
117
        return $mock;
118
    }
119
}
120
121