Skip to content

Commit 9609fab

Browse files
wip
1 parent 49f8457 commit 9609fab

File tree

4 files changed

+20
-37
lines changed

4 files changed

+20
-37
lines changed

tests/MatchesSnapshots.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ public function assertMatchesSnapshot($serializable)
1010
{
1111
$this->snapshotCount++;
1212

13-
$snapshot = Snapshot::forTestMethod(
13+
$snapshotHandler = SnapshotHandler::forTestMethod(
1414
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1],
1515
$this->snapshotCount
1616
);
1717

18-
if (! $snapshot->exists()) {
19-
$snapshot->create($this->serializeForSnapshot($serializable));
18+
if (! $snapshotHandler->exists()) {
19+
$snapshotHandler->create($this->serializeForSnapshot($serializable));
2020

21-
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
21+
return $this->markTestIncomplete("Snapshot created for {$snapshotHandler->id()}");
2222
}
2323

24-
if ($this->updateSnapshots()) {
25-
$snapshot->update($this->serializeForSnapshot($serializable));
24+
if ($this->shouldUpdateSnapshots()) {
25+
$snapshotHandler->update($this->serializeForSnapshot($serializable));
2626

27-
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
27+
return $this->markTestIncomplete("Snapshot updated for {$snapshotHandler->id()}");
2828
}
2929

30-
return $this->assertEquals($snapshot->get(), $serializable);
30+
return $this->assertEquals($snapshotHandler->get(), $serializable);
3131
}
3232

3333
/** @after **/
@@ -36,27 +36,9 @@ public function resetSnapshotCounter()
3636
$this->snapshotCount = 0;
3737
}
3838

39-
protected function hasSnapshot($path, $id): bool
39+
protected function shouldUpdateSnapshots(): bool
4040
{
41-
if (! file_exists($path)) {
42-
return false;
43-
}
44-
45-
$snapshots = require $path;
46-
47-
return isset($snapshots[$id]);
48-
}
49-
50-
protected function getSnapshot($path, $id)
51-
{
52-
$snapshots = require $path;
53-
54-
return $snapshots[$id];
55-
}
56-
57-
protected function updateOrCreateSnapshot($path, $id, $serializable)
58-
{
59-
41+
return getenv('UPDATE_SNAPSHOTS');
6042
}
6143

6244
protected function serializeForSnapshot($serializable)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use ReflectionClass;
66

7-
class Snapshot
7+
class SnapshotHandler
88
{
99
/** @var string */
1010
protected $directory, $filename, $id;
@@ -18,7 +18,7 @@ private function __construct(string $directory, string $filename, string $id)
1818
$this->filename = $filename;
1919
$this->id = $id;
2020

21-
$this->availableSnapshots = (@include $this->path()) ?: [];
21+
$this->availableSnapshots = @include $this->path() ?? [];
2222
}
2323

2424
public static function forTestMethod($backtrace, $count): self
@@ -27,7 +27,7 @@ public static function forTestMethod($backtrace, $count): self
2727
$method = $backtrace['function'];
2828

2929
$directory = dirname($class->getFileName()).'/__snapshots__';
30-
$filename = $class->getShortName();
30+
$filename = $class->getShortName().'.php';
3131
$id = "{$method} {$count}";
3232

3333
return new self($directory, $filename, $id);
@@ -50,7 +50,7 @@ public function exists(): bool
5050

5151
public function get()
5252
{
53-
$this->availableSnapshots[$this->id];
53+
return $this->availableSnapshots[$this->id];
5454
}
5555

5656
public function create($serializedValue)
@@ -61,7 +61,7 @@ public function create($serializedValue)
6161
mkdir($this->directory);
6262
}
6363

64-
$contents = '<?php\n\n'.print_r($this->availableSnapshots, true);
64+
$contents = '<?php'.PHP_EOL.PHP_EOL.'return '.var_export($this->availableSnapshots, true).';';
6565

6666
file_put_contents($this->path(), $contents);
6767
}

tests/__snapshots__/ExampleTest

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return array (
4+
'true_is_true 1' => 'Foo',
5+
);

0 commit comments

Comments
 (0)