-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathValidatedValueTest.php
95 lines (78 loc) · 2.94 KB
/
ValidatedValueTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
use Bpost\BpostApiClient\Common\ValidatedValue;
use Bpost\BpostApiClient\Exception\BpostLogicException;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidPatternException;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
class ValidatedValueFake extends ValidatedValue
{
/**
* @throws BpostLogicException
*/
public function validate()
{
if ($this->getValue() === 'aze') {
throw new BpostLogicException('aze');
}
}
}
class ValidatedValueTest extends \PHPUnit_Framework_TestCase
{
public function testGetValue()
{
$fake = new ValidatedValueFake('qsd');
$this->assertSame('qsd', $fake->getValue());
$fake = new ValidatedValueFake('qsd');
$this->assertSame('qsd', (string)$fake);
$this->setExpectedException('Bpost\BpostApiClient\Exception\BpostLogicException');
new ValidatedValueFake('aze');
}
public function testValidateLength()
{
$fake = new ValidatedValueFake('qsd');
try {
$fake->validateLength(10);
$this->assertTrue(true);
} catch (BpostInvalidLengthException $e) {
$this->fail('Exception launched for valid value: "qsd" (tested with length=10)');
}
try {
$fake->validateLength(2);
$this->fail('Exception uncaught for invalid value: "qsd" (tested with length=2)');
} catch (BpostInvalidLengthException $e) {
$this->assertTrue(true);
}
}
public function testValidateChoice()
{
$fake = new ValidatedValueFake('qsd');
try {
$fake->validateChoice(array('aze', 'qsd'));
$this->assertTrue(true);
} catch (BpostInvalidValueException $e) {
$this->fail('Exception launched for valid value: "qsd" (tested with ["aze", "qsd"])');
}
try {
$fake->validateChoice(array('aze', 'wxc'));
$this->fail('Exception uncaught for invalid value: "qsd" (tested with ["aze", "wxc"])');
} catch (BpostInvalidValueException $e) {
$this->assertTrue(true);
}
}
public function testValidatePattern()
{
$fake = new ValidatedValueFake('[email protected]');
try {
$fake->validatePattern('([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+');
$this->assertTrue(true);
} catch (BpostInvalidPatternException $e) {
$this->fail('Exception launched for valid value: "[email protected]"');
}
try {
$fake->validatePattern('([a-zA-Z0-9_\.\-+])+(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+');
$this->fail('Exception uncaught for invalid value: "[email protected]"');
} catch (BpostInvalidPatternException $e) {
$this->assertTrue(true);
}
}
}