These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminBundle\Tests\Helper\Security\Acl; |
||
4 | |||
5 | use DateTime; |
||
6 | use Doctrine\ORM\EntityManager; |
||
7 | use Doctrine\ORM\EntityRepository; |
||
8 | use Kunstmaan\AdminBundle\Entity\Group; |
||
9 | use Kunstmaan\AdminBundle\Entity\User; |
||
10 | use Kunstmaan\AdminBundle\Helper\Security\OAuth\OAuthUserCreator; |
||
11 | use Kunstmaan\AdminBundle\Helper\Security\OAuth\OAuthUserFinderInterface; |
||
12 | use PHPUnit\Framework\TestCase; |
||
13 | use ReflectionClass; |
||
14 | |||
15 | class OAuthUserCreatorTest extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var OAuthUserCreator |
||
19 | */ |
||
20 | private $object; |
||
21 | |||
22 | /** |
||
23 | * @var EntityManager |
||
24 | */ |
||
25 | private $em; |
||
26 | |||
27 | /** |
||
28 | * @var OAuthUserFinderInterface |
||
29 | */ |
||
30 | private $finder; |
||
31 | |||
32 | /** |
||
33 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
0 ignored issues
–
show
|
|||
34 | */ |
||
35 | private function getEm() |
||
36 | { |
||
37 | if (!isset($this->em)) { |
||
38 | $this->em = $this->createMock(EntityManager::class); |
||
0 ignored issues
–
show
It seems like
$this->createMock(\Doctr...M\EntityManager::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
39 | } |
||
40 | |||
41 | return $this->em; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
0 ignored issues
–
show
|
|||
46 | */ |
||
47 | private function getFinder() |
||
48 | { |
||
49 | if (!isset($this->finder)) { |
||
50 | $this->finder = $this->createMock(OAuthUserFinderInterface::class); |
||
0 ignored issues
–
show
It seems like
$this->createMock(\Kunst...FinderInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\AdminBu...uthUserFinderInterface> of property $finder .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
51 | } |
||
52 | |||
53 | return $this->finder; |
||
54 | } |
||
55 | |||
56 | public function setup() |
||
57 | { |
||
58 | $em = $this->getEm(); |
||
59 | $finder = $this->getFinder(); |
||
60 | $object = new OAuthUserCreator($em, [[ |
||
0 ignored issues
–
show
It seems like
$em defined by $this->getEm() on line 58 can also be of type object<PHPUnit\Framework\MockObject\MockObject> ; however, Kunstmaan\AdminBundle\He...rCreator::__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...
|
|||
61 | 'domain_name' => 'gmail.com', |
||
62 | 'access_levels' => ['ROLE_ADMIN'], |
||
63 | ]], User::class, $finder); |
||
0 ignored issues
–
show
It seems like
$finder defined by $this->getFinder() on line 59 can also be of type object<PHPUnit\Framework\MockObject\MockObject> ; however, Kunstmaan\AdminBundle\He...rCreator::__construct() does only seem to accept object<Kunstmaan\AdminBu...uthUserFinderInterface> , 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...
|
|||
64 | $this->object = $object; |
||
65 | } |
||
66 | |||
67 | public function testGetOrCreateUserReturnsNull() |
||
68 | { |
||
69 | $object = $this->object; |
||
70 | $user = $object->getOrCreateUser('[email protected]', 12345679); |
||
71 | $this->assertNull($user); |
||
72 | } |
||
73 | |||
74 | View Code Duplication | public function testGetOrCreateUserReturnsUser() |
|
0 ignored issues
–
show
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...
|
|||
75 | { |
||
76 | $object = $this->object; |
||
77 | $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new User()); |
||
0 ignored issues
–
show
The method
expects does only exist in PHPUnit\Framework\MockObject\MockObject , but not in Kunstmaan\AdminBundle\He...AuthUserFinderInterface .
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
Loading history...
|
|||
78 | $mockGroup = $this->createMock(Group::class); |
||
79 | $mockRepo = $this->createMock(EntityRepository::class); |
||
80 | $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup); |
||
81 | $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo); |
||
0 ignored issues
–
show
The method
expects does only exist in PHPUnit\Framework\MockObject\MockObject , 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
Loading history...
|
|||
82 | $this->getEm()->expects($this->once())->method('persist')->willReturn(true); |
||
83 | $this->getEm()->expects($this->once())->method('flush')->willReturn(true); |
||
84 | $user = $object->getOrCreateUser('[email protected]', 12345679); |
||
85 | $this->assertInstanceOf(User::class, $user); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testGetOrCreateUserCreatesCorrectUserClass() |
|
0 ignored issues
–
show
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...
|
|||
89 | { |
||
90 | $object = $this->object; |
||
91 | $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new DateTime()); |
||
0 ignored issues
–
show
The method
expects does only exist in PHPUnit\Framework\MockObject\MockObject , but not in Kunstmaan\AdminBundle\He...AuthUserFinderInterface .
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
Loading history...
|
|||
92 | $mockGroup = $this->createMock(Group::class); |
||
93 | $mockRepo = $this->createMock(EntityRepository::class); |
||
94 | $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup); |
||
95 | $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo); |
||
0 ignored issues
–
show
The method
expects does only exist in PHPUnit\Framework\MockObject\MockObject , 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
Loading history...
|
|||
96 | $this->getEm()->expects($this->once())->method('persist')->willReturn(true); |
||
97 | $this->getEm()->expects($this->once())->method('flush')->willReturn(true); |
||
98 | $user = $object->getOrCreateUser('[email protected]', 12345679); |
||
99 | $this->assertInstanceOf(User::class, $user); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @throws \ReflectionException |
||
104 | */ |
||
105 | public function testAccessLevelsReturnsNull() |
||
106 | { |
||
107 | $em = $this->getEm(); |
||
108 | $finder = $this->getFinder(); |
||
109 | $object = new OAuthUserCreator($em, [[ |
||
0 ignored issues
–
show
It seems like
$em defined by $this->getEm() on line 107 can also be of type object<PHPUnit\Framework\MockObject\MockObject> ; however, Kunstmaan\AdminBundle\He...rCreator::__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...
|
|||
110 | 'domain_name' => 'gmail.com', |
||
111 | 'access_levels' => ['ROLE_ADMIN'], |
||
112 | ]], User::class, $finder); |
||
0 ignored issues
–
show
It seems like
$finder defined by $this->getFinder() on line 108 can also be of type object<PHPUnit\Framework\MockObject\MockObject> ; however, Kunstmaan\AdminBundle\He...rCreator::__construct() does only seem to accept object<Kunstmaan\AdminBu...uthUserFinderInterface> , 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...
|
|||
113 | |||
114 | $mirror = new ReflectionClass(OAuthUserCreator::class); |
||
115 | $method = $mirror->getMethod('getAccessLevels'); |
||
116 | $method->setAccessible(true); |
||
117 | $levels = $method->invoke($object, '[email protected]'); |
||
118 | $this->assertNull($levels); |
||
119 | } |
||
120 | } |
||
121 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.