Skip to content

Commit 0709b10

Browse files
authored
Rename with() -> one() and scope() -> with() (#12)
* rename with() to one() * rename trait to targetable * pint * rename scope() to with() * rename trait * pint
1 parent a84b362 commit 0709b10

13 files changed

Lines changed: 89 additions & 89 deletions

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function testComponent: void
179179
->assertAttributeEquals('method', 'post')
180180
->assertAttributeEquals('action', '/foo/bar');
181181

182-
$form->with('input[name="name"]', function (AssertableElement $input) {
182+
$form->one('input[name="name"]', function (AssertableElement $input) {
183183
$input->assertAttributeEquals('value', 'My New Action');
184184
$input->assertAttributePresent('required');
185185
$input->assertClassContains('form-input');
@@ -366,12 +366,12 @@ $document->getElementById('bar');
366366

367367
### Scopes
368368

369-
Sometimes your assertions need room to breathe. For this you can use `with()`, `many()`, `elsewhere()` and `scope()` to filter elements into a callback for better readability.
369+
Sometimes your assertions need room to breathe. For this you can use `one()`, `many()`, `elsewhere()` and `with()` to filter elements into a callback for better readability.
370370

371-
- `with()`: The first matching element in the **current** scope using `querySelector()`.
371+
- `one()`: The first matching element in the **current** scope using `querySelector()`.
372372
- `many()`: Every matching element in the **current** scope using `querySelectorAll()`,
373373
- `elsewhere()`: The first matching element in the **document** scope using `querySelector()`.
374-
- `scope()`: The current element.
374+
- `with()`: The current element.
375375

376376
Let's give them a try:
377377

@@ -392,7 +392,7 @@ $document = AssertableDocument::createFromString(<<<'HTML'
392392
</div>
393393
HTML, LIBXML_HTML_NOIMPLIED);
394394

395-
$document->with('div#inner', function (AssertableElement $inner) {
395+
$document->one('div#inner', function (AssertableElement $inner) {
396396
$inner->assertIdEquals('inner');
397397

398398
$inner->many('div.innermost', function (AssertableElementsList $innerMosts) {
@@ -403,7 +403,7 @@ $document->with('div#inner', function (AssertableElement $inner) {
403403
$anotherInner->assertIdEquals('another-inner');
404404
});
405405

406-
$inner->scope(function (AssertableElement $inner) {
406+
$inner->with(function (AssertableElement $inner) {
407407
$inner->assertIdEquals('inner');
408408
});
409409
});
@@ -522,5 +522,5 @@ This package wouldn't be possible without the following people and projects:
522522
- Rachel ❤️, Archie 🐶 and Rigby 🐶
523523
- Niels Dossche (PHP 8.4 HTML parsing API author)
524524
- [Laravel DOM Assertions](https://github.com/sinnbeck/laravel-dom-assertions) for showing me the possibilities of HTML assertions
525-
- [Laravel Dusk](https://github.com/laravel/dusk) for showing me the `with()` and `elsewhere()` scoping syntax
525+
- [Laravel Dusk](https://github.com/laravel/dusk) for showing me the `one()` and `elsewhere()` scoping syntax
526526
- [Lexbor](https://github.com/lexbor/lexbor) (the library that powers PHP 8.4's HTML parsing API)

src/Concerns/Scopeable.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Concerns/Targetable.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ziadoz\AssertableHtml\Concerns;
6+
7+
use Ziadoz\AssertableHtml\Dom\AssertableDocument;
8+
use Ziadoz\AssertableHtml\Dom\AssertableElement;
9+
use Ziadoz\AssertableHtml\Dom\AssertableElementsList;
10+
11+
trait Targetable
12+
{
13+
/**
14+
* Scope the first assertable element within the current assertable document or element matching the given selector.
15+
*
16+
* @param callable(AssertableElement $assertable): void $callback
17+
*/
18+
public function one(string $selector, callable $callback): static
19+
{
20+
$callback($this->querySelector($selector));
21+
22+
return $this;
23+
}
24+
25+
/**
26+
* Scope the matching assertable elements within the current assertable document or element matching the given selector.
27+
*
28+
* @param callable(AssertableElementsList $assertable): void $callback
29+
*/
30+
public function many(string $selector, callable $callback): static
31+
{
32+
$callback($this->querySelectorAll($selector));
33+
34+
return $this;
35+
}
36+
37+
/**
38+
* Scope the first assertable element elsewhere in the assertable document matching the given selector.
39+
*
40+
* @param callable(AssertableElement $assertable): void $callback
41+
*/
42+
public function elsewhere(string $selector, callable $callback): static
43+
{
44+
$document = $this instanceof AssertableDocument
45+
? $this
46+
: AssertableDocument::createFromString($this->element->ownerDocument->saveHtml());
47+
48+
$callback($document->querySelector($selector));
49+
50+
return $this;
51+
}
52+
}

src/Concerns/Withable.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,16 @@
44

55
namespace Ziadoz\AssertableHtml\Concerns;
66

7-
use Ziadoz\AssertableHtml\Dom\AssertableDocument;
8-
use Ziadoz\AssertableHtml\Dom\AssertableElement;
9-
use Ziadoz\AssertableHtml\Dom\AssertableElementsList;
10-
117
trait Withable
128
{
139
/**
14-
* Scope the first assertable element within the current assertable document or element matching the given selector.
15-
*
16-
* @param callable(AssertableElement $assertable): void $callback
17-
*/
18-
public function with(string $selector, callable $callback): static
19-
{
20-
$callback($this->querySelector($selector));
21-
22-
return $this;
23-
}
24-
25-
/**
26-
* Scope the matching assertable elements within the current assertable document or element matching the given selector.
10+
* Scope with the current assertable, mostly for readability purposes.
2711
*
28-
* @param callable(AssertableElementsList $assertable): void $callback
12+
* @param callable(static $assertable): void $callback
2913
*/
30-
public function many(string $selector, callable $callback): static
14+
public function with(callable $callback): static
3115
{
32-
$callback($this->querySelectorAll($selector));
33-
34-
return $this;
35-
}
36-
37-
/**
38-
* Scope the first assertable element elsewhere in the assertable document matching the given selector.
39-
*
40-
* @param callable(AssertableElement $assertable): void $callback
41-
*/
42-
public function elsewhere(string $selector, callable $callback): static
43-
{
44-
$document = $this instanceof AssertableDocument
45-
? $this
46-
: AssertableDocument::createFromString($this->element->ownerDocument->saveHtml());
47-
48-
$callback($document->querySelector($selector));
16+
$callback($this);
4917

5018
return $this;
5119
}

src/Dom/AssertableDocument.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
use ErrorException;
1010
use PHPUnit\Framework\Assert as PHPUnit;
1111
use Ziadoz\AssertableHtml\Concerns\AssertsDocument;
12-
use Ziadoz\AssertableHtml\Concerns\Scopeable;
12+
use Ziadoz\AssertableHtml\Concerns\Targetable;
1313
use Ziadoz\AssertableHtml\Concerns\Whenable;
1414
use Ziadoz\AssertableHtml\Concerns\Withable;
1515
use Ziadoz\AssertableHtml\Exceptions\UnableToCreateAssertableDocument;
1616

1717
final readonly class AssertableDocument
1818
{
1919
use AssertsDocument;
20-
use Scopeable;
20+
use Targetable;
2121
use Whenable;
2222
use Withable;
2323

src/Dom/AssertableElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
use PHPUnit\Framework\Assert as PHPUnit;
1010
use Ziadoz\AssertableHtml\Concerns\AssertsElement;
1111
use Ziadoz\AssertableHtml\Concerns\IdentifiesElement;
12-
use Ziadoz\AssertableHtml\Concerns\Scopeable;
12+
use Ziadoz\AssertableHtml\Concerns\Targetable;
1313
use Ziadoz\AssertableHtml\Concerns\Whenable;
1414
use Ziadoz\AssertableHtml\Concerns\Withable;
1515

1616
readonly class AssertableElement
1717
{
1818
use AssertsElement;
1919
use IdentifiesElement;
20-
use Scopeable;
20+
use Targetable;
2121
use Whenable;
2222
use Withable;
2323

src/Dom/AssertableElementsList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
use RuntimeException;
1717
use Traversable;
1818
use Ziadoz\AssertableHtml\Concerns\AssertsElementsList;
19-
use Ziadoz\AssertableHtml\Concerns\Scopeable;
2019
use Ziadoz\AssertableHtml\Concerns\Whenable;
20+
use Ziadoz\AssertableHtml\Concerns\Withable;
2121

2222
final readonly class AssertableElementsList implements ArrayAccess, Countable, IteratorAggregate
2323
{
2424
use AssertsElementsList;
25-
use Scopeable;
2625
use Whenable;
26+
use Withable;
2727

2828
/** The assertable elements. */
2929
private array $elements;

src/Mixins/TestComponentMixins.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function assertableHtml(): Closure
2121
public function assertComponent(): Closure
2222
{
2323
return function (callable $callback, int $options = LIBXML_HTML_NOIMPLIED, ?string $overrideEncoding = null): static {
24-
$this->assertableHtml($options, $overrideEncoding)->scope($callback);
24+
$this->assertableHtml($options, $overrideEncoding)->with($callback);
2525

2626
return $this;
2727
};
@@ -31,7 +31,7 @@ public function assertComponent(): Closure
3131
public function assertElement(): Closure
3232
{
3333
return function (string $selector, callable $callback, int $options = LIBXML_HTML_NOIMPLIED, ?string $overrideEncoding = null): static {
34-
$this->assertableHtml($options, $overrideEncoding)->with($selector, $callback);
34+
$this->assertableHtml($options, $overrideEncoding)->one($selector, $callback);
3535

3636
return $this;
3737
};

src/Mixins/TestResponseMixins.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function assertableHtml(): Closure
2828
public function assertHtml(): Closure
2929
{
3030
return function (callable $callback, int $options = 0, ?string $overrideEncoding = null): static {
31-
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->scope($callback);
31+
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->with($callback);
3232

3333
return $this;
3434
};
@@ -38,7 +38,7 @@ public function assertHtml(): Closure
3838
public function assertHead(): Closure
3939
{
4040
return function (callable $callback, int $options = 0, ?string $overrideEncoding = null): static {
41-
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->with('head', $callback);
41+
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->one('head', $callback);
4242

4343
return $this;
4444
};
@@ -48,7 +48,7 @@ public function assertHead(): Closure
4848
public function assertBody(): Closure
4949
{
5050
return function (callable $callback, int $options = 0, ?string $overrideEncoding = null): static {
51-
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->with('body', $callback);
51+
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->one('body', $callback);
5252

5353
return $this;
5454
};
@@ -58,7 +58,7 @@ public function assertBody(): Closure
5858
public function assertElement(): Closure
5959
{
6060
return function (string $selector, callable $callback, int $options = 0, ?string $overrideEncoding = null): static {
61-
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->with($selector, $callback);
61+
AssertableDocument::createFromString($this->getHtmlContent(), $options, $overrideEncoding)->one($selector, $callback);
6262

6363
return $this;
6464
};

src/Mixins/TestViewMixins.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function assertableHtml(): Closure
2121
public function assertView(): Closure
2222
{
2323
return function (callable $callback, int $options = LIBXML_HTML_NOIMPLIED, ?string $overrideEncoding = null): static {
24-
$this->assertableHtml($options, $overrideEncoding)->scope($callback);
24+
$this->assertableHtml($options, $overrideEncoding)->with($callback);
2525

2626
return $this;
2727
};
@@ -31,7 +31,7 @@ public function assertView(): Closure
3131
public function assertElement(): Closure
3232
{
3333
return function (string $selector, callable $callback, int $options = LIBXML_HTML_NOIMPLIED, ?string $overrideEncoding = null): static {
34-
$this->assertableHtml($options, $overrideEncoding)->with($selector, $callback);
34+
$this->assertableHtml($options, $overrideEncoding)->one($selector, $callback);
3535

3636
return $this;
3737
};

0 commit comments

Comments
 (0)