PropelCollectionHandlerTest.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace JMS\Serializer\Tests\Handler;
  3. use JMS\Serializer\SerializerBuilder;
  4. class PropelCollectionHandlerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /** @var $serializer \JMS\Serializer\Serializer */
  7. private $serializer;
  8. public function setUp()
  9. {
  10. $this->serializer = SerializerBuilder::create()
  11. ->addDefaultHandlers() //load PropelCollectionHandler
  12. ->build();
  13. }
  14. public function testSerializePropelObjectCollection()
  15. {
  16. $collection = new \PropelObjectCollection();
  17. $collection->setData(array(new TestSubject('lolo'), new TestSubject('pepe')));
  18. $json = $this->serializer->serialize($collection, 'json');
  19. $data = json_decode($json, true);
  20. $this->assertCount(2, $data); //will fail if PropelCollectionHandler not loaded
  21. foreach ($data as $testSubject) {
  22. $this->assertArrayHasKey('name', $testSubject);
  23. }
  24. }
  25. }
  26. class TestSubject
  27. {
  28. protected $name;
  29. public function __construct($name)
  30. {
  31. $this->name = $name;
  32. }
  33. }