GroupSequenceTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Validator\Tests\Constraints;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraints\GroupSequence;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class GroupSequenceTest extends TestCase
  17. {
  18. public function testCreate()
  19. {
  20. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  21. $this->assertSame(array('Group 1', 'Group 2'), $sequence->groups);
  22. }
  23. public function testCreateDoctrineStyle()
  24. {
  25. $sequence = new GroupSequence(array('value' => array('Group 1', 'Group 2')));
  26. $this->assertSame(array('Group 1', 'Group 2'), $sequence->groups);
  27. }
  28. /**
  29. * @group legacy
  30. */
  31. public function testLegacyIterate()
  32. {
  33. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  34. $this->assertSame(array('Group 1', 'Group 2'), iterator_to_array($sequence));
  35. }
  36. /**
  37. * @group legacy
  38. */
  39. public function testLegacyCount()
  40. {
  41. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  42. $this->assertCount(2, $sequence);
  43. }
  44. /**
  45. * @group legacy
  46. */
  47. public function testLegacyArrayAccess()
  48. {
  49. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  50. $this->assertSame('Group 1', $sequence[0]);
  51. $this->assertSame('Group 2', $sequence[1]);
  52. $this->assertArrayHasKey(0, $sequence);
  53. $this->assertArrayNotHasKey(2, $sequence);
  54. unset($sequence[0]);
  55. $this->assertArrayNotHasKey(0, $sequence);
  56. $sequence[] = 'Group 3';
  57. $this->assertArrayHasKey(2, $sequence);
  58. $this->assertSame('Group 3', $sequence[2]);
  59. $sequence[0] = 'Group 1';
  60. $this->assertArrayHasKey(0, $sequence);
  61. $this->assertSame('Group 1', $sequence[0]);
  62. }
  63. /**
  64. * @expectedException \Symfony\Component\Validator\Exception\OutOfBoundsException
  65. * @group legacy
  66. */
  67. public function testLegacyGetExpectsExistingKey()
  68. {
  69. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  70. $sequence[2];
  71. }
  72. /**
  73. * @group legacy
  74. */
  75. public function testLegacyUnsetIgnoresNonExistingKeys()
  76. {
  77. $sequence = new GroupSequence(array('Group 1', 'Group 2'));
  78. // should not fail
  79. unset($sequence[2]);
  80. $this->assertCount(2, $sequence);
  81. }
  82. }