Completed
Push — master ( 74c3c7...591b88 )
by Eric
08:09
created

createLocaleContextMock()   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\Translation\Tests\Repository\Doctrine\MongoDB;
13
14
use Doctrine\ODM\MongoDB\DocumentManager;
15
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
16
use Doctrine\ODM\MongoDB\Query\Builder;
17
use Doctrine\ODM\MongoDB\UnitOfWork;
18
use Lug\Component\Resource\Model\ResourceInterface;
19
use Lug\Component\Translation\Context\LocaleContextInterface;
20
use Lug\Component\Translation\Model\TranslatableInterface;
21
use Lug\Component\Translation\Model\TranslatableTrait;
22
use Lug\Component\Translation\Model\TranslationInterface;
23
use Lug\Component\Translation\Model\TranslationTrait;
24
use Lug\Component\Translation\Repository\Doctrine\MongoDB\TranslatableRepository;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
class TranslatableRepositoryTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var TranslatableRepository
33
     */
34
    private $translatableRepository;
35
36
    /**
37
     * @var \PHPUnit_Framework_MockObject_MockObject|DocumentManager
38
     */
39
    private $documentManager;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
43
     */
44
    private $unitOfWork;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
48
     */
49
    private $classMetadata;
50
51
    /**
52
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
53
     */
54
    private $resource;
55
56
    /**
57
     * @var string
58
     */
59
    private $class;
60
61
    /**
62
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
63
     */
64
    private $localeContext;
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function setUp()
70
    {
71
        if (!class_exists(DocumentManager::class)) {
72
            $this->markTestSkipped();
73
        }
74
75
        $this->documentManager = $this->createDocumentManagerMock();
76
        $this->unitOfWork = $this->createUnitOfWorkMock();
77
        $this->classMetadata = $this->createClassMetadataMock();
78
        $this->resource = $this->createResourceMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createResourceMock() can also be of type object<Lug\Component\Res...odel\ResourceInterface>. However, the property $resource 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...
79
        $this->localeContext = $this->createLocaleContextMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createLocaleContextMock() can also be of type object<Lug\Component\Tra...LocaleContextInterface>. However, the property $localeContext 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...
80
        $this->class = $this->classMetadata->name = TranslatableTest::class;
81
82
        $this->translatableRepository = new TranslatableRepository(
83
            $this->documentManager,
84
            $this->unitOfWork,
85
            $this->classMetadata,
86
            $this->resource,
87
            $this->localeContext
88
        );
89
    }
90
91
    public function testCreateQueryBuilderForCollection()
92
    {
93
        $this->classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Mapping\ClassMetadata.

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...
94
            ->expects($this->once())
95
            ->method('getFieldNames')
96
            ->will($this->returnValue([]));
97
98
        $this->classMetadata
99
            ->expects($this->once())
100
            ->method('getAssociationTargetClass')
101
            ->with($this->identicalTo('translations'))
102
            ->will($this->returnValue($translationClass = TranslationTest::class));
103
104
        $translationClassMetadata = $this->createClassMetadataMock();
105
        $translationClassMetadata
106
            ->expects($this->once())
107
            ->method('getFieldNames')
108
            ->will($this->returnValue(['locale']));
109
110
        $this->documentManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\DocumentManager.

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...
111
            ->expects($this->once())
112
            ->method('getClassMetadata')
113
            ->with($this->identicalTo($translationClass))
114
            ->will($this->returnValue($translationClassMetadata));
115
116
        $this->documentManager
117
            ->expects($this->once())
118
            ->method('createQueryBuilder')
119
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
120
121
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
122
            ->expects($this->once())
123
            ->method('field')
124
            ->with($this->identicalTo('translations.locale'))
125
            ->will($this->returnSelf());
126
127
        $this->localeContext
128
            ->expects($this->once())
129
            ->method('getLocales')
130
            ->will($this->returnValue($locales = ['fr']));
131
132
        $this->localeContext
133
            ->expects($this->once())
134
            ->method('getFallbackLocale')
135
            ->will($this->returnValue($fallbackLocale = 'en'));
136
137
        $queryBuilder
138
            ->expects($this->once())
139
            ->method('in')
140
            ->with($this->identicalTo(array_merge($locales, [$fallbackLocale])))
141
            ->will($this->returnSelf());
142
143
        $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection());
144
    }
145
146
    /**
147
     * @return \PHPUnit_Framework_MockObject_MockObject|DocumentManager
148
     */
149
    private function createDocumentManagerMock()
150
    {
151
        return $this->getMockBuilder(DocumentManager::class)
152
            ->disableOriginalConstructor()
153
            ->getMock();
154
    }
155
156
    /**
157
     * @return \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
158
     */
159
    private function createUnitOfWorkMock()
160
    {
161
        return $this->getMockBuilder(UnitOfWork::class)
162
            ->disableOriginalConstructor()
163
            ->getMock();
164
    }
165
166
    /**
167
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
168
     */
169
    private function createClassMetadataMock()
170
    {
171
        return $this->getMockBuilder(ClassMetadata::class)
172
            ->disableOriginalConstructor()
173
            ->getMock();
174
    }
175
176
    /**
177
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
178
     */
179
    private function createResourceMock()
180
    {
181
        return $this->getMock(ResourceInterface::class);
182
    }
183
184
    /**
185
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
186
     */
187
    private function createLocaleContextMock()
188
    {
189
        return $this->getMock(LocaleContextInterface::class);
190
    }
191
192
    /**
193
     * @return \PHPUnit_Framework_MockObject_MockObject|Builder
194
     */
195
    private function createQueryBuilderMock()
196
    {
197
        return $this->getMockBuilder(Builder::class)
198
            ->disableOriginalConstructor()
199
            ->getMock();
200
    }
201
}
202
203
/**
204
 * @author GeLo <[email protected]>
205
 */
206
class TranslatableTest implements TranslatableInterface
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...
207
{
208
    use TranslatableTrait;
209
}
210
211
/**
212
 * @author GeLo <[email protected]>
213
 */
214
class TranslationTest implements TranslationInterface
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...
215
{
216
    use TranslationTrait;
217
}
218