TypeGuesserChainTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. */
  11. namespace Sonata\AdminBundle\Tests\Guesser;
  12. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  13. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Form\Guess\Guess;
  15. use Sonata\AdminBundle\Model\ModelManagerInterface;
  16. use Sonata\AdminBundle\Guesser\TypeGuesserChain;
  17. use Symfony\Component\Form\Guess\TypeGuess;
  18. /**
  19. * TypeGuesserChain Test
  20. *
  21. * @author Andrej Hudec <pulzarraider@gmail.com>
  22. */
  23. class TypeGuesserChainTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testConstructorWithException()
  26. {
  27. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  28. $typeGuesserChain = new TypeGuesserChain(array(new \stdClass()));
  29. }
  30. public function testGuessType()
  31. {
  32. $typeGuess1 = new TypeGuess('foo1', array(), Guess::MEDIUM_CONFIDENCE);
  33. $guesser1 = $this->getMock('Sonata\AdminBundle\Guesser\TypeGuesserInterface');
  34. $guesser1->expects($this->any())
  35. ->method('guessType')
  36. ->will($this->returnValue($typeGuess1));
  37. $typeGuess2 = new TypeGuess('foo2', array(), Guess::HIGH_CONFIDENCE);
  38. $guesser2 = $this->getMock('Sonata\AdminBundle\Guesser\TypeGuesserInterface');
  39. $guesser2->expects($this->any())
  40. ->method('guessType')
  41. ->will($this->returnValue($typeGuess2));
  42. $typeGuess3 = new TypeGuess('foo3', array(), Guess::LOW_CONFIDENCE);
  43. $guesser3 = $this->getMock('Sonata\AdminBundle\Guesser\TypeGuesserInterface');
  44. $guesser3->expects($this->any())
  45. ->method('guessType')
  46. ->will($this->returnValue($typeGuess3));
  47. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  48. $class = '\stdClass';
  49. $property = 'firstName';
  50. $typeGuesserChain = new TypeGuesserChain(array($guesser1, $guesser2, $guesser3));
  51. $this->assertEquals($typeGuess2, $typeGuesserChain->guessType($class, $property, $modelManager));
  52. $typeGuess4 = new TypeGuess('foo4', array(), Guess::LOW_CONFIDENCE);
  53. $guesser4 = $this->getMock('Sonata\AdminBundle\Guesser\TypeGuesserInterface');
  54. $guesser4->expects($this->any())
  55. ->method('guessType')
  56. ->will($this->returnValue($typeGuess4));
  57. $typeGuesserChain = new TypeGuesserChain(array($guesser4, $typeGuesserChain));
  58. $this->assertEquals($typeGuess2, $typeGuesserChain->guessType($class, $property, $modelManager));
  59. }
  60. }