Skip to content

Commit 705a1a6

Browse files
committed
Embrace strict(er) types, ensure that #13 is covered by Selector
1 parent fdf46f3 commit 705a1a6

File tree

10 files changed

+144
-93
lines changed

10 files changed

+144
-93
lines changed

src/Constraints/SelectorCount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function toString(): string
4949
* Evaluates the constraint for parameter $other. Returns true if the
5050
* constraint is met, false otherwise.
5151
*
52-
* @param mixed $other value or object to evaluate
52+
* @param mixed $html value or object to evaluate
5353
*
5454
* @return bool
5555
*/

src/MarkupAssertionsTrait.php

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ trait MarkupAssertionsTrait
2222
*
2323
* @since 1.0.0
2424
*
25-
* @param string $selector A query selector for the element to find.
26-
* @param string $markup The output that should contain the $selector.
27-
* @param string $message A message to display if the assertion fails.
25+
* @param string|array<string, scalar> $selector A query selector to search for.
26+
* @param string $markup The output that should contain the $selector.
27+
* @param string $message A message to display if the assertion fails.
2828
*
2929
* @return void
3030
*/
31-
public function assertContainsSelector($selector, $markup = '', $message = '')
32-
{
31+
public function assertContainsSelector(
32+
$selector,
33+
string $markup = '',
34+
string $message = ''
35+
): void {
3336
$constraint = new ContainsSelector(new Selector($selector));
3437

3538
static::assertThat($markup, $constraint, $message);
@@ -40,14 +43,17 @@ public function assertContainsSelector($selector, $markup = '', $message = '')
4043
*
4144
* @since 1.0.0
4245
*
43-
* @param string $selector A query selector for the element to find.
44-
* @param string $markup The output that should not contain the $selector.
45-
* @param string $message A message to display if the assertion fails.
46+
* @param string|array<string, scalar> $selector A query selector to search for.
47+
* @param string $markup The output that should not contain the $selector.
48+
* @param string $message A message to display if the assertion fails.
4649
*
4750
* @return void
4851
*/
49-
public function assertNotContainsSelector($selector, $markup = '', $message = '')
50-
{
52+
public function assertNotContainsSelector(
53+
$selector,
54+
string $markup = '',
55+
string $message = ''
56+
): void {
5157
$constraint = new LogicalNot(new ContainsSelector(new Selector($selector)));
5258

5359
static::assertThat($markup, $constraint, $message);
@@ -58,15 +64,19 @@ public function assertNotContainsSelector($selector, $markup = '', $message = ''
5864
*
5965
* @since 1.0.0
6066
*
61-
* @param int $count The number of matching elements expected.
62-
* @param string $selector A query selector for the element to find.
63-
* @param string $markup The markup to run the assertion against.
64-
* @param string $message A message to display if the assertion fails.
67+
* @param int $count The number of matching elements expected.
68+
* @param string|array<string, scalar> $selector A query selector to search for.
69+
* @param string $markup The markup to run the assertion against.
70+
* @param string $message A message to display if the assertion fails.
6571
*
6672
* @return void
6773
*/
68-
public function assertSelectorCount($count, $selector, $markup = '', $message = '')
69-
{
74+
public function assertSelectorCount(
75+
int $count,
76+
$selector,
77+
string $markup = '',
78+
string $message = ''
79+
): void {
7080
$constraint = new SelectorCount(new Selector($selector), $count);
7181

7282
static::assertThat($markup, $constraint, $message);
@@ -85,8 +95,11 @@ public function assertSelectorCount($count, $selector, $markup = '', $message =
8595
*
8696
* @return void
8797
*/
88-
public function assertHasElementWithAttributes($attributes = [], $markup = '', $message = '')
89-
{
98+
public function assertHasElementWithAttributes(
99+
array $attributes = [],
100+
string $markup = '',
101+
string $message = ''
102+
): void {
90103
$constraint = new ContainsSelector(new Selector($attributes));
91104

92105
static::assertThat($markup, $constraint, $message);
@@ -105,8 +118,11 @@ public function assertHasElementWithAttributes($attributes = [], $markup = '', $
105118
*
106119
* @return void
107120
*/
108-
public function assertNotHasElementWithAttributes($attributes = [], $markup = '', $message = '')
109-
{
121+
public function assertNotHasElementWithAttributes(
122+
$attributes = [],
123+
$markup = '',
124+
$message = ''
125+
): void {
110126
$constraint = new LogicalNot(new ContainsSelector(new Selector($attributes)));
111127

112128
static::assertThat($markup, $constraint, $message);
@@ -117,15 +133,19 @@ public function assertNotHasElementWithAttributes($attributes = [], $markup = ''
117133
*
118134
* @since 1.1.0
119135
*
120-
* @param string $contents The string to look for within the DOM node's contents.
121-
* @param string $selector A query selector for the element to find.
122-
* @param string $markup The output that should contain the $selector.
123-
* @param string $message A message to display if the assertion fails.
136+
* @param string $contents The string to look for within the DOM node's contents.
137+
* @param string|array<string, scalar> $selector A query selector to search for.
138+
* @param string $markup The output that should contain the $selector.
139+
* @param string $message A message to display if the assertion fails.
124140
*
125141
* @return void
126142
*/
127-
public function assertElementContains($contents, $selector = '', $markup = '', $message = '')
128-
{
143+
public function assertElementContains(
144+
string $contents,
145+
$selector = '',
146+
string $markup = '',
147+
string $message = ''
148+
): void {
129149
$constraint = new ElementContainsString(new Selector($selector), $contents);
130150

131151
static::assertThat($markup, $constraint, $message);
@@ -136,15 +156,19 @@ public function assertElementContains($contents, $selector = '', $markup = '', $
136156
*
137157
* @since 1.1.0
138158
*
139-
* @param string $contents The string to look for within the DOM node's contents.
140-
* @param string $selector A query selector for the element to find.
141-
* @param string $markup The output that should not contain the $selector.
142-
* @param string $message A message to display if the assertion fails.
159+
* @param string $contents The string to look for within the DOM node's contents.
160+
* @param string|array<string, scalar> $selector A query selector to search for.
161+
* @param string $markup The output that should not contain the $selector.
162+
* @param string $message A message to display if the assertion fails.
143163
*
144164
* @return void
145165
*/
146-
public function assertElementNotContains($contents, $selector = '', $markup = '', $message = '')
147-
{
166+
public function assertElementNotContains(
167+
string $contents,
168+
$selector = '',
169+
string $markup = '',
170+
string $message = ''
171+
): void {
148172
$constraint = new LogicalNot(new ElementContainsString(new Selector($selector), $contents));
149173

150174
static::assertThat($markup, $constraint, $message);
@@ -155,15 +179,19 @@ public function assertElementNotContains($contents, $selector = '', $markup = ''
155179
*
156180
* @since 1.1.0
157181
*
158-
* @param string $regexp The regular expression pattern to look for within the DOM node.
159-
* @param string $selector A query selector for the element to find.
160-
* @param string $markup The output that should contain the $selector.
161-
* @param string $message A message to display if the assertion fails.
182+
* @param string $regexp The regular expression pattern to look for within the DOM node.
183+
* @param string|array<string, scalar> $selector A query selector to search for.
184+
* @param string $markup The output that should contain the $selector.
185+
* @param string $message A message to display if the assertion fails.
162186
*
163187
* @return void
164188
*/
165-
public function assertElementRegExp($regexp, $selector = '', $markup = '', $message = '')
166-
{
189+
public function assertElementRegExp(
190+
string $regexp,
191+
$selector = '',
192+
string $markup = '',
193+
string $message = ''
194+
): void {
167195
$constraint = new ElementMatchesRegExp(new Selector($selector), $regexp);
168196

169197
static::assertThat($markup, $constraint, $message);
@@ -174,15 +202,19 @@ public function assertElementRegExp($regexp, $selector = '', $markup = '', $mess
174202
*
175203
* @since 1.1.0
176204
*
177-
* @param string $regexp The regular expression pattern to look for within the DOM node.
178-
* @param string $selector A query selector for the element to find.
179-
* @param string $markup The output that should not contain the $selector.
180-
* @param string $message A message to display if the assertion fails.
205+
* @param string $regexp The regular expression pattern to look for within the DOM node.
206+
* @param string|array<string, scalar> $selector A query selector to search for.
207+
* @param string $markup The output that should not contain the $selector.
208+
* @param string $message A message to display if the assertion fails.
181209
*
182210
* @return void
183211
*/
184-
public function assertElementNotRegExp($regexp, $selector = '', $markup = '', $message = '')
185-
{
212+
public function assertElementNotRegExp(
213+
string $regexp,
214+
$selector = '',
215+
string $markup = '',
216+
string $message = ''
217+
): void {
186218
$constraint = new LogicalNot(new ElementMatchesRegExp(new Selector($selector), $regexp));
187219

188220
static::assertThat($markup, $constraint, $message);

src/Selector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($selector)
3838
*
3939
* @return string
4040
*/
41-
public function __toString()
41+
public function __toString(): string
4242
{
4343
return $this->getValue();
4444
}
@@ -48,7 +48,7 @@ public function __toString()
4848
*
4949
* @return string
5050
*/
51-
public function getValue()
51+
public function getValue(): string
5252
{
5353
return $this->selector;
5454
}
@@ -61,7 +61,7 @@ public function getValue()
6161
*
6262
* @return string The flattened attribute array.
6363
*/
64-
private function attributeArrayToString($attributes)
64+
private function attributeArrayToString(array $attributes): string
6565
{
6666
if (empty($attributes)) {
6767
throw new AttributeArrayException('Attributes array is empty.');

tests/Integration/AssertionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MarkupAssertionsTraitTest extends TestCase
3030
</main>
3131
HTML;
3232

33-
public function testPresenceOfSelectors()
33+
public function testPresenceOfSelectors(): void
3434
{
3535
$this->assertContainsSelector('main', $this->markup);
3636
$this->assertContainsSelector('h1', $this->markup);
@@ -57,7 +57,7 @@ public function testPresenceOfSelectors()
5757
);
5858
}
5959

60-
public function testMatchingContentsOfSelectors()
60+
public function testMatchingContentsOfSelectors(): void
6161
{
6262
$this->assertElementContains('Good news', 'main', $this->markup);
6363
$this->assertElementContains('Good news', 'h1', $this->markup);

tests/Unit/Constraints/ContainsSelectorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ContainsSelectorTest extends TestCase
1919
* @test
2020
* @dataProvider provideSelectorVariants
2121
*/
22-
public function it_should_find_matching_selectors_in_content($selector)
22+
public function it_should_find_matching_selectors_in_content(string $selector): void
2323
{
2424
$constraint = new ContainsSelector(new Selector($selector));
2525
$html = '<a href="https://example.com" id="my-link" class="link another-class">Example</a>';
@@ -31,7 +31,7 @@ public function it_should_find_matching_selectors_in_content($selector)
3131
* @test
3232
* @dataProvider provideSelectorVariants
3333
*/
34-
public function it_should_not_find_unmatched_selectors_in_content($selector)
34+
public function it_should_not_find_unmatched_selectors_in_content(string $selector): void
3535
{
3636
$constraint = new ContainsSelector(new Selector($selector));
3737
$html = '<h1 id="page-title" class="foo bar">This element has little to do with the link.</h1>';
@@ -42,7 +42,7 @@ public function it_should_not_find_unmatched_selectors_in_content($selector)
4242
/**
4343
* @test
4444
*/
45-
public function it_should_fail_with_a_useful_error_message()
45+
public function it_should_fail_with_a_useful_error_message(): void
4646
{
4747
$selector = new Selector('p');
4848
$html = '<h1>Some Title</h1>';
@@ -65,7 +65,7 @@ public function it_should_fail_with_a_useful_error_message()
6565
*
6666
* @return iterable<string,array<string>>
6767
*/
68-
public function provideSelectorVariants()
68+
public function provideSelectorVariants(): iterable
6969
{
7070
yield 'Simple tag name' => ['a'];
7171
yield 'Class name' => ['.link'];

tests/Unit/Constraints/ElementContainsStringTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ElementContainsStringTest extends TestCase
1818
/**
1919
* @test
2020
*/
21-
public function it_should_match_a_string_in_the_given_markup()
21+
public function it_should_match_a_string_in_the_given_markup(): void
2222
{
2323
$constraint = new ElementContainsString(new Selector('p'), 'This is the content', true);
2424
$html = '<h1>Title</h1><p>This is the content</p>';
@@ -29,7 +29,7 @@ public function it_should_match_a_string_in_the_given_markup()
2929
/**
3030
* @test
3131
*/
32-
public function it_should_match_in_a_case_sensitive_manner_by_default()
32+
public function it_should_match_in_a_case_sensitive_manner_by_default(): void
3333
{
3434
$constraint = new ElementContainsString(new Selector('p'), 'THIS IS THE CONTENT');
3535
$html = '<h1>Title</h1><p>This is the content</p>';
@@ -43,7 +43,7 @@ public function it_should_match_in_a_case_sensitive_manner_by_default()
4343
/**
4444
* @test
4545
*/
46-
public function it_should_be_able_to_ignore_case()
46+
public function it_should_be_able_to_ignore_case(): void
4747
{
4848
$constraint = new ElementContainsString(new Selector('p'), 'THIS IS THE CONTENT', true);
4949
$html = '<h1>Title</h1><p>This is the content</p>';
@@ -57,7 +57,7 @@ public function it_should_be_able_to_ignore_case()
5757
/**
5858
* @test
5959
*/
60-
public function it_should_fail_if_no_match_is_found()
60+
public function it_should_fail_if_no_match_is_found(): void
6161
{
6262
$constraint = new ElementContainsString(new Selector('p'), 'This is the content');
6363
$html = '<h1>This is the content</h1><p>But this is not</p>';
@@ -72,7 +72,7 @@ public function it_should_fail_if_no_match_is_found()
7272
*
7373
* @ticket https://github.com/stevegrunwell/phpunit-markup-assertions/issues/31
7474
*/
75-
public function it_should_be_able_to_handle_various_character_sets($greeting)
75+
public function it_should_be_able_to_handle_various_character_sets(string $greeting): void
7676
{
7777
$constraint = new ElementContainsString(new Selector('h1'), $greeting);
7878
$html = sprintf('<div><h1>%s</h1></div>', $greeting);
@@ -83,7 +83,7 @@ public function it_should_be_able_to_handle_various_character_sets($greeting)
8383
/**
8484
* @test
8585
*/
86-
public function it_should_test_against_the_inner_contents_of_the_found_nodes()
86+
public function it_should_test_against_the_inner_contents_of_the_found_nodes(): void
8787
{
8888
$constraint = new ElementContainsString(new Selector('p'), 'class');
8989
$html = '<p class="first">First</p><p class="second">Second</p>';
@@ -97,7 +97,7 @@ public function it_should_test_against_the_inner_contents_of_the_found_nodes()
9797
/**
9898
* @test
9999
*/
100-
public function it_should_fail_with_a_useful_error_message()
100+
public function it_should_fail_with_a_useful_error_message(): void
101101
{
102102
$html = '<p>Some other string</p>';
103103
$expected = <<<'MSG'
@@ -121,7 +121,7 @@ public function it_should_fail_with_a_useful_error_message()
121121
/**
122122
* @test
123123
*/
124-
public function it_should_include_all_relevant_matches_in_error_messages()
124+
public function it_should_include_all_relevant_matches_in_error_messages(): void
125125
{
126126
$html = '<p>Some other string</p><p>Yet another string</p>';
127127
$expected = <<<'MSG'
@@ -146,7 +146,7 @@ public function it_should_include_all_relevant_matches_in_error_messages()
146146
/**
147147
* @test
148148
*/
149-
public function it_should_provide_a_simple_error_message_if_no_selector_matches_are_found()
149+
public function it_should_provide_a_simple_error_message_if_no_selector_matches_are_found(): void
150150
{
151151
$html = '<p>Some other string</p><p>Yet another string</p>';
152152
$expected = "Failed asserting that any elements match selector 'h1'.";
@@ -164,9 +164,9 @@ public function it_should_provide_a_simple_error_message_if_no_selector_matches_
164164
/**
165165
* Provide a list of strings in various language.
166166
*
167-
* @return Iterable<string,array<string>>
167+
* @return iterable<string,array<string>>
168168
*/
169-
public function provideGreetingsInDifferentLanguages()
169+
public function provideGreetingsInDifferentLanguages(): iterable
170170
{
171171
yield 'Arabic' => ['مرحبا!'];
172172
yield 'Chinese' => ['你好'];

0 commit comments

Comments
 (0)