ArrayDenormalizerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Serializer\Tests\Normalizer;
  11. use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. class ArrayDenormalizerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var ArrayDenormalizer
  17. */
  18. private $denormalizer;
  19. /**
  20. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $serializer;
  23. protected function setUp()
  24. {
  25. $this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock();
  26. $this->denormalizer = new ArrayDenormalizer();
  27. $this->denormalizer->setSerializer($this->serializer);
  28. }
  29. public function testDenormalize()
  30. {
  31. $this->serializer->expects($this->at(0))
  32. ->method('denormalize')
  33. ->with(array('foo' => 'one', 'bar' => 'two'))
  34. ->will($this->returnValue(new ArrayDummy('one', 'two')));
  35. $this->serializer->expects($this->at(1))
  36. ->method('denormalize')
  37. ->with(array('foo' => 'three', 'bar' => 'four'))
  38. ->will($this->returnValue(new ArrayDummy('three', 'four')));
  39. $result = $this->denormalizer->denormalize(
  40. array(
  41. array('foo' => 'one', 'bar' => 'two'),
  42. array('foo' => 'three', 'bar' => 'four'),
  43. ),
  44. __NAMESPACE__.'\ArrayDummy[]'
  45. );
  46. $this->assertEquals(
  47. array(
  48. new ArrayDummy('one', 'two'),
  49. new ArrayDummy('three', 'four'),
  50. ),
  51. $result
  52. );
  53. }
  54. public function testSupportsValidArray()
  55. {
  56. $this->serializer->expects($this->once())
  57. ->method('supportsDenormalization')
  58. ->with($this->anything(), __NAMESPACE__.'\ArrayDummy', $this->anything())
  59. ->will($this->returnValue(true));
  60. $this->assertTrue(
  61. $this->denormalizer->supportsDenormalization(
  62. array(
  63. array('foo' => 'one', 'bar' => 'two'),
  64. array('foo' => 'three', 'bar' => 'four'),
  65. ),
  66. __NAMESPACE__.'\ArrayDummy[]'
  67. )
  68. );
  69. }
  70. public function testSupportsInvalidArray()
  71. {
  72. $this->serializer->expects($this->any())
  73. ->method('supportsDenormalization')
  74. ->will($this->returnValue(false));
  75. $this->assertFalse(
  76. $this->denormalizer->supportsDenormalization(
  77. array(
  78. array('foo' => 'one', 'bar' => 'two'),
  79. array('foo' => 'three', 'bar' => 'four'),
  80. ),
  81. __NAMESPACE__.'\InvalidClass[]'
  82. )
  83. );
  84. }
  85. public function testSupportsNoArray()
  86. {
  87. $this->assertFalse(
  88. $this->denormalizer->supportsDenormalization(
  89. array('foo' => 'one', 'bar' => 'two'),
  90. __NAMESPACE__.'\ArrayDummy'
  91. )
  92. );
  93. }
  94. }
  95. class ArrayDummy
  96. {
  97. public $foo;
  98. public $bar;
  99. public function __construct($foo, $bar)
  100. {
  101. $this->foo = $foo;
  102. $this->bar = $bar;
  103. }
  104. }