Completed
Push — master ( e26eba...178ca9 )
by Eric
37:26 queued 30:42
created

createRegistryMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\TranslationBundle\Tests\EventSubscriber;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ORM\Event\LifecycleEventArgs;
16
use Doctrine\ORM\Events;
17
use Lug\Bundle\TranslationBundle\EventSubscriber\TranslatableResourceSubscriber;
18
use Lug\Component\Registry\Model\RegistryInterface;
19
use Lug\Component\Resource\Factory\FactoryInterface;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
use Lug\Component\Translation\Context\LocaleContextInterface;
22
use Lug\Component\Translation\Model\TranslatableInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class TranslatableResourceSubscriberTest extends \PHPUnit_Framework_TestCase
29
{
30
    /**
31
     * @var TranslatableResourceSubscriber
32
     */
33
    private $translatableResourceSubscriber;
34
35
    /**
36
     * @var ContainerInterface
37
     */
38
    private $container;
39
40
    /**
41
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
42
     */
43
    private $resourceRegistry;
44
45
    /**
46
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
47
     */
48
    private $localeContext;
49
50
    /**
51
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
52
     */
53
    private $factoryRegistry;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function setUp()
59
    {
60
        $this->container = $this->createContainerMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createContainerMock() of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
61
        $this->resourceRegistry = $this->createRegistryMock();
62
        $this->localeContext = $this->createLocaleContextMock();
63
        $this->factoryRegistry = $this->createRegistryMock();
64
65
        $this->container
66
            ->expects($this->any())
67
            ->method('get')
68
            ->will($this->returnValueMap([
69
                [
70
                    'lug.resource.registry',
71
                    ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
72
                    $this->resourceRegistry,
73
                ],
74
                [
75
                    'lug.translation.context.locale',
76
                    ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
77
                    $this->localeContext,
78
                ],
79
                [
80
                    'lug.resource.registry.factory',
81
                    ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
82
                    $this->factoryRegistry,
83
                ],
84
            ]));
85
86
        $this->translatableResourceSubscriber = new TranslatableResourceSubscriber($this->container);
87
    }
88
89
    public function testInheritance()
90
    {
91
        $this->assertInstanceOf(EventSubscriber::class, $this->translatableResourceSubscriber);
92
    }
93
94
    public function testSubscribedEvents()
95
    {
96
        $this->assertSame([Events::postLoad], $this->translatableResourceSubscriber->getSubscribedEvents());
97
    }
98
99
    public function testPostLoadWithTranslatable()
100
    {
101
        $event = $this->createLifecycleEventArgsMock();
102
        $event
103
            ->expects($this->once())
104
            ->method('getEntity')
105
            ->will($this->returnValue($translatable = $this->createTranslatableMock()));
106
107
        $this->localeContext
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Translatio...\LocaleContextInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
            ->expects($this->once())
109
            ->method('getLocales')
110
            ->will($this->returnValue($locales = ['fr']));
111
112
        $translatable
113
            ->expects($this->once())
114
            ->method('setLocales')
115
            ->with($this->identicalTo($locales));
116
117
        $this->localeContext
118
            ->expects($this->once())
119
            ->method('getFallbackLocale')
120
            ->will($this->returnValue($fallbackLocale = 'en'));
121
122
        $translatable
123
            ->expects($this->once())
124
            ->method('setFallbackLocale')
125
            ->with($this->identicalTo($fallbackLocale));
126
127
        $this->resourceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
            ->expects($this->once())
129
            ->method('getIterator')
130
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
131
132
        $resource
133
            ->expects($this->once())
134
            ->method('getModel')
135
            ->will($this->returnValue(get_class($translatable)));
136
137
        $resource
138
            ->expects($this->once())
139
            ->method('getRelation')
140
            ->with($this->identicalTo('translation'))
141
            ->will($this->returnValue($translation = $this->createResourceMock()));
142
143
        $translation
144
            ->expects($this->once())
145
            ->method('getName')
146
            ->will($this->returnValue($name = 'name'));
147
148
        $this->factoryRegistry
149
            ->expects($this->once())
150
            ->method('offsetGet')
151
            ->with($this->identicalTo($name))
152
            ->will($this->returnValue($translationFactory = $this->createFactoryMock()));
153
154
        $translatable
155
            ->expects($this->once())
156
            ->method('setTranslationFactory')
157
            ->with($this->identicalTo($translationFactory));
158
159
        $this->translatableResourceSubscriber->postLoad($event);
160
    }
161
162
    public function testPostLoadWithoutTranslatable()
163
    {
164
        $event = $this->createLifecycleEventArgsMock();
165
        $event
166
            ->expects($this->once())
167
            ->method('getEntity')
168
            ->will($this->returnValue($entity = $this->createStdClassMock()));
169
170
        $entity
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
171
            ->expects($this->never())
172
            ->method('setCurrentLocale');
173
174
        $entity
175
            ->expects($this->never())
176
            ->method('setFallbackLocale');
177
178
        $entity
179
            ->expects($this->never())
180
            ->method('setTranslationFactory');
181
182
        $this->translatableResourceSubscriber->postLoad($event);
183
    }
184
185
    /**
186
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
187
     */
188
    private function createContainerMock()
189
    {
190
        return $this->createMock(ContainerInterface::class);
191
    }
192
193
    /**
194
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
195
     */
196
    private function createRegistryMock()
197
    {
198
        return $this->createMock(RegistryInterface::class);
199
    }
200
201
    /**
202
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
203
     */
204
    private function createLocaleContextMock()
205
    {
206
        return $this->createMock(LocaleContextInterface::class);
207
    }
208
209
    /**
210
     * @return \PHPUnit_Framework_MockObject_MockObject|LifecycleEventArgs
211
     */
212
    private function createLifecycleEventArgsMock()
213
    {
214
        return $this->createMock(LifecycleEventArgs::class);
215
    }
216
217
    /**
218
     * @return \PHPUnit_Framework_MockObject_MockObject|TranslatableInterface
219
     */
220
    private function createTranslatableMock()
221
    {
222
        return $this->createMock(TranslatableInterface::class);
223
    }
224
225
    /**
226
     * @return \PHPUnit_Framework_MockObject_MockObject|\stdClass
227
     */
228
    private function createStdClassMock()
229
    {
230
        return $this
231
            ->getMockBuilder(\stdClass::class)
232
            ->setMethods([
233
                'setCurrentLocale',
234
                'setFallbackLocale',
235
                'setTranslationFactory',
236
            ])
237
            ->getMock();
238
    }
239
240
    /**
241
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
242
     */
243
    private function createResourceMock()
244
    {
245
        return $this->createMock(ResourceInterface::class);
246
    }
247
248
    /**
249
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
250
     */
251
    private function createFactoryMock()
252
    {
253
        return $this->createMock(FactoryInterface::class);
254
    }
255
}
256