| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JhUserTest\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use JhUserTest\Util\ServiceManagerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JhUserTest\Fixture\SinglePermission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class PermissionRepositoryTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package JhUserTest\Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Aydin Hassan <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class PermissionRepositoryTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $fixtureExectutor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var \JhUser\Repository\RoleRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $sm = ServiceManagerFactory::getServiceManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->repository = $sm->get('JhUser\Repository\PermissionRepository'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->fixtureExectutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertInstanceOf('JhUser\Repository\PermissionRepository', $this->repository); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function testGetAllPermissions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $permission = new SinglePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->fixtureExectutor->execute([$permission]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertCount(1, $this->repository->findAll()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testFindByPermissionNameReturnsNullIfNotExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->assertNull($this->repository->findByName("delete")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function testFindByPermissionNameReturnsPermissionIfExists() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $permission = new SinglePermission(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->fixtureExectutor->execute([$permission]); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $result = $this->repository->findByName((string) $permission->getPermission()); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertInstanceOf('JhUser\Entity\Permission', $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertSame($permission->getPermission()->getId(), $result->getId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertSame((string) $permission->getPermission(), (string) $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function testFindOneByReturnsNullIfNotExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertNull($this->repository->findOneBy(["name" => "super-admin"])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function testFindOneByReturnsPermissionIfExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $permission = new SinglePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->fixtureExectutor->execute([$permission]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $result = $this->repository->findOneBy(["name" => "delete"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertInstanceOf('JhUser\Entity\Permission', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertSame($permission->getPermission()->getId(), $result->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertSame((string) $permission->getPermission(), (string) $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function testFindByReturnsEmptyIfNonExist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertEmpty($this->repository->findBy(['name' => 'admin'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function testFindByReturnsCollectionIfExist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertEmpty($this->repository->findBy(['name' => 'user'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $permission = new SinglePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->fixtureExectutor->execute([$permission]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $result = $this->repository->findBy(["name" => (string) $permission->getPermission()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertSame(1, count($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function testFindById() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $permission = new SinglePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->fixtureExectutor->execute([$permission]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $result = $this->repository->find($permission->getPermission()->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->assertInstanceOf('JhUser\Entity\Permission', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertSame($permission->getPermission()->getId(), $result->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertSame((string) $permission->getPermission(), (string) $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function testGetClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertSame('JhUser\Entity\Permission', $this->repository->getClassName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  | 
            
                        
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.