Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

LongPollingUtilsTest::getEvolvingPersonRepo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Tests\LongPolling;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\PersonRepository;
15
use LoginCidadao\CoreBundle\LongPolling\LongPollingUtils;
16
use LoginCidadao\CoreBundle\Model\PersonInterface;
17
use Symfony\Component\Stopwatch\Stopwatch;
18
19
/**
20
 * @group time-sensitive
21
 */
22
class LongPollingUtilsTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testRunTimeLimited()
25
    {
26
        $timer = 'timeLimited';
27
        $stopwatch = new Stopwatch();
28
        $stopwatch->start($timer);
29
30
        $longPolling = new LongPollingUtils($this->getEntityManager(), 60);
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\CoreBundle\...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31
32
        $count = 5;
33
        $response = $longPolling->runTimeLimited(function () use (&$count) {
34
            return --$count <= 0;
35
        });
36
        $duration = $stopwatch->stop($timer)->getDuration();
37
38
        $this->assertTrue($response);
39
        $this->assertEquals(0, $count);
40
        $this->assertLessThan(60, $duration);
41
    }
42
43
    public function testTimeout()
44
    {
45
        $this->setExpectedException('LoginCidadao\APIBundle\Exception\RequestTimeoutException');
46
47
        $longPolling = new LongPollingUtils($this->getEntityManager(), 30);
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\CoreBundle\...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
        $longPolling->runTimeLimited(function () {
49
            return false;
50
        });
51
    }
52
53
    public function testEntityUpdateCheckerCallback()
54
    {
55
        $updatedAt = new \DateTime('-1 hour');
56
        $currentUpdatedAt = new \DateTime('-1 hour');
57
58
        $person = $this->getPerson();
59
        $person->expects($this->any())->method('getUpdatedAt')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
60
            ->willReturnCallback(function () use (&$currentUpdatedAt) {
61
                return $currentUpdatedAt;
62
            });
63
64
        $em = $this->getEntityManager();
65
        $em->expects($this->once())->method('getRepository')
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...
66
            ->willReturn($this->getEvolvingPersonRepo($person, $currentUpdatedAt));
67
68
        $longPolling = new LongPollingUtils($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 64 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() 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...
69
        $callback = $longPolling->getEntityUpdateCheckerCallback($person, $updatedAt);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 58 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...UpdateCheckerCallback() does only seem to accept object<LoginCidadao\Core...\LongPollableInterface>, 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...
70
71
        call_user_func($callback);
72
        $response = call_user_func($callback);
73
74
        $this->assertNotFalse($response);
75
        $this->assertNotEquals($updatedAt, $currentUpdatedAt);
76
        $this->assertEquals($person, $response);
77
    }
78
79
    public function testEntityUpdateCheckerCallbackNeverUpdated()
80
    {
81
        $person = $this->getPerson();
82
        $person->expects($this->once())->method('getUpdatedAt')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
83
84
        $repo = $this->getPersonRepository();
85
        $repo->expects($this->any())->method('find')->willReturn($person);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Entity\PersonRepository.

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...
86
87
        $em = $this->getEntityManager();
88
        $em->expects($this->once())->method('getRepository')->willReturn($repo);
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...
89
90
        $longPolling = new LongPollingUtils($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 87 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() 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...
91
        $callback = $longPolling->getEntityUpdateCheckerCallback($person, new \DateTime());
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 81 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...UpdateCheckerCallback() does only seem to accept object<LoginCidadao\Core...\LongPollableInterface>, 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...
92
        $response = call_user_func($callback);
93
94
        $this->assertFalse($response);
95
    }
96
97
    public function testObviouslyValidEmail()
98
    {
99
        $person = $this->getPerson();
100
        $person->expects($this->once())->method('getEmailConfirmedAt')->willReturn(new \DateTime());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
101
102
        $longPolling = new LongPollingUtils($this->getEntityManager());
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\CoreBundle\...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
        $response = $longPolling->waitValidEmail($person, new \DateTime());
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 99 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...Utils::waitValidEmail() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
104
105
        $this->assertTrue($response);
106
    }
107
108
    public function testWaitValidEmail()
109
    {
110
        $updatedAt = new \DateTime('-1 hour');
111
        $currentUpdatedAt = new \DateTime('-1 hour');
112
113
        $person = $this->getPerson();
114
        $person->expects($this->any())->method('getUpdatedAt')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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
            ->willReturnCallback(function () use (&$currentUpdatedAt) {
116
                return $currentUpdatedAt;
117
            });
118
119
        $repo = $this->getEvolvingPersonRepo($person, $currentUpdatedAt);
120
121
        $em = $this->getEntityManager();
122
        $em->expects($this->once())->method('getRepository')->willReturn($repo);
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...
123
124
        $longPolling = new LongPollingUtils($em);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 121 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...ingUtils::__construct() 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...
125
        $response = $longPolling->waitValidEmail($person, $updatedAt);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 113 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...Utils::waitValidEmail() does only seem to accept object<LoginCidadao\Core...\Model\PersonInterface>, 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...
126
127
        $this->assertTrue($response);
128
    }
129
130
    /**
131
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
132
     */
133
    private function getEntityManager()
134
    {
135
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
136
137
        return $em;
138
    }
139
140
    /**
141
     * @return \PHPUnit_Framework_MockObject_MockObject|PersonRepository
142
     */
143
    private function getPersonRepository()
144
    {
145
        $repo = $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository')
146
            ->disableOriginalConstructor()->getMock();
147
148
        return $repo;
149
    }
150
151
    /**
152
     * @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject
153
     */
154
    private function getPerson()
155
    {
156
        /** @var \PHPUnit_Framework_MockObject_MockObject|PersonInterface $person */
157
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
158
        $person->expects($this->any())->method('getId')->willReturn(123);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
159
160
        return $person;
161
    }
162
163
    /**
164
     * @param PersonInterface|\PHPUnit_Framework_MockObject_MockObject $person
165
     * @param \DateTime|null $currentUpdatedAt
166
     * @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject
167
     */
168
    private function getEvolvingPersonRepo(&$person, &$currentUpdatedAt)
169
    {
170
        $repo = $this->getPersonRepository();
171
        $repo->expects($this->at(0))->method('find')->willReturn($person);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Entity\PersonRepository.

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...
172
        $repo->expects($this->at(1))->method('find')
173
            ->willReturnCallback(function () use (&$person, &$currentUpdatedAt) {
174
                $currentUpdatedAt = new \DateTime();
175
                $person->expects($this->any())->method('getEmailConfirmedAt')->willReturn($currentUpdatedAt);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
176
177
                return $person;
178
            });
179
180
        return $repo;
181
    }
182
}
183