ObjectConverterTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\ValueConverter;
  3. use Ddeboer\DataImport\ValueConverter\ObjectConverter;
  4. /**
  5. * @author Markus Bachmann <markus.bachmann@bachi.biz
  6. */
  7. class ObjectConverterTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testGetAndSetPropertyPath()
  10. {
  11. $converter = new ObjectConverter();
  12. $this->assertNull($converter->getPropertyPath());
  13. $converter->setPropertyPath('foo.bar');
  14. $this->assertEquals('foo.bar', $converter->getPropertyPath());
  15. }
  16. public function testConvertWithToString()
  17. {
  18. $converter = new ObjectConverter();
  19. $object = new ToStringDummy();
  20. $this->assertEquals('foo', call_user_func($converter, $object));
  21. }
  22. public function testConvertWithPropertyPath()
  23. {
  24. $converter = new ObjectConverter('foo');
  25. $object = new Dummy();
  26. $this->assertEquals('bar', call_user_func($converter, $object));
  27. }
  28. /**
  29. * @expectedException RuntimeException
  30. */
  31. public function testConvertAObjectWithoutToString()
  32. {
  33. $converter = new ObjectConverter;
  34. call_user_func($converter, new Dummy());
  35. }
  36. /**
  37. * @expectedException Ddeboer\DataImport\Exception\UnexpectedTypeException
  38. */
  39. public function testConvetANonObject()
  40. {
  41. $converter = new ObjectConverter();
  42. call_user_func($converter, 'foo');
  43. }
  44. }
  45. class Dummy
  46. {
  47. public $foo = 'bar';
  48. }
  49. class ToStringDummy
  50. {
  51. public function __toString()
  52. {
  53. return 'foo';
  54. }
  55. }