Completed
Push — master ( 625a74...74c3c7 )
by Eric
07:53
created

LocaleDomainSubscriberTest::createResourceMock()   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\Component\LocaleB\Tests\EventSubscriber;
13
14
use Lug\Component\Locale\EventSubscriber\LocaleDomainSubscriber;
15
use Lug\Component\Locale\Model\LocaleInterface;
16
use Lug\Component\Locale\Provider\LocaleProviderInterface;
17
use Lug\Component\Resource\Domain\DomainEvent;
18
use Lug\Component\Resource\Model\ResourceInterface;
19
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
20
use Symfony\Component\Translation\TranslatorInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class LocaleDomainSubscriberTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var LocaleDomainSubscriber
29
     */
30
    private $localeDomainSubscriber;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleProviderInterface
34
     */
35
    private $localeProvider;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject|TranslatorInterface
39
     */
40
    private $translator;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface
44
     */
45
    private $propertyAccessor;
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function setUp()
51
    {
52
        $this->localeProvider = $this->createLocaleProviderMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createLocaleProviderMock() can also be of type object<Lug\Component\Loc...ocaleProviderInterface>. However, the property $localeProvider is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
53
        $this->translator = $this->createTranslatorMock();
54
        $this->propertyAccessor = $this->createPropertyAccessorMock();
55
56
        $this->localeDomainSubscriber = new LocaleDomainSubscriber(
57
            $this->localeProvider,
58
            $this->translator,
59
            $this->propertyAccessor
60
        );
61
    }
62
63
    public function testValidateDefaultLocale()
64
    {
65
        $this->localeProvider
66
            ->expects($this->once())
67
            ->method('getDefaultLocale')
68
            ->will($this->returnValue($locale = $this->createLocaleMock()));
69
70
        $domainEvent = $this->createDomainEventMock();
71
        $domainEvent
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Domain\DomainEvent.

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...
72
            ->expects($this->once())
73
            ->method('getObject')
74
            ->will($this->returnValue($locale));
75
76
        $domainEvent
77
            ->expects($this->once())
78
            ->method('getResource')
79
            ->will($this->returnValue($resource = $this->createResourceMock()));
80
81
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
82
            ->expects($this->exactly(2))
83
            ->method('getName')
84
            ->will($this->returnValue($resourceName = 'resource'));
85
86
        $resource
87
            ->expects($this->once())
88
            ->method('getLabelPropertyPath')
89
            ->will($this->returnValue($labelPropertyPath = 'code'));
90
91
        $this->propertyAccessor
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Proper...opertyAccessorInterface.

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...
92
            ->expects($this->once())
93
            ->method('getValue')
94
            ->with($this->identicalTo($locale), $this->identicalTo($labelPropertyPath))
95
            ->will($this->returnValue($localeCode = 'code'));
96
97
        $domainEvent
98
            ->expects($this->once())
99
            ->method('setStopped')
100
            ->with($this->identicalTo(true));
101
102
        $domainEvent
103
            ->expects($this->once())
104
            ->method('setStatusCode')
105
            ->with($this->identicalTo(409));
106
107
        $domainEvent
108
            ->expects($this->once())
109
            ->method('setMessageType')
110
            ->with($this->identicalTo('error'));
111
112
        $domainEvent
113
            ->expects($this->once())
114
            ->method('getAction')
115
            ->will($this->returnValue('action'));
116
117
        $this->translator
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Translation\TranslatorInterface.

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...
118
            ->expects($this->once())
119
            ->method('trans')
120
            ->with(
121
                $this->identicalTo('lug.resource.action.default'),
122
                $this->identicalTo(['%resource%' => $localeCode]),
123
                $this->identicalTo('flashes')
124
            )
125
            ->will($this->returnValue($translation = 'translation'));
126
127
        $domainEvent
128
            ->expects($this->once())
129
            ->method('setMessage')
130
            ->with($this->identicalTo($translation));
131
132
        $this->localeDomainSubscriber->validateDefaultLocale($domainEvent);
0 ignored issues
show
Bug introduced by
It seems like $domainEvent defined by $this->createDomainEventMock() on line 70 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Locale\Eve...validateDefaultLocale() does only seem to accept object<Lug\Component\Resource\Domain\DomainEvent>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
133
    }
134
135
    public function testValidateNotDefaultLocale()
136
    {
137
        $this->localeProvider
138
            ->expects($this->once())
139
            ->method('getDefaultLocale')
140
            ->will($this->returnValue($this->createLocaleMock()));
141
142
        $domainEvent = $this->createDomainEventMock();
143
        $domainEvent
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Domain\DomainEvent.

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...
144
            ->expects($this->once())
145
            ->method('getObject')
146
            ->will($this->returnValue($this->createLocaleMock()));
147
148
        $domainEvent
149
            ->expects($this->never())
150
            ->method('setStopped');
151
152
        $domainEvent
153
            ->expects($this->never())
154
            ->method('setStatusCode');
155
156
        $domainEvent
157
            ->expects($this->never())
158
            ->method('setMessageType');
159
160
        $domainEvent
161
            ->expects($this->never())
162
            ->method('setMessage');
163
164
        $this->localeDomainSubscriber->validateDefaultLocale($domainEvent);
0 ignored issues
show
Bug introduced by
It seems like $domainEvent defined by $this->createDomainEventMock() on line 142 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Locale\Eve...validateDefaultLocale() does only seem to accept object<Lug\Component\Resource\Domain\DomainEvent>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
165
    }
166
167
    public function testSubscribedEvents()
168
    {
169
        $this->assertSame(
170
            ['lug.locale.pre_delete' => 'validateDefaultLocale'],
171
            $this->localeDomainSubscriber->getSubscribedEvents()
172
        );
173
    }
174
175
    /**
176
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleProviderInterface
177
     */
178
    private function createLocaleProviderMock()
179
    {
180
        return $this->getMock(LocaleProviderInterface::class);
181
    }
182
183
    /**
184
     * @return \PHPUnit_Framework_MockObject_MockObject|TranslatorInterface
185
     */
186
    private function createTranslatorMock()
187
    {
188
        return $this->getMock(TranslatorInterface::class);
189
    }
190
191
    /**
192
     * @return \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface
193
     */
194
    private function createPropertyAccessorMock()
195
    {
196
        return $this->getMock(PropertyAccessorInterface::class);
197
    }
198
199
    /**
200
     * @return \PHPUnit_Framework_MockObject_MockObject|DomainEvent
201
     */
202
    private function createDomainEventMock()
203
    {
204
        return $this->getMockBuilder(DomainEvent::class)
205
            ->disableOriginalConstructor()
206
            ->getMock();
207
    }
208
209
    /**
210
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
211
     */
212
    private function createResourceMock()
213
    {
214
        return $this->getMock(ResourceInterface::class);
215
    }
216
217
    /**
218
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleInterface
219
     */
220
    private function createLocaleMock()
221
    {
222
        return $this->getMock(LocaleInterface::class);
223
    }
224
}
225