Skip to content

Commit 8dbabbc

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

File tree

8 files changed

+112
-59
lines changed

8 files changed

+112
-59
lines changed

src/Constraints/ElementContainsString.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ protected function additionalFailureDescription($other): string
9090
*/
9191
protected function exportMatchesArray(array $matches): string
9292
{
93+
$matches = array_map('trim', $matches);
94+
9395
return '[' . PHP_EOL . ' ' . implode(PHP_EOL . ' ', $matches) . PHP_EOL . ']';
9496
}
9597

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+
) {
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+
) {
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+
) {
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+
) {
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+
) {
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+
) {
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+
) {
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+
) {
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+
) {
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/Unit/Constraints/ContainsSelectorTest.php

Lines changed: 2 additions & 2 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)
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)
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>';

tests/Unit/Constraints/ElementContainsStringTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
7676
{
7777
$constraint = new ElementContainsString(new Selector('h1'), $greeting);
7878
$html = sprintf('<div><h1>%s</h1></div>', $greeting);
@@ -164,7 +164,7 @@ 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
*/
169169
public function provideGreetingsInDifferentLanguages()
170170
{

tests/Unit/DOMTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class DOMTest extends TestCase
1919
*
2020
* @dataProvider provideMarkupWithInnerClass
2121
*/
22-
public function it_should_be_able_to_count_selectors($markup, $expected)
22+
public function it_should_be_able_to_count_selectors(string $markup, int $expected)
2323
{
2424
$dom = new DOM($markup);
2525
$selector = new Selector('.inner');
@@ -63,7 +63,7 @@ public function getInnerHtml_should_return_an_empty_array_if_there_are_no_matche
6363
$this->assertEmpty($dom->getInnerHtml(new Selector('h2')));
6464
}
6565

66-
/**
66+
/**
6767
* @test
6868
* @testdox getOuterHtml() should retrieve the outer HTML for each matching element.
6969
*/
@@ -121,7 +121,7 @@ public function query_should_throw_a_SelectorException_if_the_selector_is_invali
121121
/**
122122
* Return test cases with varying numbers of .inner elements.
123123
*
124-
* @return Iterable<string, array{string, int}>
124+
* @return iterable<string, array{string, int}>
125125
*/
126126
public function provideMarkupWithInnerClass()
127127
{

tests/Unit/SelectorTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,34 @@ public function it_should_accept_string_selectors()
2626
/**
2727
* @test
2828
*
29+
* @param array<string, scalar> $attributes
30+
* @param string $expected
31+
*
2932
* @dataProvider provideAttributes
3033
*/
31-
public function it_should_automatically_convert_attribute_arrays_to_strings($attributes, $expected)
32-
{
34+
public function it_should_automatically_convert_attribute_arrays_to_strings(
35+
array $attributes,
36+
string $expected
37+
) {
3338
$selector = new Selector($attributes);
3439

3540
$this->assertSame($expected, $selector->getValue());
3641
}
3742

43+
/**
44+
* @test
45+
*
46+
* @ticket https://github.com/stevegrunwell/phpunit-markup-assertions/issues/13
47+
*/
48+
public function it_should_be_able_to_handle_spaces_in_attribute_values()
49+
{
50+
$selector = new Selector([
51+
'data-attr' => 'foo bar baz',
52+
]);
53+
54+
$this->assertSame('*[data-attr="foo bar baz"]', $selector->getValue());
55+
}
56+
3857
/**
3958
* @test
4059
*/
@@ -58,9 +77,9 @@ public function it_should_be_able_to_be_cast_to_a_string()
5877
/**
5978
* Data provider for testFlattenAttributeArray().
6079
*
61-
* @return Iterable{array<string, scalar>, string} The attribute array and teh expected string.
80+
* @return iterable<string, array{array<string, ?scalar>, string}> The attribute array and the expected string.
6281
*/
63-
public function provideAttributes()
82+
public function provideAttributes(): iterable
6483
{
6584
yield 'Single attribute' => [
6685
[

0 commit comments

Comments
 (0)