Skip to content

Commit 336e128

Browse files
committed
Add StringParserTest
1 parent 6024e94 commit 336e128

File tree

6 files changed

+78
-25
lines changed

6 files changed

+78
-25
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ DataValues Common has been written by the Wikidata team, as [Wikimedia Germany]
5252
### 0.3.0 (2015-08-11)
5353

5454
* Added `DispatchingValueParser`
55+
* Added `StringNormalizer` interface
56+
* Added `NullStringNormalizer`
57+
* Added `StringParser`
5558
* Dropped deprecated constant `DataValuesCommon_VERSION`, use `DATAVALUES_COMMON_VERSION` instead
5659
* Dropped `ValueParserTestBase::getParserClass`
5760
* Dropped `ValueParserTestBase::newParserOptions`

src/ValueParsers/Normalizers/NullStringNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
/**
88
* Null implementation of StringNormalizer.
99
*
10+
* @since 0.3
11+
*
1012
* @license GPL 2+
1113
* @author Daniel Kinzler
1214
*/
1315
class NullStringNormalizer implements StringNormalizer {
1416

1517
/**
16-
* @param string $value the value to normalize
18+
* @param string $value
1719
*
20+
* @throws InvalidArgumentException if $value is not a string
1821
* @return string the normalized value
1922
*/
2023
public function normalize( $value ) {

src/ValueParsers/Normalizers/StringNormalizer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
use InvalidArgumentException;
66

77
/**
8-
* Interface for string normalization
8+
* Interface for string normalization.
9+
*
10+
* @since 0.3
911
*
1012
* @license GPL 2+
1113
* @author Daniel Kinzler
1214
*/
1315
interface StringNormalizer {
1416

1517
/**
16-
* @param string $value the value to normalize
17-
*
18-
* @return string the normalized value
18+
* @param string $value
1919
*
2020
* @throws InvalidArgumentException if $value is not a string
21+
* @return string the normalized value
2122
*/
2223
public function normalize( $value );
2324

src/ValueParsers/StringParser.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace ValueParsers;
44

5-
use InvalidArgumentException;
65
use DataValues\StringValue;
6+
use InvalidArgumentException;
77
use ValueParsers\Normalizers\NullStringNormalizer;
88
use ValueParsers\Normalizers\StringNormalizer;
99

1010
/**
1111
* Implementation of the ValueParser interface for StringValues.
1212
*
13-
* @since 0.2.4
13+
* @since 0.3
1414
*
1515
* @licence GNU GPL v2+
1616
* @author Daniel Kinzler
@@ -26,21 +26,16 @@ class StringParser implements ValueParser {
2626
* @param StringNormalizer $normalizer
2727
*/
2828
public function __construct( StringNormalizer $normalizer = null ) {
29-
if ( $normalizer === null ) {
30-
$normalizer = new NullStringNormalizer();
31-
}
32-
33-
$this->normalizer = $normalizer;
29+
$this->normalizer = $normalizer ?: new NullStringNormalizer();
3430
}
3531

3632
/**
3733
* @see ValueParser::parse
3834
*
3935
* @param string $value
4036
*
41-
* @return StringValue
42-
*
4337
* @throws InvalidArgumentException if $value is not a string
38+
* @return StringValue
4439
*/
4540
public function parse( $value ) {
4641
if ( !is_string( $value ) ) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers\Test;
4+
5+
use DataValues\DataValue;
6+
use DataValues\StringValue;
7+
use PHPUnit_Framework_TestCase;
8+
use ValueParsers\Normalizers\NullStringNormalizer;
9+
10+
/**
11+
* @covers ValueParsers\Normalizers\NullStringNormalizer
12+
*
13+
* @group ValueParsers
14+
* @group DataValueExtensions
15+
*
16+
* @licence GNU GPL v2+
17+
* @author Thiemo Mättig
18+
*/
19+
class NullStringNormalizerTest extends PHPUnit_Framework_TestCase {
20+
21+
/**
22+
* @dataProvider stringProvider
23+
*/
24+
public function testNormalize( $value ) {
25+
$normalizer = new NullStringNormalizer();
26+
$this->assertSame( $value, $normalizer->normalize( $value ) );
27+
}
28+
29+
public function stringProvider() {
30+
return array(
31+
array( '' ),
32+
array( 'a' ),
33+
array( ' a ' ),
34+
);
35+
}
36+
37+
/**
38+
* @dataProvider invalidValueProvider
39+
*/
40+
public function testNormalizeException( $value ) {
41+
$normalizer = new NullStringNormalizer();
42+
$this->setExpectedException( 'InvalidArgumentException' );
43+
$normalizer->normalize( $value );
44+
}
45+
46+
public function invalidValueProvider() {
47+
return array(
48+
array( null ),
49+
array( true ),
50+
array( 1 ),
51+
array( new StringValue( '' ) ),
52+
);
53+
}
54+
55+
}

tests/ValueParsers/StringParserTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
use DataValues\DataValue;
66
use DataValues\StringValue;
7+
use ValueParsers\Normalizers\StringNormalizer;
78
use ValueParsers\StringParser;
89

910
/**
10-
* Unit test StringParser class.
11-
*
12-
* @covers StringParser
13-
*
14-
* @since 0.1
11+
* @covers ValueParsers\StringParser
1512
*
1613
* @group ValueParsers
1714
* @group DataValueExtensions
@@ -38,15 +35,15 @@ public function provideParse() {
3835
/**
3936
* @dataProvider provideParse
4037
*/
41-
public function testParse( $input, $normalizer, DataValue $expected ) {
38+
public function testParse( $input, StringNormalizer $normalizer = null, DataValue $expected ) {
4239
$parser = new StringParser( $normalizer );
4340
$value = $parser->parse( $input );
4441

4542
$this->assertInstanceOf( 'DataValues\StringValue', $value );
4643
$this->assertEquals( $expected->toArray(), $value->toArray() );
4744
}
4845

49-
public function provideParse_InvalidArgumentException() {
46+
public function nonStringProvider() {
5047
return array(
5148
'null' => array( null ),
5249
'array' => array( array() ),
@@ -55,12 +52,11 @@ public function provideParse_InvalidArgumentException() {
5552
}
5653

5754
/**
58-
* @dataProvider provideParse_InvalidArgumentException
55+
* @dataProvider nonStringProvider
5956
*/
60-
public function testParse_InvalidArgumentException( $input ) {
61-
$this->setExpectedException( 'InvalidArgumentException' );
62-
57+
public function testGivenNonString_parseThrowsException( $input ) {
6358
$parser = new StringParser();
59+
$this->setExpectedException( 'InvalidArgumentException' );
6460
$parser->parse( $input );
6561
}
6662

0 commit comments

Comments
 (0)