ChainDecoderTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Encoder;
  11. use Symfony\Component\Serializer\Encoder\ChainDecoder;
  12. class ChainDecoderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. const FORMAT_1 = 'format1';
  15. const FORMAT_2 = 'format2';
  16. const FORMAT_3 = 'format3';
  17. private $chainDecoder;
  18. private $decoder1;
  19. private $decoder2;
  20. protected function setUp()
  21. {
  22. $this->decoder1 = $this
  23. ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
  24. ->getMock();
  25. $this->decoder1
  26. ->method('supportsDecoding')
  27. ->will($this->returnValueMap(array(
  28. array(self::FORMAT_1, true),
  29. array(self::FORMAT_2, false),
  30. array(self::FORMAT_3, false),
  31. )));
  32. $this->decoder2 = $this
  33. ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
  34. ->getMock();
  35. $this->decoder2
  36. ->method('supportsDecoding')
  37. ->will($this->returnValueMap(array(
  38. array(self::FORMAT_1, false),
  39. array(self::FORMAT_2, true),
  40. array(self::FORMAT_3, false),
  41. )));
  42. $this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
  43. }
  44. public function testSupportsDecoding()
  45. {
  46. $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
  47. $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
  48. $this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
  49. }
  50. public function testDecode()
  51. {
  52. $this->decoder1->expects($this->never())->method('decode');
  53. $this->decoder2->expects($this->once())->method('decode');
  54. $this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
  55. }
  56. /**
  57. * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
  58. */
  59. public function testDecodeUnsupportedFormat()
  60. {
  61. $this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
  62. }
  63. }