Completed
Push — master ( d461d1...b123ad )
by Joachim
05:50
created

SiteSettingRepositoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 52.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 33
loc 63
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnNull() 0 10 1
A testFindBySiteIdAndSetting() 11 11 1
A testFindBySiteId() 11 11 1
A testFindBySetting() 11 11 1
A getSiteSettingRepository() 0 10 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
namespace Loevgaard\DandomainAltapayBundle\Tests\Entity;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\SiteSetting;
6
use Loevgaard\DandomainAltapayBundle\Entity\SiteSettingRepository;
7
use PHPUnit\Framework\TestCase;
8
9
class SiteSettingRepositoryTest extends TestCase
10
{
11
    public function testReturnNull()
12
    {
13
        $siteSettingRepository = $this->getSiteSettingRepository();
14
15
        $siteSettingRepository
16
            ->method('findBy')
17
            ->willReturn(null);
18
19
        $this->assertSame(null, $siteSettingRepository->findBySiteId(26));
0 ignored issues
show
Bug introduced by
The method findBySiteId does only exist in Loevgaard\DandomainAltap...y\SiteSettingRepository, but not in PHPUnit_Framework_MockObject_MockObject.

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...
20
    }
21
22 View Code Duplication
    public function testFindBySiteIdAndSetting()
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...
23
    {
24
        $siteSettingRepository = $this->getSiteSettingRepository();
25
26
        $obj = new SiteSetting();
27
        $siteSettingRepository
28
            ->method('findOneBy')
29
            ->willReturn($obj);
30
31
        $this->assertSame($obj, $siteSettingRepository->findBySiteIdAndSetting(26, 'setting'));
0 ignored issues
show
Bug introduced by
The method findBySiteIdAndSetting does only exist in Loevgaard\DandomainAltap...y\SiteSettingRepository, but not in PHPUnit_Framework_MockObject_MockObject.

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...
32
    }
33
34 View Code Duplication
    public function testFindBySiteId()
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...
35
    {
36
        $siteSettingRepository = $this->getSiteSettingRepository();
37
38
        $obj = new SiteSetting();
39
        $siteSettingRepository
40
            ->method('findBy')
41
            ->willReturn([$obj]);
42
43
        $this->assertSame([$obj], $siteSettingRepository->findBySiteId(26));
0 ignored issues
show
Bug introduced by
The method findBySiteId does only exist in Loevgaard\DandomainAltap...y\SiteSettingRepository, but not in PHPUnit_Framework_MockObject_MockObject.

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...
44
    }
45
46 View Code Duplication
    public function testFindBySetting()
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...
47
    {
48
        $siteSettingRepository = $this->getSiteSettingRepository();
49
50
        $obj = new SiteSetting();
51
        $siteSettingRepository
52
            ->method('findBy')
53
            ->willReturn([$obj]);
54
55
        $this->assertSame([$obj], $siteSettingRepository->findBySetting('setting'));
0 ignored issues
show
Bug introduced by
The method findBySetting does only exist in Loevgaard\DandomainAltap...y\SiteSettingRepository, but not in PHPUnit_Framework_MockObject_MockObject.

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...
56
    }
57
58
    /**
59
     * @return SiteSettingRepository|\PHPUnit_Framework_MockObject_MockObject
60
     */
61
    private function getSiteSettingRepository()
62
    {
63
        /** @var SiteSettingRepository|\PHPUnit_Framework_MockObject_MockObject $siteSettingRepository */
64
        $siteSettingRepository = $this->getMockBuilder(SiteSettingRepository::class)
65
            ->disableOriginalConstructor()
66
            ->setMethods(['findOneBy', 'findBy'])
67
            ->getMock();
68
69
        return $siteSettingRepository;
70
    }
71
}
72