1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace ValueParsers\Tests; |
6
|
|
|
|
7
|
|
|
use DataValues\DataValue; |
8
|
|
|
use DataValues\StringValue; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ValueParsers\Normalizers\StringNormalizer; |
11
|
|
|
use ValueParsers\ParseException; |
12
|
|
|
use ValueParsers\StringParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \ValueParsers\StringParser |
16
|
|
|
* |
17
|
|
|
* @group ValueParsers |
18
|
|
|
* @group DataValueExtensions |
19
|
|
|
* |
20
|
|
|
* @license GPL-2.0-or-later |
21
|
|
|
* @author Daniel Kinzler |
22
|
|
|
*/ |
23
|
|
|
class StringParserTest extends TestCase { |
24
|
|
|
|
25
|
|
|
public function provideParse() { |
26
|
|
|
$normalizer = $this->createMock( StringNormalizer::class ); |
27
|
|
|
$normalizer->expects( $this->once() ) |
28
|
|
|
->method( 'normalize' ) |
29
|
|
|
->will( $this->returnCallback( function( $value ) { |
30
|
|
|
return strtolower( trim( $value ) ); |
31
|
|
|
} ) ); |
32
|
|
|
|
33
|
|
|
return [ |
34
|
|
|
'simple' => [ 'hello world', null, new StringValue( 'hello world' ) ], |
35
|
|
|
'normalize' => [ ' Hello World ', $normalizer, new StringValue( 'hello world' ) ], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider provideParse |
41
|
|
|
*/ |
42
|
|
|
public function testParse( $input, ?StringNormalizer $normalizer, DataValue $expected ) { |
43
|
|
|
$parser = new StringParser( $normalizer ); |
44
|
|
|
$value = $parser->parse( $input ); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf( StringValue::class, $value ); |
47
|
|
|
$this->assertEquals( $expected->toArray(), $value->toArray() ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function nonStringProvider() { |
51
|
|
|
return [ |
52
|
|
|
'null' => [ null ], |
53
|
|
|
'array' => [ [] ], |
54
|
|
|
'int' => [ 7 ], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @dataProvider nonStringProvider |
60
|
|
|
*/ |
61
|
|
|
public function testGivenNonString_parseThrowsException( $input ) { |
62
|
|
|
$parser = new StringParser(); |
63
|
|
|
|
64
|
|
|
$this->expectException( ParseException::class ); |
65
|
|
|
|
66
|
|
|
$parser->parse( $input ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|