Completed
Pull Request — master (#1)
by
unknown
13:02
created

testFindAllWithSearch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace Sulu\Bundle\ElasticsearchActivityLogBundle\Tests\Storage;
4
5
use Prophecy\Argument;
6
use Sulu\Bundle\ElasticsearchActivityLogBundle\Storage\ElasticsearchActivityStorage;
7
use ONGR\ElasticsearchBundle\Service\Manager;
8
use Sulu\Component\ActivityLog\Repository\UserRepositoryInterface;
9
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
class ElasticsearchActivityStorageTest extends KernelTestCase
14
{
15
    /**
16
     * @var ContainerInterface
17
     */
18
    private $container;
19
20
    /**
21
     * @var ElasticsearchActivityStorage
22
     */
23
    private $storage;
24
25
    /**
26
     * @var UserInterface
27
     */
28
    private $user;
29
30
    public function setUp()
31
    {
32
        /** @var Manager $manager */
33
        $manager = $this->getContainer()->get('es.manager.live');
34
        $manager->dropAndCreateIndex();
35
36
        $userRepository = $this->prophesize(UserRepositoryInterface::class);
37
        $this->user = $this->prophesize(TestUserInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sulu\...stUserInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Symfony\Component...ore\User\UserInterface> of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
39
        $userRepository->findOneById(Argument::any())->willReturn($this->user->reveal());
40
41
        $this->storage = new ElasticsearchActivityStorage(
42
            $manager,
43
            $userRepository->reveal()
44
        );
45
    }
46
47
    public function testPersist()
48
    {
49
        $activity = $this->storage->create('test')->setData(['test' => 'test'])->setCreator($this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
51
        $this->storage->persist($activity);
52
        $this->storage->flush();
53
54
        $result = $this->storage->find($activity->getUuid());
55
        $this->assertEquals($activity->getUuid(), $result->getUuid());
56
        $this->assertEquals($activity->getData(), $result->getData());
57
        $this->assertEquals($activity->getCreator(), $this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
59
        return $activity;
60
    }
61
62
    public function testPersistChildren()
63
    {
64
        $parentActivity = $this->storage->create('test');
65
        $childActivity = $this->storage->create('test')->setParent($parentActivity)->setCreator($this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
67
        $this->storage->persist($parentActivity);
68
        $this->storage->persist($childActivity);
69
        $this->storage->flush();
70
71
        $result = $this->storage->findByParent($parentActivity, 1, 10);
72
73
        $this->assertCount(1, $result);
74
        $this->assertEquals($childActivity->getUuid(), $result[0]->getUuid());
75
    }
76
77
    public function testFind()
78
    {
79
        $parentActivity = $this->storage->create('test');
80
        $childActivity = $this->storage->create('test')->setParent($parentActivity)->setCreator($this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
82
        $this->storage->persist($parentActivity);
83
        $this->storage->persist($childActivity);
84
        $this->storage->flush();
85
86
        $result = $this->storage->find($childActivity->getUuid());
87
88
        $this->assertEquals($childActivity->getUuid(), $result->getUuid());
89
        $this->assertEquals($parentActivity->getUuid(), $result->getParent()->getUuid());
90
        $this->assertEquals($childActivity->getCreator(), $this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
    }
92
93
    public function testFindAll()
94
    {
95
        $parentActivity = $this->storage->create('test')->setCreator($this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
        $childActivity = $this->storage->create('test')->setParent($parentActivity)->setCreator($this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
98
        $this->storage->persist($parentActivity);
99
        $this->storage->persist($childActivity);
100
        $this->storage->flush();
101
102
        $result = $this->storage->findAll(1, 10);
103
104
        $this->assertCount(2, $result);
105
        $this->assertEquals($parentActivity->getUuid(), $result[0]->getUuid());
106
        $this->assertEquals($childActivity->getUuid(), $result[1]->getUuid());
107
        $this->assertEquals($parentActivity->getUuid(), $result[1]->getParent()->getUuid());
108
        $this->assertEquals($childActivity->getCreator(), $this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        $this->assertEquals($parentActivity->getCreator(), $this->user->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
    }
111
112
    public function testFindAllWithSearch()
113
    {
114
        $activity = $this->storage->create('test')->setMessage('Message which should be found');
115
        $activity2 = $this->storage->create('test')->setMessage('Secret message');
116
        $activity3 = $this->storage->create('test')->setMessage('Another message which should be found');
117
        $activity4 = $this->storage->create('test')->setMessage('Another secret message');
118
        $activity5 = $this->storage->create('test')->setMessage('Third message which should be found');
119
        $activity6 = $this->storage->create('test')->setMessage('Third secret message');
120
121
        $this->storage->persist($activity);
122
        $this->storage->persist($activity2);
123
        $this->storage->persist($activity3);
124
        $this->storage->persist($activity4);
125
        $this->storage->persist($activity5);
126
        $this->storage->persist($activity6);
127
        $this->storage->flush();
128
129
        $result = $this->storage->findAllWithSearch();
130
        $resultWithSearch = $this->storage->findAllWithSearch('found', ['message']);
131
        $sortedResultWithSearch = $this->storage->findAllWithSearch('found', ['message'], 1, 2, 'message', 'desc');
132
133
        $this->assertCount(6, $result);
134
135
        $this->assertCount(3, $resultWithSearch);
136
        $this->assertEquals($activity->getUuid(), $resultWithSearch[0]->getUuid());
137
        $this->assertEquals($activity3->getUuid(), $resultWithSearch[1]->getUuid());
138
        $this->assertEquals($activity5->getUuid(), $resultWithSearch[2]->getUuid());
139
140
        $this->assertCount(2, $sortedResultWithSearch);
141
        $this->assertEquals($activity->getUuid(), $sortedResultWithSearch[1]->getUuid());
142
        $this->assertEquals($activity5->getUuid(), $sortedResultWithSearch[0]->getUuid());
143
    }
144
145
    public function testFindAllWithSearchAndSorting()
146
    {
147
        $activity = $this->storage->create('test')->setMessage('Message which should be found');
148
        $activity2 = $this->storage->create('test')->setMessage('Secret message');
149
        $activity3 = $this->storage->create('test')->setMessage('Another message which should be found');
150
        $activity4 = $this->storage->create('test')->setMessage('Another secret message');
151
152
        $this->storage->persist($activity);
153
        $this->storage->persist($activity2);
154
        $this->storage->persist($activity3);
155
        $this->storage->persist($activity4);
156
        $this->storage->flush();
157
158
        $result = $this->storage->findAllWithSearch('found', ['message']);
159
160
        $this->assertCount(2, $result);
161
        $this->assertEquals($activity->getUuid(), $result[0]->getUuid());
162
        $this->assertEquals($activity3->getUuid(), $result[1]->getUuid());
163
    }
164
165
    /**
166
     * Return the test container.
167
     *
168
     * This container will use the default configuration of the kernel.
169
     *
170
     * If you require the container for a different kernel environment
171
     * you should create a new Kernel with the `getKernel` method and
172
     * retrieve the Container from that.
173
     *
174
     * @return ContainerInterface
175
     */
176
    public function getContainer()
177
    {
178
        if ($this->container) {
179
            return $this->container;
180
        }
181
182
        self::bootKernel();
183
184
        $this->container = self::$kernel->getContainer();
185
186
        return $this->container;
187
    }
188
}
189
190
interface TestUserInterface extends UserInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
191
{
192
    public function getId();
193
}
194