|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/20/14 |
|
5
|
|
|
* Time: 2:47 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Tests\NilPortugues\Validator\Attribute\Object; |
|
12
|
|
|
|
|
13
|
|
|
use NilPortugues\Validator\Validator; |
|
14
|
|
|
use Tests\NilPortugues\Validator\Resources\Dummy; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ObjectAttributeTest |
|
18
|
|
|
* @package Tests\NilPortugues\Validator\Attribute\ObjectAttribute |
|
19
|
|
|
*/ |
|
20
|
|
|
class ObjectAttributeTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @return \NilPortugues\Validator\Attribute\Object\ObjectAttribute |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function getValidator() |
|
26
|
|
|
{ |
|
27
|
|
|
$validator = Validator::create(); |
|
28
|
|
|
|
|
29
|
|
|
return $validator->isObject('propertyName'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
*/ |
|
35
|
|
|
public function itShouldCheckIfIsObject() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->assertTrue($this->getValidator()->validate(new \stdClass())); |
|
38
|
|
|
$this->assertFalse($this->getValidator()->validate('a')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @test |
|
43
|
|
|
*/ |
|
44
|
|
|
public function itShouldCheckIfIsInstanceOf() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertTrue($this->getValidator()->isInstanceOf('DateTime')->validate(new \DateTime())); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertFalse($this->getValidator()->isInstanceOf('DateTime')->validate(new \stdClass())); |
|
49
|
|
|
$this->assertFalse($this->getValidator()->isInstanceOf('DateTime')->validate('a')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
*/ |
|
55
|
|
|
public function itShouldCheckIfHasProperty() |
|
56
|
|
|
{ |
|
57
|
|
|
$dummy = new Dummy(); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertTrue($this->getValidator()->hasProperty('userName')->validate($dummy)); |
|
60
|
|
|
$this->assertFalse($this->getValidator()->hasProperty('password')->validate($dummy)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function itShouldCheckIfHasMethod() |
|
67
|
|
|
{ |
|
68
|
|
|
$dummy = new Dummy(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue($this->getValidator()->hasMethod('getUserName')->validate($dummy)); |
|
71
|
|
|
$this->assertFalse($this->getValidator()->hasMethod('getPassword')->validate($dummy)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @test |
|
76
|
|
|
*/ |
|
77
|
|
|
public function itShouldCheckIfHasParentClass() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->assertTrue($this->getValidator()->hasParentClass()->validate(new Dummy())); |
|
80
|
|
|
$this->assertFalse($this->getValidator()->hasParentClass()->validate(new \stdClass())); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @test |
|
85
|
|
|
*/ |
|
86
|
|
|
public function itShouldCheckIfIsChildOf() |
|
87
|
|
|
{ |
|
88
|
|
|
$dummy = new Dummy(); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertTrue($this->getValidator()->isChildOf('DateTime')->validate($dummy)); |
|
91
|
|
|
$this->assertFalse($this->getValidator()->isChildOf('DateTimeZone')->validate($dummy)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
public function itShouldCheckIfInheritsFrom() |
|
98
|
|
|
{ |
|
99
|
|
|
$dummy = new Dummy(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($this->getValidator()->inheritsFrom('DateTime')->validate($dummy)); |
|
102
|
|
|
$this->assertFalse($this->getValidator()->inheritsFrom('DateTimeZone')->validate($dummy)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @test |
|
107
|
|
|
*/ |
|
108
|
|
|
public function itShouldCheckIfHasInterface() |
|
109
|
|
|
{ |
|
110
|
|
|
$dummy = new Dummy(); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertTrue( |
|
113
|
|
|
$this->getValidator() |
|
114
|
|
|
->hasInterface('Tests\NilPortugues\Validator\Resources\DummyInterface') |
|
115
|
|
|
->validate($dummy) |
|
116
|
|
|
); |
|
117
|
|
|
$this->assertFalse($this->getValidator()->inheritsFrom('DateTimeZone')->validate($dummy)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|