Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 9478211

Browse files
committed
Refactors testUnderscore
Renames `testUnderscore` to `testValidatorHandlesUnderscoresInDomainsCorrectly()`, and has it use a data provider with individual inputs and assertions.
1 parent f4c26cf commit 9478211

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

test/HostnameTest.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,58 @@ public function testDashes()
119119
}
120120
}
121121

122+
/**
123+
* @return iterable
124+
*/
125+
public function domainsWithUnderscores()
126+
{
127+
yield 'subdomain with leading underscore' => [
128+
'_subdomain.domain.com',
129+
'assertTrue',
130+
];
131+
132+
yield 'subdomain with trailing underscore' => [
133+
'subdomain_.domain.com',
134+
'assertTrue',
135+
];
136+
137+
yield 'subdomain with single underscore' => [
138+
'sub_domain.domain.com',
139+
'assertTrue',
140+
];
141+
142+
yield 'subdomain with double underscore' => [
143+
'sub__domain.domain.com',
144+
'assertTrue',
145+
];
146+
147+
yield 'root domain with leading underscore' => [
148+
'_domain.com',
149+
'assertFalse',
150+
];
151+
152+
yield 'root domain with trailing underscore' => [
153+
'domain_.com',
154+
'assertFalse',
155+
];
156+
157+
yield 'root domain with underscore' => [
158+
'do_main.com',
159+
'assertFalse',
160+
];
161+
}
162+
122163
/**
123164
* Ensure the underscore character tests work as expected
124165
*
166+
* @dataProvider domainsWithUnderscores
167+
* @param string $input
168+
* @param string $assertion
125169
*/
126-
public function testUnderscores()
170+
public function testValidatorHandlesUnderscoresInDomainsCorrectly($input, $assertion)
127171
{
128-
$valuesExpected = [
129-
[Hostname::ALLOW_DNS, true, [
130-
'_subdomain.domain.com', 'subdomain_.domain.com', 'sub_domain.domain.com', 'sub__domain.domain.com'
131-
]],
132-
[Hostname::ALLOW_DNS, false, ['_domain.com', 'domain_.com', 'do_main.com']]
133-
];
134-
foreach ($valuesExpected as $element) {
135-
$validator = new Hostname($element[0]);
136-
foreach ($element[2] as $input) {
137-
$this->assertEquals(
138-
$element[1],
139-
$validator->isValid($input),
140-
implode("\n", $validator->getMessages()) . $input
141-
);
142-
}
143-
}
172+
$validator = new Hostname(Hostname::ALLOW_DNS);
173+
$this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages()));
144174
}
145175

146176
/**

0 commit comments

Comments
 (0)