TypeParserTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace JMS\SerializerBundle\Tests\Serializer;
  3. use JMS\SerializerBundle\Serializer\TypeParser;
  4. class TypeParserTest extends \PHPUnit_Framework_TestCase
  5. {
  6. private $parser;
  7. /**
  8. * @dataProvider getTypes
  9. */
  10. public function testParse($type, $name, array $params = array())
  11. {
  12. $this->assertEquals(array('name' => $name, 'params' => $params), $this->parser->parse($type));
  13. }
  14. public function getTypes()
  15. {
  16. $types = array();
  17. $types[] = array('string', 'string');
  18. $types[] = array('array<Foo>', 'array', array(array('name' => 'Foo', 'params' => array())));
  19. $types[] = array('array<Foo,Bar>', 'array', array(array('name' => 'Foo', 'params' => array()), array('name' => 'Bar', 'params' => array())));
  20. $types[] = array('array<Foo\Bar, Baz\Boo>', 'array', array(array('name' => 'Foo\Bar', 'params' => array()), array('name' => 'Baz\Boo', 'params' => array())));
  21. $types[] = array('a<b<c,d>,e>', 'a', array(array('name' => 'b', 'params' => array(array('name' => 'c', 'params' => array()), array('name' => 'd', 'params' => array()))), array('name' => 'e', 'params' => array())));
  22. $types[] = array('Foo', 'Foo');
  23. $types[] = array('Foo\Bar', 'Foo\Bar');
  24. $types[] = array('Foo<"asdf asdf">', 'Foo', array('asdf asdf'));
  25. return $types;
  26. }
  27. /**
  28. * @expectedException \InvalidArgumentException
  29. * @expectedExceptionMessage Expected token T_CLOSE_BRACKET, but reached end of type.
  30. */
  31. public function testParamTypeMustEndWithBracket()
  32. {
  33. $this->parser->parse('Foo<bar');
  34. }
  35. /**
  36. * @expectedException \InvalidArgumentException
  37. * @expectedExceptionMessage Expected token T_NAME, but got T_COMMA at position 0.
  38. */
  39. public function testMustStartWithName()
  40. {
  41. $this->parser->parse(',');
  42. }
  43. /**
  44. * @expectedException \InvalidArgumentException
  45. * @expectedExceptionMessage Expected any of T_NAME or T_STRING, but got T_CLOSE_BRACKET at position 4.
  46. */
  47. public function testEmptyParams()
  48. {
  49. $this->parser->parse('Foo<>');
  50. }
  51. /**
  52. * @expectedException \InvalidArgumentException
  53. * @expectedExceptionMessage Expected any of T_NAME or T_STRING, but got T_CLOSE_BRACKET at position 7.
  54. */
  55. public function testNoTrailingComma()
  56. {
  57. $this->parser->parse('Foo<aa,>');
  58. }
  59. protected function setUp()
  60. {
  61. $this->parser = new TypeParser();
  62. }
  63. }