Completed
Push — master ( 960666...619cc6 )
by Tom
14s
created

testInvokeWithObjectManagerGiven()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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