Completed
Pull Request — master (#7)
by Yann
02:14
created

DoctrineORMTokenRepositoryTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 18.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 22
loc 122
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 7 1
A repository() 0 4 1
A it_throw_exception_if_token_not_found() 0 8 1
A it_throw_exception_if_token_expired() 0 10 1
A it_throw_exception_if_token_used() 0 12 1
A it_get_valid_token() 0 12 1
A it_create_token() 11 11 1
A it_update_token() 11 11 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 Yokai\SecurityTokenBundle\Tests\Repository;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Yokai\SecurityTokenBundle\Entity\Token;
10
use Yokai\SecurityTokenBundle\Repository\DoctrineORMTokenRepository;
11
12
/**
13
 * @author Yann Eugoné <[email protected]>
14
 */
15
class DoctrineORMTokenRepositoryTest extends TestCase
16
{
17
    /**
18
     * @var EntityManager|ObjectProphecy
19
     */
20
    private $manager;
21
22
    /**
23
     * @var EntityRepository|ObjectProphecy
24
     */
25
    private $repository;
26
27
    protected function setUp()
28
    {
29
        $this->manager = $this->prophesize(EntityManager::class);
30
        $this->repository = $this->prophesize(EntityRepository::class);
31
    }
32
33
    protected function tearDown()
34
    {
35
        unset(
36
            $this->manager,
37
            $this->repository
38
        );
39
    }
40
41
    protected function repository()
42
    {
43
        return new DoctrineORMTokenRepository($this->manager->reveal(), $this->repository->reveal());
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Doctrine\ORM\EntityManager.

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...
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Doctrine\ORM\EntityRepository.

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
    /**
47
     * @test
48
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenNotFoundException
49
     */
50
    public function it_throw_exception_if_token_not_found()
51
    {
52
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
0 ignored issues
show
Bug introduced by
The method findOneBy does only exist in Doctrine\ORM\EntityRepository, but not in Prophecy\Prophecy\ObjectProphecy.

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...
53
            ->shouldBeCalledTimes(1)
54
            ->willReturn(null);
55
56
        $this->repository()->get('unique', 'init_password');
57
    }
58
59
    /**
60
     * @test
61
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenExpiredException
62
     */
63
    public function it_throw_exception_if_token_expired()
64
    {
65
        $token = new Token('string', 'jdoe','unique', 'init_password',  '-1 day', []);
66
67
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
0 ignored issues
show
Bug introduced by
The method findOneBy does only exist in Doctrine\ORM\EntityRepository, but not in Prophecy\Prophecy\ObjectProphecy.

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
            ->shouldBeCalledTimes(1)
69
            ->willReturn($token);
70
71
        $this->repository()->get('unique', 'init_password');
72
    }
73
74
    /**
75
     * @test
76
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenUsedException
77
     */
78
    public function it_throw_exception_if_token_used()
79
    {
80
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
81
        $token->setUsedAt(new \DateTime());
82
        $token->setUsedInformation(['info']);
83
84
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
0 ignored issues
show
Bug introduced by
The method findOneBy does only exist in Doctrine\ORM\EntityRepository, but not in Prophecy\Prophecy\ObjectProphecy.

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...
85
            ->shouldBeCalledTimes(1)
86
            ->willReturn($token);
87
88
        $this->repository()->get('unique', 'init_password');
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function it_get_valid_token()
95
    {
96
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
97
98
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
0 ignored issues
show
Bug introduced by
The method findOneBy does only exist in Doctrine\ORM\EntityRepository, but not in Prophecy\Prophecy\ObjectProphecy.

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...
99
            ->shouldBeCalledTimes(1)
100
            ->willReturn($token);
101
102
        $got = $this->repository()->get('unique', 'init_password');
103
104
        self::assertSame($token, $got);
105
    }
106
107
    /**
108
     * @test
109
     */
110 View Code Duplication
    public function it_create_token()
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...
111
    {
112
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
113
114
        $this->manager->persist($token)
0 ignored issues
show
Bug introduced by
The method persist does only exist in Doctrine\ORM\EntityManager, but not in Prophecy\Prophecy\ObjectProphecy.

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...
115
            ->shouldBeCalledTimes(1);
116
        $this->manager->flush($token)
0 ignored issues
show
Bug introduced by
The method flush does only exist in Doctrine\ORM\EntityManager, but not in Prophecy\Prophecy\ObjectProphecy.

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...
117
            ->shouldBeCalledTimes(1);
118
119
        $this->repository()->create($token);
120
    }
121
122
    /**
123
     * @test
124
     */
125 View Code Duplication
    public function it_update_token()
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...
126
    {
127
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
128
129
        $this->manager->persist($token)
0 ignored issues
show
Bug introduced by
The method persist does only exist in Doctrine\ORM\EntityManager, but not in Prophecy\Prophecy\ObjectProphecy.

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...
130
            ->shouldBeCalledTimes(1);
131
        $this->manager->flush($token)
0 ignored issues
show
Bug introduced by
The method flush does only exist in Doctrine\ORM\EntityManager, but not in Prophecy\Prophecy\ObjectProphecy.

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...
132
            ->shouldBeCalledTimes(1);
133
134
        $this->repository()->update($token);
135
    }
136
}
137