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

RepositoryFactoryTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 49.64 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 69
loc 139
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testInheritance() 0 4 1
B testRepository() 36 36 1
B testResourceRepository() 33 33 1
A createResourceRegistryMock() 0 4 1
A createEntityManagerMock() 0 4 1
A createClassMetadataMock() 0 6 1
A createConfigurationMock() 0 4 1
A createResourceMock() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Resource\Tests\Repository\Doctrine\ORM;
13
14
use Doctrine\ORM\Configuration;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Doctrine\ORM\EntityRepository;
17
use Doctrine\ORM\Mapping\ClassMetadata;
18
use Doctrine\ORM\Repository\RepositoryFactory as RepositoryFactoryInterface;
19
use Lug\Component\Registry\Model\RegistryInterface;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository;
22
use Lug\Component\Resource\Repository\Doctrine\ORM\RepositoryFactory;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class RepositoryFactoryTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var RepositoryFactory
31
     */
32
    private $repositoryFactory;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
36
     */
37
    private $resourceRegistry;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->resourceRegistry = $this->createResourceRegistryMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createResourceRegistryMock() can also be of type object<Lug\Component\Reg...odel\RegistryInterface>. However, the property $resourceRegistry 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...
45
        $this->repositoryFactory = new RepositoryFactory($this->resourceRegistry);
46
    }
47
48
    public function testInheritance()
49
    {
50
        $this->assertInstanceOf(RepositoryFactoryInterface::class, $this->repositoryFactory);
51
    }
52
53 View Code Duplication
    public function testRepository()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $entityManager = $this->createEntityManagerMock();
56
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
57
            ->expects($this->exactly(3))
58
            ->method('getClassMetadata')
59
            ->with($this->identicalTo($entityName = 'entity'))
60
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
61
62
        $entityManager
63
            ->expects($this->once())
64
            ->method('getConfiguration')
65
            ->will($this->returnValue($configuration = $this->createConfigurationMock()));
66
67
        $configuration
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Configuration.

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...
68
            ->expects($this->once())
69
            ->method('getDefaultRepositoryClassName')
70
            ->will($this->returnValue($repositoryClass = EntityRepository::class));
71
72
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\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...
73
            ->expects($this->exactly(3))
74
            ->method('getName')
75
            ->will($this->returnValue($entityName));
76
77
        $this->resourceRegistry
78
            ->expects($this->once())
79
            ->method('getIterator')
80
            ->will($this->returnValue(new \ArrayIterator([])));
81
82
        $this->assertInstanceOf(
83
            $repositoryClass,
84
            $repository = $this->repositoryFactory->getRepository($entityManager, $entityName)
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createEntityManagerMock() on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Resource\R...actory::getRepository() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
85
        );
86
87
        $this->assertSame($repository, $this->repositoryFactory->getRepository($entityManager, $entityName));
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createEntityManagerMock() on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Resource\R...actory::getRepository() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
88
    }
89
90 View Code Duplication
    public function testResourceRepository()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $entityManager = $this->createEntityManagerMock();
93
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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->exactly(3))
95
            ->method('getClassMetadata')
96
            ->with($this->identicalTo($entityName = 'entity'))
97
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
98
99
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\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...
100
            ->expects($this->exactly(3))
101
            ->method('getName')
102
            ->will($this->returnValue($entityName));
103
104
        $this->resourceRegistry
105
            ->expects($this->once())
106
            ->method('getIterator')
107
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
108
109
        $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...
110
            ->expects($this->once())
111
            ->method('getModel')
112
            ->will($this->returnValue($entityName));
113
114
        $classMetadata->customRepositoryClassName = $repositoryClass = Repository::class;
115
116
        $this->assertInstanceOf(
117
            $repositoryClass,
118
            $repository = $this->repositoryFactory->getRepository($entityManager, $entityName)
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createEntityManagerMock() on line 92 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Resource\R...actory::getRepository() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
119
        );
120
121
        $this->assertSame($repository, $this->repositoryFactory->getRepository($entityManager, $entityName));
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createEntityManagerMock() on line 92 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Component\Resource\R...actory::getRepository() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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...
122
    }
123
124
    /**
125
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
126
     */
127
    private function createResourceRegistryMock()
128
    {
129
        return $this->getMock(RegistryInterface::class);
130
    }
131
132
    /**
133
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
134
     */
135
    private function createEntityManagerMock()
136
    {
137
        return $this->getMock(EntityManagerInterface::class);
138
    }
139
140
    /**
141
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
142
     */
143
    private function createClassMetadataMock()
144
    {
145
        return $this->getMockBuilder(ClassMetadata::class)
146
            ->disableOriginalConstructor()
147
            ->getMock();
148
    }
149
150
    /**
151
     * @return \PHPUnit_Framework_MockObject_MockObject|Configuration
152
     */
153
    private function createConfigurationMock()
154
    {
155
        return $this->getMock(Configuration::class);
156
    }
157
158
    /**
159
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
160
     */
161
    private function createResourceMock()
162
    {
163
        return $this->getMock(ResourceInterface::class);
164
    }
165
}
166