Skip to content

Commit bff9579

Browse files
committed
[Feature] Add assertion that document does not have links
1 parent d1e91d4 commit bff9579

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. This projec
1212
- New `assertDoesntHaveIncluded` assertion to assert that the JSON:API document does not have the top-level `included`
1313
member.
1414
- New `assertDoesntHaveMeta` assertion to assert the JSON:API document does not have the top-level `meta` member.
15+
- New `assertDoesntHaveLinks` assertion to assert the JSON:API document does not have the top-level `links` member.
1516

1617
### Fixed
1718

src/Concerns/HasHttpAssertions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,18 @@ public function assertExactLinks(array $expected, bool $strict = true): self
623623
return $this;
624624
}
625625

626+
/**
627+
* Assert that the document does not have the top-level links member.
628+
*
629+
* @return $this
630+
*/
631+
public function assertDoesntHaveLinks(): self
632+
{
633+
$this->getDocument()->assertNotExists('links', 'Document has top-level links.');
634+
635+
return $this;
636+
}
637+
626638
/**
627639
* Assert the document contains a single error that matches the supplied error.
628640
*

tests/Assertions/LinksTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/*
3+
* Copyright 2022 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace CloudCreativity\JsonApi\Testing\Tests\Assertions;
21+
22+
use CloudCreativity\JsonApi\Testing\HttpMessage;
23+
use CloudCreativity\JsonApi\Testing\Tests\TestCase;
24+
25+
class LinksTest extends TestCase
26+
{
27+
/**
28+
* @var array
29+
*/
30+
private array $links = [
31+
'self' => '/api/v1/comments/123/relationships/post',
32+
'related' => '/api/v1/comments/123/post',
33+
];
34+
35+
/**
36+
* @var array
37+
*/
38+
private array $post = [
39+
'type' => 'posts',
40+
'id' => '1',
41+
'attributes' => [
42+
'title' => 'Hello World',
43+
'content' => '...',
44+
],
45+
];
46+
47+
/**
48+
* @var HttpMessage
49+
*/
50+
private HttpMessage $http;
51+
52+
/**
53+
* @return void
54+
*/
55+
protected function setUp(): void
56+
{
57+
parent::setUp();
58+
59+
$this->http = new HttpMessage(
60+
200,
61+
'application/vnd.api+json',
62+
json_encode(['data' => $this->post, 'links' => $this->links]),
63+
['Content-Type' => 'application/vnd.api+json', 'Accept' => 'application/vnd.api+json'],
64+
);
65+
}
66+
67+
public function testLinks(): void
68+
{
69+
$partial = $this->links;
70+
unset($partial['self']);
71+
72+
$invalid = $this->links;
73+
$invalid['related'] = '/api/foo/bar';
74+
75+
$this->http->assertLinks($this->links);
76+
$this->http->assertLinks($partial);
77+
78+
$this->assertThatItFails(
79+
'member at [/links] matches the subset',
80+
fn() => $this->http->assertLinks($invalid)
81+
);
82+
}
83+
84+
public function testExactLinks(): void
85+
{
86+
$partial = $this->links;
87+
unset($partial['self']);
88+
89+
$invalid = $this->links;
90+
$invalid['related'] = '/api/foo/bar';
91+
92+
$this->http->assertExactLinks($this->links);
93+
94+
$this->assertThatItFails(
95+
'member at [/links] exactly matches',
96+
fn() => $this->http->assertExactLinks($partial),
97+
);
98+
99+
$this->assertThatItFails(
100+
'member at [/links] exactly matches',
101+
fn() => $this->http->assertExactLinks($invalid),
102+
);
103+
}
104+
105+
public function testDoesntHaveLinks(): void
106+
{
107+
$none = $this->http->withContent(json_encode(['data' => $this->post]));
108+
$none->assertDoesntHaveLinks();
109+
110+
$this->assertThatItFails(
111+
'Document has top-level links.',
112+
fn() => $this->http->assertDoesntHaveLinks()
113+
);
114+
}
115+
}

0 commit comments

Comments
 (0)