BasicAttributeTest::testSetKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Common;
4
5
use Bpost\BpostApiClient\Common\BasicAttribute;
6
use Bpost\BpostApiClient\Exception\BpostLogicException;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
8
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidPatternException;
9
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
10
use PHPUnit_Framework_TestCase;
11
12
class BasicAttributeFake extends BasicAttribute
13
{
14
    /**
15
     * @return string
16
     */
17
    protected function getDefaultKey()
18
    {
19
        return 'defaultKey';
20
    }
21
22
    /**
23
     * @throws BpostLogicException
24
     */
25
    public function validate()
26
    {
27
        if ($this->getValue() === 'aze') {
28
            throw new BpostLogicException('aze');
29
        }
30
    }
31
}
32
33
class BasicAttributeTest extends PHPUnit_Framework_TestCase
34
{
35
    public function testSetKey()
36
    {
37
        $fake = new BasicAttributeFake('qsd');
38
        $this->assertSame('defaultKey', $fake->getKey());
39
40
        $fake = new BasicAttributeFake('qsd', 'myKey');
41
        $this->assertSame('myKey', $fake->getKey());
42
    }
43
44
    /**
45
     * @expectedException \Bpost\BpostApiClient\Exception\BpostLogicException
46
     */
47
    public function testGetValue()
48
    {
49
        $fake = new BasicAttributeFake('qsd');
50
        $this->assertSame('qsd', $fake->getValue());
51
52
        $fake = new BasicAttributeFake('qsd');
53
        $this->assertSame('qsd', (string) $fake);
54
55
        new BasicAttributeFake('aze');
56
    }
57
58
    public function testValidateLength()
59
    {
60
        $fake = new BasicAttributeFake('qsd');
61
        try {
62
            $fake->validateLength(10);
63
            $this->assertTrue(true);
64
        } catch (BpostInvalidLengthException $e) {
65
            $this->fail('Exception launched for valid value: "qsd" (tested with length=10)');
66
        }
67
68
        try {
69
            $fake->validateLength(2);
70
            $this->fail('Exception uncaught for invalid value: "qsd" (tested with length=2)');
71
        } catch (BpostInvalidLengthException $e) {
72
            $this->assertTrue(true);
73
        }
74
    }
75
76
    public function testValidateChoice()
77
    {
78
        $fake = new BasicAttributeFake('qsd');
79
        try {
80
            $fake->validateChoice(array('aze', 'qsd'));
81
            $this->assertTrue(true);
82
        } catch (BpostInvalidValueException $e) {
83
            $this->fail('Exception launched for valid value: "qsd" (tested with ["aze", "qsd"])');
84
        }
85
86
        try {
87
            $fake->validateChoice(array('aze', 'wxc'));
88
            $this->fail('Exception uncaught for invalid value: "qsd" (tested with ["aze", "wxc"])');
89
        } catch (BpostInvalidValueException $e) {
90
            $this->assertTrue(true);
91
        }
92
    }
93
94
    public function testValidatePattern()
95
    {
96
        $fake = new BasicAttributeFake('[email protected]');
97
        try {
98
            $fake->validatePattern('([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+');
99
            $this->assertTrue(true);
100
        } catch (BpostInvalidPatternException $e) {
101
            $this->fail('Exception launched for valid value: "[email protected]"');
102
        }
103
104
        try {
105
            $fake->validatePattern('([a-zA-Z0-9_\.\-+])+(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+');
106
            $this->fail('Exception uncaught for invalid value: "[email protected]"');
107
        } catch (BpostInvalidPatternException $e) {
108
            $this->assertTrue(true);
109
        }
110
    }
111
}
112