TypeGuesserChainTest.php 2.6 KB

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