Completed
Pull Request — master (#61)
by Eric
31:37
created

testCreateQueryBuilderForCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 1
eloc 43
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DocumentRepository;
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Doctrine\ODM\MongoDB\Query\Builder;
18
use Doctrine\ODM\MongoDB\UnitOfWork;
19
use Lug\Component\Resource\Model\ResourceInterface;
20
use Lug\Component\Translation\Context\LocaleContextInterface;
21
use Lug\Component\Translation\Model\TranslatableInterface;
22
use Lug\Component\Translation\Model\TranslatableTrait;
23
use Lug\Component\Translation\Model\TranslationInterface;
24
use Lug\Component\Translation\Model\TranslationTrait;
25
use Lug\Component\Translation\Repository\Doctrine\MongoDB\TranslatableRepository;
26
use Lug\Component\Translation\Repository\TranslatableRepositoryInterface;
27
28
/**
29
 * @author GeLo <[email protected]>
30
 */
31
class TranslatableRepositoryTest extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var TranslatableRepository
35
     */
36
    private $translatableRepository;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject|DocumentManager
40
     */
41
    private $documentManager;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
45
     */
46
    private $unitOfWork;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
50
     */
51
    private $classMetadata;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
55
     */
56
    private $resource;
57
58
    /**
59
     * @var string
60
     */
61
    private $class;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
65
     */
66
    private $localeContext;
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function setUp()
72
    {
73
        if (!class_exists(DocumentManager::class)) {
74
            $this->markTestSkipped();
75
        }
76
77
        $this->documentManager = $this->createDocumentManagerMock();
78
        $this->unitOfWork = $this->createUnitOfWorkMock();
79
        $this->classMetadata = $this->createClassMetadataMock();
80
        $this->resource = $this->createResourceMock();
81
        $this->localeContext = $this->createLocaleContextMock();
82
        $this->class = $this->classMetadata->name = TranslatableTest::class;
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
84
        $this->translatableRepository = new TranslatableRepository(
85
            $this->documentManager,
86
            $this->unitOfWork,
87
            $this->classMetadata,
88
            $this->resource,
89
            $this->localeContext
90
        );
91
    }
92
93
    public function testInheritance()
94
    {
95
        $this->assertInstanceOf(TranslatableRepositoryInterface::class, $this->translatableRepository);
96
        $this->assertInstanceOf(DocumentRepository::class, $this->translatableRepository);
97
    }
98
99
    public function testCreateQueryBuilderForCollection()
100
    {
101
        $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...
102
            ->expects($this->once())
103
            ->method('getFieldNames')
104
            ->will($this->returnValue([]));
105
106
        $this->classMetadata
107
            ->expects($this->once())
108
            ->method('getAssociationTargetClass')
109
            ->with($this->identicalTo('translations'))
110
            ->will($this->returnValue($translationClass = TranslationTest::class));
111
112
        $translationClassMetadata = $this->createClassMetadataMock();
113
        $translationClassMetadata
114
            ->expects($this->once())
115
            ->method('getFieldNames')
116
            ->will($this->returnValue(['locale']));
117
118
        $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...
119
            ->expects($this->once())
120
            ->method('getClassMetadata')
121
            ->with($this->identicalTo($translationClass))
122
            ->will($this->returnValue($translationClassMetadata));
123
124
        $this->documentManager
125
            ->expects($this->once())
126
            ->method('createQueryBuilder')
127
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
128
129
        $queryBuilder
130
            ->expects($this->once())
131
            ->method('field')
132
            ->with($this->identicalTo('translations.locale'))
133
            ->will($this->returnSelf());
134
135
        $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...
136
            ->expects($this->once())
137
            ->method('getLocales')
138
            ->will($this->returnValue($locales = ['fr']));
139
140
        $this->localeContext
141
            ->expects($this->once())
142
            ->method('getFallbackLocale')
143
            ->will($this->returnValue($fallbackLocale = 'en'));
144
145
        $queryBuilder
146
            ->expects($this->once())
147
            ->method('in')
148
            ->with($this->identicalTo(array_merge($locales, [$fallbackLocale])))
149
            ->will($this->returnSelf());
150
151
        $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection());
152
    }
153
154
    /**
155
     * @return \PHPUnit_Framework_MockObject_MockObject|DocumentManager
156
     */
157
    private function createDocumentManagerMock()
158
    {
159
        return $this->createMock(DocumentManager::class);
160
    }
161
162
    /**
163
     * @return \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
164
     */
165
    private function createUnitOfWorkMock()
166
    {
167
        return $this->createMock(UnitOfWork::class);
168
    }
169
170
    /**
171
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
172
     */
173
    private function createClassMetadataMock()
174
    {
175
        return $this->createMock(ClassMetadata::class);
176
    }
177
178
    /**
179
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
180
     */
181
    private function createResourceMock()
182
    {
183
        return $this->createMock(ResourceInterface::class);
184
    }
185
186
    /**
187
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
188
     */
189
    private function createLocaleContextMock()
190
    {
191
        return $this->createMock(LocaleContextInterface::class);
192
    }
193
194
    /**
195
     * @return \PHPUnit_Framework_MockObject_MockObject|Builder
196
     */
197
    private function createQueryBuilderMock()
198
    {
199
        return $this->createMock(Builder::class);
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