ModelToIdTransformerTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Form\DataTransformer;
  11. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
  12. class ModelToIdTransformerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $modelManager = null;
  15. public function setUp()
  16. {
  17. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  18. }
  19. public function testReverseTransformWhenPassing0AsId()
  20. {
  21. $transformer = new ModelToIdTransformer($this->modelManager,'TEST');
  22. $this->modelManager
  23. ->expects($this->exactly(2))
  24. ->method('find');
  25. //we pass 0 as integer
  26. // this must call the model manager find method... i not care what is returned, but must be called
  27. $transformer->reverseTransform(0);
  28. //we pass 0 as string
  29. //this must call the model manager find method... i not care what is returned, but must be called
  30. $transformer->reverseTransform('0');
  31. //we pass null must return null
  32. $this->assertNull($transformer->reverseTransform(null));
  33. //we pass false, must return null
  34. $this->assertNull($transformer->reverseTransform(false));
  35. }
  36. public function testTransform()
  37. {
  38. $this->modelManager->expects($this->once())
  39. ->method('getIdentifierValues')
  40. ->will($this->returnValue(array(123)));
  41. $transformer = new ModelToIdTransformer($this->modelManager,'TEST');
  42. $this->assertNull($transformer->transform(null));
  43. $this->assertNull($transformer->transform(false));
  44. $this->assertNull($transformer->transform(0));
  45. $this->assertNull($transformer->transform('0'));
  46. $this->assertEquals(123, $transformer->transform(new \stdClass));
  47. }
  48. }