|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\TomPHP\HalClient\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
|
6
|
|
|
use TomPHP\HalClient\Exception\FieldNotFoundException; |
|
7
|
|
|
use TomPHP\HalClient\Exception\LinkNotFoundException; |
|
8
|
|
|
use TomPHP\HalClient\Exception\ResourceNotFoundException; |
|
9
|
|
|
use TomPHP\HalClient\ResourceFetcher; |
|
10
|
|
|
use TomPHP\HalClient\Resource\FieldNode; |
|
11
|
|
|
use TomPHP\HalClient\Resource\Link; |
|
12
|
|
|
use TomPHP\HalClient\Resource\ResourceNode; |
|
13
|
|
|
|
|
14
|
|
|
class ResourceSpec extends ObjectBehavior |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Link */ |
|
17
|
|
|
private $link; |
|
18
|
|
|
|
|
19
|
|
|
function let(ResourceFetcher $fetcher, FieldNode $f1, FieldNode $f2, ResourceNode $resource) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->link = new Link($fetcher->getWrappedObject(), 'href'); |
|
22
|
|
|
|
|
23
|
|
|
$this->beConstructedWith( |
|
24
|
|
|
[ |
|
25
|
|
|
'field1' => $f1->getWrappedObject(), |
|
26
|
|
|
'field2' => $f2->getWrappedObject(), |
|
27
|
|
|
], |
|
28
|
|
|
['link1' => $this->link], |
|
29
|
|
|
['resource1' => $resource->getWrappedObject()] |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
function it_returns_fields_by_name(FieldNode $f1) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->getField('field1')->shouldBeLike($f1); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function it_returns_fields_by_magic_method(FieldNode $f1) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->field1->shouldReturn($f1); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function it_throws_when_requesting_an_unknown_field() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->shouldThrow(new FieldNotFoundException('unknown')) |
|
46
|
|
|
->duringGetField('unknown'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_lists_links() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->getLinks()->shouldReturn(['link1']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function it_gets_link_by_name() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->getLink('link1')->shouldBeLike($this->link); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
function it_throws_when_requesting_an_unknown_link() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->shouldThrow(new LinkNotFoundException('unknown')) |
|
62
|
|
|
->duringGetLink('unknown'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function it_gets_resource_by_name(ResourceNode $resource) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->getResource('resource1')->shouldReturn($resource); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function it_throws_when_requesting_an_unknown_resource() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->shouldThrow(new ResourceNotFoundException('unknown')) |
|
73
|
|
|
->duringGetResource('unknown'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function it_does_not_match_if_critera_includes_unknown_field() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->matches(['unknown_field' => 'some-value'])->shouldReturn(false); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
function it_does_match_if_all_criteria_fields_match(FieldNode $f1, FieldNode $f2) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$f1->matches('search-value1')->willReturn(true); |
|
84
|
|
|
$f2->matches('search-value2')->willReturn(true); |
|
85
|
|
|
|
|
86
|
|
|
$this->matches([ |
|
87
|
|
|
'field1' => 'search-value1', |
|
88
|
|
|
'field2' => 'search-value2', |
|
89
|
|
|
])->shouldReturn(true); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
function it_does_not_match_if_any_criteria_fields_fail_to_match(FieldNode $f1, FieldNode $f2) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$f1->matches('search-value1')->willReturn(true); |
|
95
|
|
|
$f2->matches('search-value2')->willReturn(false); |
|
96
|
|
|
|
|
97
|
|
|
$this->matches([ |
|
98
|
|
|
'field1' => 'search-value1', |
|
99
|
|
|
'field2' => 'search-value2', |
|
100
|
|
|
])->shouldReturn(false); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
function it_does_not_match_unknown_resource() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->matches([ |
|
106
|
|
|
'field1' => 'search-value1', |
|
107
|
|
|
['resource', 'unknown', ['resource_field' => 'search-value2']], |
|
108
|
|
|
])->shouldReturn(false); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
function it_matches_if_resource_matches(FieldNode $f1, ResourceNode $resource) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$f1->matches('search-value1')->willReturn(true); |
|
114
|
|
|
$resource->matches(['resource_field' => 'search-value2'])->willReturn(true); |
|
115
|
|
|
|
|
116
|
|
|
$this->matches([ |
|
117
|
|
|
'field1' => 'search-value1', |
|
118
|
|
|
['resource', 'resource1', ['resource_field' => 'search-value2']], |
|
119
|
|
|
])->shouldReturn(true); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
function it_does_not_match_if_resource_does_not_match(FieldNode $f1, ResourceNode $resource) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$f1->matches('search-value1')->willReturn(true); |
|
125
|
|
|
$resource->matches(['resource_field' => 'search-value2'])->willReturn(false); |
|
126
|
|
|
|
|
127
|
|
|
$this->matches([ |
|
128
|
|
|
'field1' => 'search-value1', |
|
129
|
|
|
['resource', 'resource1', ['resource_field' => 'search-value2']], |
|
130
|
|
|
])->shouldReturn(false); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.