|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ValueParsers\Test; |
| 4 | + |
| 5 | +use DataValues\DataValue; |
| 6 | +use DataValues\StringValue; |
| 7 | +use ValueParsers\Normalizers\StringNormalizer; |
| 8 | +use ValueParsers\StringParser; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers ValueParsers\StringParser |
| 12 | + * |
| 13 | + * @group ValueParsers |
| 14 | + * @group DataValueExtensions |
| 15 | + * |
| 16 | + * @licence GNU GPL v2+ |
| 17 | + * @author Daniel Kinzler |
| 18 | + */ |
| 19 | +class StringParserTest extends \PHPUnit_Framework_TestCase { |
| 20 | + |
| 21 | + public function provideParse() { |
| 22 | + $normalizer = $this->getMock( 'ValueParsers\Normalizers\StringNormalizer' ); |
| 23 | + $normalizer->expects( $this->once() ) |
| 24 | + ->method( 'normalize' ) |
| 25 | + ->will( $this->returnCallback( function( $value ) { |
| 26 | + return strtolower( trim( $value ) ); |
| 27 | + } ) ); |
| 28 | + |
| 29 | + return array( |
| 30 | + 'simple' => array( 'hello world', null, new StringValue( 'hello world' ) ), |
| 31 | + 'normalize' => array( ' Hello World ', $normalizer, new StringValue( 'hello world' ) ), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider provideParse |
| 37 | + */ |
| 38 | + public function testParse( $input, StringNormalizer $normalizer = null, DataValue $expected ) { |
| 39 | + $parser = new StringParser( $normalizer ); |
| 40 | + $value = $parser->parse( $input ); |
| 41 | + |
| 42 | + $this->assertInstanceOf( 'DataValues\StringValue', $value ); |
| 43 | + $this->assertEquals( $expected->toArray(), $value->toArray() ); |
| 44 | + } |
| 45 | + |
| 46 | + public function nonStringProvider() { |
| 47 | + return array( |
| 48 | + 'null' => array( null ), |
| 49 | + 'array' => array( array() ), |
| 50 | + 'int' => array( 7 ), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider nonStringProvider |
| 56 | + */ |
| 57 | + public function testGivenNonString_parseThrowsException( $input ) { |
| 58 | + $parser = new StringParser(); |
| 59 | + $this->setExpectedException( 'InvalidArgumentException' ); |
| 60 | + $parser->parse( $input ); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments