Skip to content

Commit

Permalink
Switch CSS selector (#516)
Browse files Browse the repository at this point in the history
* Switching to symfony/css-selector package

* Switch to symfony here too
  • Loading branch information
srtfisher committed Mar 15, 2024
1 parent e2c8b52 commit 0d0343f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"nette/php-generator": "^4.1",
"nunomaduro/collision": "^6.0 || ^7.0",
"nunomaduro/termwind": "^1.15.1",
"phpgt/cssxpath": "^1.2",
"psr/container": "^1.1.1 || ^2.0.2",
"psr/log": "^1.0.1 || ^2.0 || ^3.0",
"psr/simple-cache": "^3.0",
"ramsey/uuid": "^4.7.4",
"spatie/phpunit-snapshot-assertions": "^4.2 || ^5.1",
"symfony/console": "^6.2",
"symfony/css-selector": "^6.2",
"symfony/finder": "^6.2",
"symfony/http-foundation": "^6.0.20",
"symfony/http-kernel": "^6.0.20",
Expand Down
6 changes: 0 additions & 6 deletions src/mantle/support/helpers/helpers-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ function blank( $value ): bool {
* Get the class "basename" of the given object / class.
*
* @param string|object $class Class or object to basename.
*
* @return string
*/
function class_basename( string|object $class ): string {
$class = is_object( $class ) ? $class::class : $class;
Expand All @@ -61,8 +59,6 @@ function class_basename( string|object $class ): string {
* Returns all traits used by a class, its parent classes and trait of their traits.
*
* @param object|string $class Class or object to analyze.
*
* @return array
*/
function class_uses_recursive( string|object $class ): array {
if ( is_object( $class ) ) {
Expand Down Expand Up @@ -152,8 +148,6 @@ function collect( $value = null ): Collection {
* Determine if a value is "filled".
*
* @param mixed $value Value to check.
*
* @return bool
*/
function filled( mixed $value ): bool {
return ! blank( $value );
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/testing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"mantle-framework/http": "^1.0",
"mantle-framework/support": "^1.0",
"nunomaduro/termwind": "^1.15.1",
"phpgt/cssxpath": "^1.2",
"symfony/css-selector": "^6.2",
"spatie/phpunit-snapshot-assertions": "^4.2 || ^5.1"
},
"extra": {
Expand Down
17 changes: 14 additions & 3 deletions src/mantle/testing/concerns/trait-element-assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPUnit\Framework\Assert as PHPUnit;
use DOMDocument;
use DOMXPath;
use Gt\CssXPath\Translator;
use Symfony\Component\CssSelector\CssSelectorConverter;

/**
* Assorted Test_Cast assertions for checking for elements in a response.
Expand All @@ -37,6 +37,17 @@ protected function get_dom_document(): DOMDocument {
return $this->document;
}

/**
* Convert a CSS selector to an XPath query.
*
* @param string $selector The selector to convert.
*/
protected function convert_query_selector( string $selector ): string {
$converter = new CssSelectorConverter( true );

return $converter->toXPath( $selector );
}

/**
* Assert that an element exists in the response.
*
Expand Down Expand Up @@ -139,7 +150,7 @@ public function assertElementMissingByTagName( string $type ): static {
* @param string $selector The selector to use.
*/
public function assertElementExistsByQuerySelector( string $selector ): static {
return $this->assertElementExists( new Translator( $selector ) );
return $this->assertElementExists( $this->convert_query_selector( $selector ) );
}

/**
Expand All @@ -157,7 +168,7 @@ public function assertQuerySelectorExists( string $selector ): static {
* @param string $selector The selector to use.
*/
public function assertElementMissingByQuerySelector( string $selector ): static {
return $this->assertElementMissing( new Translator( $selector ) );
return $this->assertElementMissing( $this->convert_query_selector( $selector ) );
}

/**
Expand Down

0 comments on commit 0d0343f

Please sign in to comment.