Skip to content

Commit 0d6a9cd

Browse files
committed
Add a trait that checks to see if $this->exporter()->export() is available before trying to use it
1 parent f85fca4 commit 0d6a9cd

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

src/Constraints/ContainsSelector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class ContainsSelector extends Constraint
1313
{
14+
use ExporterTrait;
15+
1416
/**
1517
* @var Selector
1618
*/
@@ -31,7 +33,7 @@ public function __construct(Selector $selector)
3133
*/
3234
public function toString(): string
3335
{
34-
return 'contains selector ' . $this->exporter()->export($this->selector->getValue());
36+
return 'contains selector ' . $this->exportValue($this->selector->getValue());
3537
}
3638

3739
/**

src/Constraints/ElementContainsString.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class ElementContainsString extends Constraint
1313
{
14+
use ExporterTrait;
15+
1416
/**
1517
* A cache of matches that we have checked against.
1618
*
@@ -56,7 +58,7 @@ public function toString(): string
5658
return sprintf(
5759
'%s string %s',
5860
count($this->matchingElements) >= 2 ? 'contain' : 'contains',
59-
$this->exporter()->export($this->needle)
61+
$this->exportValue($this->needle)
6062
);
6163
}
6264

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

116118
return sprintf(
117119
$label,
118-
$this->exporter()->export($this->selector->getValue()),
120+
$this->exportValue($this->selector->getValue()),
119121
$this->toString()
120122
);
121123
}

src/Constraints/ElementMatchesRegExp.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
class ElementMatchesRegExp extends ElementContainsString
1414
{
15+
use ExporterTrait;
16+
1517
/**
1618
* @var string
1719
*/
@@ -37,7 +39,7 @@ public function toString(): string
3739
return sprintf(
3840
'%s regular expression %s',
3941
count($this->matchingElements) >= 2 ? 'match' : 'matches',
40-
$this->exporter()->export($this->pattern)
42+
$this->exportValue($this->pattern)
4143
);
4244
}
4345

src/Constraints/ExporterTrait.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace SteveGrunwell\PHPUnit_Markup_Assertions\Constraints;
4+
5+
/**
6+
* Trait that exposes an exportValue method that will attempt to use
7+
* {@see SebastianBergmann\Exporter\Exporter} when available.
8+
*/
9+
trait ExporterTrait
10+
{
11+
/**
12+
* Export a value to include it in error messages.
13+
*
14+
* If {@see SebastianBergmann\Exporter\Exporter} is available (as it is in modern versions of
15+
* PHPUnit), then it will be used. Otherwise, we'll do a simple var_export().
16+
*
17+
* @param mixed $value The value to be exported.
18+
*
19+
* @return string A string representation of the value.
20+
*/
21+
private function exportValue($value): string
22+
{
23+
if (method_exists($this, 'exporter')) {
24+
return $this->exporter()->export($value);
25+
}
26+
27+
return var_export($value, true);
28+
}
29+
}

src/Constraints/SelectorCount.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class SelectorCount extends Constraint
1313
{
14+
use ExporterTrait;
15+
1416
/**
1517
* @var int
1618
*/
@@ -41,7 +43,7 @@ public function toString(): string
4143
return sprintf(
4244
'contains %d instance(s) of selector %s',
4345
$this->count,
44-
$this->exporter()->export($this->selector->getValue())
46+
$this->exportValue($this->selector->getValue())
4547
);
4648
}
4749

0 commit comments

Comments
 (0)