MaskBuilderTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Acl\Tests\Permission;
  11. use Symfony\Component\Security\Acl\Permission\MaskBuilder;
  12. class MaskBuilderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @expectedException \InvalidArgumentException
  16. * @dataProvider getInvalidConstructorData
  17. */
  18. public function testConstructorWithNonInteger($invalidMask)
  19. {
  20. new MaskBuilder($invalidMask);
  21. }
  22. public function getInvalidConstructorData()
  23. {
  24. return array(
  25. array(234.463),
  26. array('asdgasdf'),
  27. array(array()),
  28. array(new \stdClass()),
  29. );
  30. }
  31. public function testConstructorWithoutArguments()
  32. {
  33. $builder = new MaskBuilder();
  34. $this->assertEquals(0, $builder->get());
  35. }
  36. public function testConstructor()
  37. {
  38. $builder = new MaskBuilder(123456);
  39. $this->assertEquals(123456, $builder->get());
  40. }
  41. public function testAddAndRemove()
  42. {
  43. $builder = new MaskBuilder();
  44. $builder
  45. ->add('view')
  46. ->add('eDiT')
  47. ->add('ownEr')
  48. ;
  49. $mask = $builder->get();
  50. $this->assertEquals(MaskBuilder::MASK_VIEW, $mask & MaskBuilder::MASK_VIEW);
  51. $this->assertEquals(MaskBuilder::MASK_EDIT, $mask & MaskBuilder::MASK_EDIT);
  52. $this->assertEquals(MaskBuilder::MASK_OWNER, $mask & MaskBuilder::MASK_OWNER);
  53. $this->assertEquals(0, $mask & MaskBuilder::MASK_MASTER);
  54. $this->assertEquals(0, $mask & MaskBuilder::MASK_CREATE);
  55. $this->assertEquals(0, $mask & MaskBuilder::MASK_DELETE);
  56. $this->assertEquals(0, $mask & MaskBuilder::MASK_UNDELETE);
  57. $builder->remove('edit')->remove('OWner');
  58. $mask = $builder->get();
  59. $this->assertEquals(0, $mask & MaskBuilder::MASK_EDIT);
  60. $this->assertEquals(0, $mask & MaskBuilder::MASK_OWNER);
  61. $this->assertEquals(MaskBuilder::MASK_VIEW, $mask & MaskBuilder::MASK_VIEW);
  62. }
  63. public function testGetPattern()
  64. {
  65. $builder = new MaskBuilder();
  66. $this->assertEquals(MaskBuilder::ALL_OFF, $builder->getPattern());
  67. $builder->add('view');
  68. $this->assertEquals(str_repeat('.', 31).'V', $builder->getPattern());
  69. $builder->add('owner');
  70. $this->assertEquals(str_repeat('.', 24).'N......V', $builder->getPattern());
  71. $builder->add(1 << 10);
  72. $this->assertEquals(str_repeat('.', 21).MaskBuilder::ON.'..N......V', $builder->getPattern());
  73. }
  74. public function testReset()
  75. {
  76. $builder = new MaskBuilder();
  77. $this->assertEquals(0, $builder->get());
  78. $builder->add('view');
  79. $this->assertTrue($builder->get() > 0);
  80. $builder->reset();
  81. $this->assertEquals(0, $builder->get());
  82. }
  83. }