CurrentEducationNotationConfigExtensionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Doctrine\Education;
  4. use App\Doctrine\Education\CurrentEducationNotationConfigExtension;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Education\EducationNotationConfig;
  7. use App\Entity\Organization\Organization;
  8. use Doctrine\ORM\QueryBuilder;
  9. use PHPUnit\Framework\TestCase;
  10. use Symfony\Bundle\SecurityBundle\Security;
  11. class CurrentEducationNotationConfigExtensionTest extends TestCase
  12. {
  13. private $security;
  14. private $queryBuilder;
  15. private $extension;
  16. private $user;
  17. private $organization;
  18. protected function setUp(): void
  19. {
  20. $this->security = $this->createMock(Security::class);
  21. $this->queryBuilder = $this->createMock(QueryBuilder::class);
  22. $this->user = $this->createMock(Access::class);
  23. $this->organization = $this->createMock(Organization::class);
  24. $this->extension = new CurrentEducationNotationConfigExtension($this->security);
  25. }
  26. public function testSupportsReturnsTrueForEducationNotationConfigClass()
  27. {
  28. $this->assertTrue($this->extension->supports(EducationNotationConfig::class, null));
  29. }
  30. public function testSupportsReturnsFalseForOtherClasses()
  31. {
  32. $classes = ['OtherClass', Access::class];
  33. foreach ($classes as $class) {
  34. $this->assertFalse($this->extension->supports($class, null));
  35. }
  36. }
  37. public function testAddWhere()
  38. {
  39. $this->user->method('getOrganization')->willReturn($this->organization);
  40. $this->security->method('getUser')->willReturn($this->user);
  41. $this->queryBuilder->method('getRootAliases')->willReturn(['o']);
  42. $this->queryBuilder->expects($this->once())
  43. ->method('andWhere')
  44. ->with('o.organization = :organization')
  45. ->willReturn($this->queryBuilder);
  46. $this->queryBuilder->expects($this->once())
  47. ->method('setParameter')
  48. ->with('organization', $this->organization)
  49. ->willReturn($this->queryBuilder);
  50. $reflection = new \ReflectionClass($this->extension);
  51. $method = $reflection->getMethod('addWhere');
  52. $method->setAccessible(true);
  53. $method->invoke($this->extension, $this->queryBuilder, EducationNotationConfig::class, null);
  54. }
  55. public function testAddWhereWithNullUser()
  56. {
  57. $this->security->method('getUser')->willReturn(null);
  58. $this->queryBuilder->method('getRootAliases')->willReturn(['o']);
  59. $this->queryBuilder->expects($this->never())
  60. ->method('andWhere');
  61. $this->queryBuilder->expects($this->never())
  62. ->method('setParameter');
  63. $reflection = new \ReflectionClass($this->extension);
  64. $method = $reflection->getMethod('addWhere');
  65. $method->setAccessible(true);
  66. $method->invoke($this->extension, $this->queryBuilder, EducationNotationConfig::class, null);
  67. }
  68. public function testAddWhereWithNullOrganization()
  69. {
  70. $this->user->method('getOrganization')->willReturn(null);
  71. $this->security->method('getUser')->willReturn($this->user);
  72. $this->queryBuilder->method('getRootAliases')->willReturn(['o']);
  73. $this->queryBuilder->expects($this->never())
  74. ->method('andWhere');
  75. $this->queryBuilder->expects($this->never())
  76. ->method('setParameter');
  77. $reflection = new \ReflectionClass($this->extension);
  78. $method = $reflection->getMethod('addWhere');
  79. $method->setAccessible(true);
  80. $method->invoke($this->extension, $this->queryBuilder, EducationNotationConfig::class, null);
  81. }
  82. }