TypeGuesserChainTest.php 2.7 KB

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