Completed
Pull Request — develop (#693)
by Tom
11:30
created

NoObjectExistsFactoryTest::testCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Validator\Service;
6
7
use PHPUnit\Framework\TestCase;
8
use Laminas\ServiceManager\ServiceLocatorInterface;
9
use Laminas\ServiceManager\ServiceLocatorAwareInterface;
10
use DoctrineModule\Validator\NoObjectExists;
11
use Doctrine\Persistence\ObjectManager;
12
use Doctrine\Persistence\ObjectRepository;
13
use Interop\Container\ContainerInterface;
14
use Laminas\ServiceManager\ServiceLocatorAwareInterface;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Laminas\ServiceManager\ServiceLocatorAwareInterface as ServiceLocatorAwareInterface because the name is already in use
Loading history...
15
use Laminas\ServiceManager\ServiceLocatorInterface;
16
use PHPUnit\Framework\TestCase;
17
use function interface_exists;
18
19
/**
20
 * Generated by PHPUnit_SkeletonGenerator on 2017-09-04 at 11:12:27.
21
 *
22
 * @coversDefaultClass DoctrineModule\Validator\Service\NoObjectExistsFactory
23
 * @group validator
24
 */
25
class NoObjectExistsFactoryTest extends TestCase
26
{
27
    /** @var NoObjectExistsFactory */
28
    private $object;
29
30
    /**
31
     * Sets up the fixture, for example, opens a network connection.
32
     * This method is called before a test is executed.
33
     */
34
    protected function setUp() : void
35
    {
36
        $this->object = new NoObjectExistsFactory();
37
    }
38
39
    /**
40
     * @coversNothing
41
     */
42
    public function testCallable() : void
43
    {
44
        $this->assertIsCallable($this->object);
45
    }
46
47
    /**
48
     * @covers ::__invoke
49
     * @covers ::container
50
     * @covers ::getRepository
51
     * @covers ::getObjectManager
52
     * @covers ::getFields
53
     */
54
    public function testInvoke() : void
55
    {
56
        $options = [
57
            'target_class' => 'Foo\Bar',
58
            'fields'       => ['test'],
59
        ];
60
61
        $repository    = $this->prophesize(ObjectRepository::class);
62
        $objectManager = $this->prophesize(ObjectManager::class);
63
        $objectManager->getRepository('Foo\Bar')
64
            ->shouldBeCalled()
65
            ->willReturn($repository->reveal());
66
67
        $container = $this->prophesize(ContainerInterface::class);
68
        $container->get('doctrine.entitymanager.orm_default')
69
            ->shouldBeCalled()
70
            ->willReturn($objectManager->reveal());
71
72
        $instance = $this->object->__invoke(
73
            $container->reveal(),
74
            NoObjectExists::class,
75
            $options
76
        );
77
        $this->assertInstanceOf(NoObjectExists::class, $instance);
78
    }
79
80
    /**
81
     * @covers ::__invoke
82
     * @covers ::container
83
     * @covers ::getRepository
84
     * @covers ::getObjectManager
85
     * @covers ::getFields
86
     */
87
    public function testInvokeWithObjectManagerGiven() : void
88
    {
89
        $repository    = $this->prophesize(ObjectRepository::class);
90
        $objectManager = $this->prophesize(ObjectManager::class);
91
        $objectManager->getRepository('Foo\Bar')
92
            ->shouldBeCalled()
93
            ->willReturn($repository->reveal());
94
95
        $options = [
96
            'target_class'   => 'Foo\Bar',
97
            'object_manager' => $objectManager->reveal(),
98
            'fields'         => ['test'],
99
        ];
100
101
        $container = $this->prophesize(ContainerInterface::class);
102
        $container->get('doctrine.entitymanager.orm_default')
103
            ->shouldNotBeCalled();
104
105
        $instance = $this->object->__invoke(
106
            $container->reveal(),
107
            NoObjectExists::class,
108
            $options
109
        );
110
        $this->assertInstanceOf(NoObjectExists::class, $instance);
111
    }
112
113
    /**
114
     * @covers ::merge
115
     */
116
    public function testInvokeWithMerge() : void
117
    {
118
        $options = [
119
            'target_class' => 'Foo\Bar',
120
            'fields'       => ['test'],
121
            'messages'     => [NoObjectExists::ERROR_OBJECT_FOUND => 'test'],
122
        ];
123
124
        $repository    = $this->prophesize(ObjectRepository::class);
125
        $objectManager = $this->prophesize(ObjectManager::class);
126
        $objectManager->getRepository('Foo\Bar')
127
            ->shouldBeCalled()
128
            ->willReturn($repository->reveal());
129
130
        $container = $this->prophesize(ContainerInterface::class);
131
        $container->get('doctrine.entitymanager.orm_default')
132
            ->shouldBeCalled()
133
            ->willReturn($objectManager->reveal());
134
135
        $instance  = $this->object->__invoke(
136
            $container->reveal(),
137
            NoObjectExists::class,
138
            $options
139
        );
140
        $templates = $instance->getMessageTemplates();
141
        $this->assertArrayHasKey(NoObjectExists::ERROR_OBJECT_FOUND, $templates);
142
        $this->assertSame('test', $templates[NoObjectExists::ERROR_OBJECT_FOUND]);
143
    }
144
145
    /**
146
     * @covers ::getRepository
147
     */
148
    public function testInvokeWithoutTargetClass() : void
149
    {
150
        $this->expectException(ServiceCreationException::class);
151
152
        $container = $this->prophesize(ContainerInterface::class);
153
        $this->object->__invoke(
154
            $container->reveal(),
155
            NoObjectExists::class,
156
            []
157
        );
158
    }
159
160
    /**
161
     * @covers ::createService
162
     * @covers ::setCreationOptions
163
     */
164
    public function testCreateService() : void
165
    {
166
        $options = [
167
            'target_class' => 'Foo\Bar',
168
            'fields'       => ['test'],
169
        ];
170
171
        $repository    = $this->prophesize(ObjectRepository::class);
172
        $objectManager = $this->prophesize(ObjectManager::class);
173
        $objectManager->getRepository('Foo\Bar')
174
            ->shouldBeCalled()
175
            ->willReturn($repository->reveal());
176
177
        $container = $this->prophesize(ServiceLocatorInterface::class);
178
        $container->get('doctrine.entitymanager.orm_default')
179
            ->shouldBeCalled()
180
            ->willReturn($objectManager->reveal());
181
182
        $this->object->setCreationOptions($options);
183
        $instance = $this->object->createService($container->reveal());
184
        $this->assertInstanceOf(NoObjectExists::class, $instance);
185
    }
186
187
    /**
188
     * @covers ::container
189
     */
190
    public function testCreateServiceWithServiceLocatorAwareInterface() : void
191
    {
192
        if (! interface_exists(ServiceLocatorAwareInterface::class)) {
193
            $this->markTestSkipped('ServiceLocatorAwareInterface not defined');
194
        }
195
196
        $options = [
197
            'target_class' => 'Foo\Bar',
198
            'fields'       => ['test'],
199
        ];
200
201
        $repository    = $this->prophesize(ObjectRepository::class);
202
        $objectManager = $this->prophesize(ObjectManager::class);
203
        $objectManager->getRepository('Foo\Bar')
204
            ->shouldBeCalled()
205
            ->willReturn($repository->reveal());
206
207
        $container = $this->prophesize(ServiceLocatorInterface::class);
208
        $container->get('doctrine.entitymanager.orm_default')
209
            ->shouldBeCalled()
210
            ->willReturn($objectManager->reveal());
211
212
        $parentContainer = $this->prophesize(ServiceLocatorInterface::class);
213
        $parentContainer->willImplement(ServiceLocatorAwareInterface::class);
214
        $parentContainer->getServiceLocator()
215
            ->shouldBeCalled()
216
            ->willReturn($container->reveal());
217
218
        $this->object->setCreationOptions($options);
219
        $instance = $this->object->createService($parentContainer->reveal());
220
        $this->assertInstanceOf(NoObjectExists::class, $instance);
221
    }
222
}
223