ArrayValueConverterMapTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\ValueConverter;
  3. use Ddeboer\DataImport\ValueConverter\ArrayValueConverterMap;
  4. use Ddeboer\DataImport\ValueConverter\CallbackValueConverter;
  5. /**
  6. * @author Christoph Rosse <christoph@rosse.at>
  7. */
  8. class ArrayValueConverterMapTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @expectedException InvalidArgumentException
  12. */
  13. public function testConvertWithNoArrayArgument()
  14. {
  15. $converter = new ArrayValueConverterMap(array('foo' => function($input) {return $input;}));
  16. call_user_func($converter, 'foo');
  17. }
  18. public function testConvertWithMultipleFields()
  19. {
  20. $data = array(
  21. array(
  22. 'foo' => 'test',
  23. 'bar' => 'test'
  24. ),
  25. array(
  26. 'foo' => 'test2',
  27. 'bar' => 'test2'
  28. ),
  29. );
  30. $addBarConverter = function($input) { return 'bar'.$input; };
  31. $addBazConverter = function($input) { return 'baz'.$input; };
  32. $converter = new ArrayValueConverterMap(
  33. array(
  34. 'foo' => array($addBarConverter),
  35. 'bar' => array($addBazConverter, $addBarConverter),
  36. )
  37. );
  38. $data = call_user_func($converter, $data);
  39. $this->assertEquals('bartest', $data[0]['foo']);
  40. $this->assertEquals('barbaztest', $data[0]['bar']);
  41. $this->assertEquals('bartest2', $data[1]['foo']);
  42. $this->assertEquals('barbaztest2', $data[1]['bar']);
  43. }
  44. }