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 895c8d0 commit a5622fe
Show file tree
Hide file tree
Showing 5 changed files with 32 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
19 changes: 19 additions & 0 deletions src/Constraints/ExporterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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
{
private function exportValue($value)
{
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 a5622fe

Please sign in to comment.