Skip to content

Commit 736900e

Browse files
authored
Add assertSelect and assertSelectExists macros (#39)
1 parent d55cb4b commit 736900e

File tree

6 files changed

+280
-119
lines changed

6 files changed

+280
-119
lines changed

ide-helper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public function assertFormExists($selector = 'form', $callback = null)
3333
return $instance;
3434
}
3535

36+
public function assertSelect($selector = 'select', $callback = null)
37+
{
38+
/** @var \Illuminate\Testing\TestResponse $instance */
39+
return $instance;
40+
}
41+
42+
public function assertSelectExists($selector = 'select', $callback = null)
43+
{
44+
/** @var \Illuminate\Testing\TestResponse $instance */
45+
return $instance;
46+
}
47+
3648
public function assertContainsElement($selector, array $attributes = [])
3749
{
3850
/** @var \Illuminate\Testing\TestResponse $instance */

src/TestResponseMacros.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\Assert;
1212
use Sinnbeck\DomAssertions\Asserts\AssertElement;
1313
use Sinnbeck\DomAssertions\Asserts\AssertForm;
14+
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
1415
use Sinnbeck\DomAssertions\Support\DomParser;
1516

1617
/**
@@ -203,4 +204,52 @@ public function assertFormExists(): Closure
203204
return $this;
204205
};
205206
}
207+
208+
public function assertSelect(): Closure
209+
{
210+
return $this->assertSelectExists();
211+
}
212+
213+
public function assertSelectExists(): Closure
214+
{
215+
return function ($selector = 'select', $callback = null): TestResponse {
216+
/** @var TestResponse $this */
217+
Assert::assertNotEmpty(
218+
$this->getContent(),
219+
'The view is empty!'
220+
);
221+
222+
try {
223+
$parser = DomParser::new($this->getContent());
224+
} catch (DOMException $exception) {
225+
Assert::fail($exception->getMessage());
226+
}
227+
228+
if ($selector instanceof Closure) {
229+
$callback = $selector;
230+
$selector = 'select';
231+
}
232+
233+
if (is_string($selector)) {
234+
$select = $parser->query($selector);
235+
} else {
236+
Assert::fail('Invalid selector!');
237+
}
238+
239+
Assert::assertNotNull(
240+
$select,
241+
sprintf('No select was found with selector "%s"', $selector)
242+
);
243+
Assert::assertEquals(
244+
'select',
245+
$select->nodeName,
246+
'Element is not of type select!');
247+
248+
if ($callback) {
249+
$callback(new AssertSelect($this->getContent(), $select));
250+
}
251+
252+
return $this;
253+
};
254+
}
206255
}

src/TestViewMacros.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\Assert;
1212
use Sinnbeck\DomAssertions\Asserts\AssertElement;
1313
use Sinnbeck\DomAssertions\Asserts\AssertForm;
14+
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
1415
use Sinnbeck\DomAssertions\Support\DomParser;
1516

1617
/**
@@ -204,4 +205,52 @@ public function assertFormExists(): Closure
204205
return $this;
205206
};
206207
}
208+
209+
public function assertSelect(): Closure
210+
{
211+
return $this->assertSelectExists();
212+
}
213+
214+
public function assertSelectExists(): Closure
215+
{
216+
return function ($selector = 'select', $callback = null): TestView {
217+
/** @var TestView $this */
218+
Assert::assertNotEmpty(
219+
(string) $this,
220+
'The view is empty!'
221+
);
222+
223+
try {
224+
$parser = DomParser::new((string) $this);
225+
} catch (DOMException $exception) {
226+
Assert::fail($exception->getMessage());
227+
}
228+
229+
if ($selector instanceof Closure) {
230+
$callback = $selector;
231+
$selector = 'select';
232+
}
233+
234+
if (is_string($selector)) {
235+
$select = $parser->query($selector);
236+
} else {
237+
Assert::fail('Invalid selector!');
238+
}
239+
240+
Assert::assertNotNull(
241+
$select,
242+
sprintf('No select was found with selector "%s"', $selector)
243+
);
244+
Assert::assertEquals(
245+
'select',
246+
$select->nodeName,
247+
'Element is not of type select!');
248+
249+
if ($callback) {
250+
$callback(new AssertSelect((string) $this, $select));
251+
}
252+
253+
return $this;
254+
};
255+
}
207256
}

tests/FormTest.php

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Sinnbeck\DomAssertions\Asserts\AssertDatalist;
55
use Sinnbeck\DomAssertions\Asserts\AssertElement;
66
use Sinnbeck\DomAssertions\Asserts\AssertForm;
7-
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
87

98
it('assertForm alias works for assertFormExists', function () {
109
$this->get('form')
@@ -177,123 +176,6 @@
177176
})->assertOk();
178177
});
179178

180-
it('can parse a select with options', function () {
181-
$this->get('form')
182-
->assertFormExists('#form2', function (AssertForm $form) {
183-
$form->findSelect('select:nth-of-type(2)', function (AssertSelect $selectAssert) {
184-
$selectAssert->has('name', 'country')
185-
->containsOption([
186-
'x-data' => 'none',
187-
'value' => 'none',
188-
'text' => 'None',
189-
'selected' => 'selected',
190-
])
191-
->containsOptions(
192-
[
193-
'value' => 'dk',
194-
'text' => 'Denmark',
195-
],
196-
[
197-
'value' => 'us',
198-
'text' => 'USA',
199-
],
200-
201-
);
202-
});
203-
})->assertOk();
204-
});
205-
206-
it('can parse a select with optgroups', function () {
207-
$this->get('form')
208-
->assertFormExists('#form2', function (AssertForm $form) {
209-
$form->findSelect('select:nth-of-type(3)', function (AssertSelect $selectAssert) {
210-
$selectAssert->has('name', 'things')
211-
->containsOptgroup([
212-
'label' => 'Animals',
213-
])
214-
->containsOptgroups(
215-
[
216-
'label' => 'Vegetables',
217-
'x-data' => 'none',
218-
],
219-
[
220-
'label' => 'Minerals',
221-
]
222-
)
223-
->containsOptions(
224-
[
225-
'value' => 'dog',
226-
'text' => 'Dog',
227-
],
228-
[
229-
'value' => 'cat',
230-
'text' => 'Cat',
231-
],
232-
[
233-
'value' => 'carrot',
234-
'text' => 'Carrot',
235-
],
236-
[
237-
'value' => 'onion',
238-
'text' => 'Onion',
239-
],
240-
[
241-
'value' => 'calcium',
242-
'text' => 'Calcium',
243-
],
244-
[
245-
'value' => 'zinc',
246-
'text' => 'Zinc',
247-
],
248-
);
249-
});
250-
})->assertOk();
251-
});
252-
253-
it('can parse a select with options functional', function () {
254-
$this->get('form')
255-
->assertFormExists('#form2', function (AssertForm $form) {
256-
$form->findSelect('select:nth-of-type(2)', function (AssertSelect $selectAssert) {
257-
$selectAssert->has('name', 'country')
258-
->findOption(function (AssertElement $optionAssert) {
259-
$optionAssert->hasValue('none');
260-
$optionAssert->hasText('None');
261-
});
262-
});
263-
})->assertOk();
264-
});
265-
266-
it('can assert that select has value', function () {
267-
$this->get('form')
268-
->assertFormExists('#form2', function (AssertForm $form) {
269-
$form->findSelect('select:nth-of-type(2)', function (AssertSelect $selectAssert) {
270-
$selectAssert->hasValue('none');
271-
});
272-
})->assertOk();
273-
});
274-
275-
it('can assert that option is selected', function () {
276-
$this->get('form')
277-
->assertFormExists('#form2', function (AssertForm $form) {
278-
$form->findSelect('select:nth-of-type(2)', function (AssertSelect $selectAssert) {
279-
$selectAssert->findOption(function (AssertElement $option) {
280-
$option->hasValue('none');
281-
$option->hasText('None');
282-
$option->hasSelected();
283-
});
284-
});
285-
})->assertOk();
286-
});
287-
288-
it('can assert that select has multiple values', function () {
289-
$this->get('form')
290-
->assertFormExists('#form2', function (AssertForm $form) {
291-
$form->findSelect('select', function (AssertSelect $selectAssert) {
292-
$selectAssert->hasValues(['da', 'en']);
293-
});
294-
})->assertOk();
295-
});
296-
297179
it('can find a button', function () {
298180
$this->get('form')
299181
->assertFormExists('#form2', function (AssertForm $form) {

0 commit comments

Comments
 (0)