Skip to content

Commit b04e87f

Browse files
committed
feat(src) show diff by default on failure
1 parent 6b4ebd2 commit b04e87f

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ name: CI
33
on: push
44

55
jobs:
6-
sniff;
6+
sniff:
77
name: Code validation
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@master
11-
- uses: MilesChou/composer-action/install@master
11+
- uses: MilesChou/composer-action/7.3/install@master
12+
with:
13+
args: require --dev phpstan/phpstan
1214
- uses: docker://php:7.3-alpine
1315
with:
1416
args: vendor/bin/phpcs --colors -p --standard=PSR2 -s src
1517
- uses: docker://php:7.3-alpine
1618
with:
17-
args: phpstan analyze src --level=7
19+
args: vendor/bin/phpstan analyze src --level=7
1820
php56:
1921
name: Test on PHP 5.6
2022
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.3.0] - 2023-09-08
10+
### Changed
11+
- show diff on failure by default
12+
913
## [0.2.4] - 2020-02-05
1014
### Added
1115
- data visitor support in all assertions to allow pre-processing of the expected and current values before assertions

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"codeception/codeception": "^2.5 || ^3.0 || ^4.0"
1616
},
1717
"require-dev": {
18-
"php": ">=7.1",
19-
"squizlabs/php_codesniffer": "*",
20-
"phpstan/phpstan": "^0.12.9"
18+
"squizlabs/php_codesniffer": "*"
2119
},
2220
"autoload": {
2321
"psr-4": {

src/AbstractSnapshot.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AbstractSnapshot extends Snapshot
2424
/**
2525
* Keeps a counter for each class, function and data-set combination.
2626
*
27-
* @var array<string,array>
27+
* @var array<string,array<string,int>>
2828
*/
2929
protected static $counters = [];
3030

@@ -49,6 +49,13 @@ class AbstractSnapshot extends Snapshot
4949
*/
5050
protected $dataVisitor;
5151

52+
/**
53+
* By default, show the diff on failure.
54+
*
55+
* @var bool
56+
*/
57+
protected $showDiff = true;
58+
5259
/**
5360
* Snapshot constructor.
5461
*
@@ -84,14 +91,19 @@ protected function getFileName()
8491
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS
8592
| DEBUG_BACKTRACE_PROVIDE_OBJECT, 5),
8693
static function (array $backtraceEntry) use ($traitMethods) {
87-
return !in_array(
94+
return isset($backtraceEntry['class']) && !in_array(
8895
$backtraceEntry['class'],
8996
[Snapshot::class, static::class, self::class, SnapshotAssertions::class],
9097
true
9198
) && !in_array($backtraceEntry['function'], $traitMethods, true);
9299
}
93100
));
94-
$class = $backtrace[0]['class'];
101+
$class = isset($backtrace[0]['class']) ? $backtrace[0]['class'] : '';
102+
103+
if (empty($class)) {
104+
throw new \RuntimeException('Cannot get ithe class name.');
105+
}
106+
95107
$classFrags = explode('\\', $class);
96108
$classBasename = array_pop($classFrags);
97109
$classFile = (new \ReflectionClass($class))->getFileName();
@@ -103,7 +115,7 @@ static function (array $backtraceEntry) use ($traitMethods) {
103115
$classDir = dirname($classFile);
104116
$function = $backtrace[0]['function'];
105117
$dataSetFrag = '';
106-
if ($backtrace[0]['object'] instanceof TestCase) {
118+
if (isset($backtrace[0]['object']) && $backtrace[0]['object'] instanceof TestCase) {
107119
/** @var TestCase $testCase */
108120
$testCase = $backtrace[0]['object'];
109121
$dataName = $this->getDataName($testCase);
@@ -268,6 +280,10 @@ public function assert()
268280
return;
269281
}
270282

283+
if ($this->showDiff) {
284+
throw $exception;
285+
}
286+
271287
$this->fail($exception->getMessage());
272288
}
273289
}

src/DirectorySnapshot.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function fetchData()
3030
*
3131
* @param string $dir The directory to build the iterator for.
3232
*
33-
* @return \RecursiveIteratorIterator The recursive directory iterator, built on the specified directory.
33+
* @return \RecursiveIteratorIterator<\RecursiveDirectoryIterator> The iterator, built on the specified directory.
3434
*/
3535
protected function buildIterator($dir)
3636
{
@@ -125,6 +125,7 @@ protected function assertData($data)
125125
$sortedFiles
126126
);
127127

128+
// @phpstan-ignore-next-line
128129
uksort($sortedFiles, 'strcasecmp');
129130
$multiIterator->attachIterator(new \ArrayIterator($sortedFiles));
130131

0 commit comments

Comments
 (0)