Skip to content

Commit

Permalink
Add a trait that checks to see if $this->exporter()->export() is avai…
Browse files Browse the repository at this point in the history
…lable before trying to use it
  • Loading branch information
stevegrunwell committed Dec 12, 2023
1 parent f85fca4 commit 0d6a9cd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Constraints/ContainsSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class ContainsSelector extends Constraint
{
use ExporterTrait;

/**
* @var Selector
*/
Expand All @@ -31,7 +33,7 @@ public function __construct(Selector $selector)
*/
public function toString(): string
{
return 'contains selector ' . $this->exporter()->export($this->selector->getValue());
return 'contains selector ' . $this->exportValue($this->selector->getValue());
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Constraints/ElementContainsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class ElementContainsString extends Constraint
{
use ExporterTrait;

/**
* A cache of matches that we have checked against.
*
Expand Down Expand Up @@ -56,7 +58,7 @@ public function toString(): string
return sprintf(
'%s string %s',
count($this->matchingElements) >= 2 ? 'contain' : 'contains',
$this->exporter()->export($this->needle)
$this->exportValue($this->needle)
);
}

Expand Down Expand Up @@ -115,7 +117,7 @@ protected function failureDescription($html): string

return sprintf(
$label,
$this->exporter()->export($this->selector->getValue()),
$this->exportValue($this->selector->getValue()),
$this->toString()
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Constraints/ElementMatchesRegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class ElementMatchesRegExp extends ElementContainsString
{
use ExporterTrait;

/**
* @var string
*/
Expand All @@ -37,7 +39,7 @@ public function toString(): string
return sprintf(
'%s regular expression %s',
count($this->matchingElements) >= 2 ? 'match' : 'matches',
$this->exporter()->export($this->pattern)
$this->exportValue($this->pattern)
);
}

Expand Down
29 changes: 29 additions & 0 deletions src/Constraints/ExporterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SteveGrunwell\PHPUnit_Markup_Assertions\Constraints;

/**
* Trait that exposes an exportValue method that will attempt to use
* {@see SebastianBergmann\Exporter\Exporter} when available.
*/
trait ExporterTrait
{
/**
* Export a value to include it in error messages.
*
* If {@see SebastianBergmann\Exporter\Exporter} is available (as it is in modern versions of
* PHPUnit), then it will be used. Otherwise, we'll do a simple var_export().
*
* @param mixed $value The value to be exported.
*
* @return string A string representation of the value.
*/
private function exportValue($value): string
{
if (method_exists($this, 'exporter')) {
return $this->exporter()->export($value);
}

return var_export($value, true);
}
}
4 changes: 3 additions & 1 deletion src/Constraints/SelectorCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class SelectorCount extends Constraint
{
use ExporterTrait;

/**
* @var int
*/
Expand Down Expand Up @@ -41,7 +43,7 @@ public function toString(): string
return sprintf(
'contains %d instance(s) of selector %s',
$this->count,
$this->exporter()->export($this->selector->getValue())
$this->exportValue($this->selector->getValue())
);
}

Expand Down

0 comments on commit 0d6a9cd

Please sign in to comment.