Skip to content

Commit 0e7fb7b

Browse files
Stephan Wentzpl-github
authored andcommitted
feat: Allow disable create snapshots
1 parent 62d1e92 commit 0e7fb7b

File tree

7 files changed

+410
-0
lines changed

7 files changed

+410
-0
lines changed

src/Snapshot/SnapshotTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Filesystem\Filesystem;
1010
use tidy;
1111

12+
use function basename;
1213
use function dirname;
1314
use function getenv;
1415
use function is_string;
@@ -309,6 +310,13 @@ private function snapshotDump(string $fixtureFilename, string $string): void
309310
return;
310311
}
311312

313+
if (
314+
(getenv('CREATE_SNAPSHOTS') === '0' || getenv('CREATE_SNAPSHOTS') === 'false') &&
315+
!$filesystem->exists($fixtureFilename)
316+
) {
317+
self::fail('Snapshot ' . basename($fixtureFilename) . ' does not exist.');
318+
}
319+
312320
$filesystem->mkdir(dirname($fixtureFilename));
313321
$filesystem->dumpFile($fixtureFilename, $string);
314322
}

tests/Snapshot/ArraySnapshotTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait;
88
use org\bovigo\vfs\vfsStream;
99
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\AssertionFailedError;
1011
use PHPUnit\Framework\ExpectationFailedException;
1112
use PHPUnit\Framework\TestCase;
1213

14+
use function getenv;
1315
use function Safe\file_put_contents;
1416
use function Safe\json_decode;
17+
use function Safe\putenv;
1518

1619
/** @covers \Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait */
1720
final class ArraySnapshotTest extends TestCase
@@ -79,6 +82,70 @@ public function testNamedArrayAssertionFails(): void
7982
$this->fail('Assertion did not fail');
8083
}
8184

85+
public function testSnapshotsAreNotCreatedForCreateSnapshotFalse(): void
86+
{
87+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
88+
89+
$prior = getenv('CREATE_SNAPSHOTS');
90+
putenv('CREATE_SNAPSHOTS=false');
91+
92+
$thrownException = null;
93+
94+
try {
95+
$this->assertMatchesXmlSnapshot($data);
96+
} catch (AssertionFailedError $e) {
97+
$thrownException = $e;
98+
} finally {
99+
putenv('CREATE_SNAPSHOTS=' . $prior);
100+
}
101+
102+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
103+
$this->assertSame(
104+
'Snapshot array_snapshot_snapshots_are_not_created_for_create_snapshot_false.xml does not exist.',
105+
$thrownException->getMessage(),
106+
);
107+
}
108+
109+
public function testSnapshotsAreNotCreatedForCreateSnapshotZero(): void
110+
{
111+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
112+
113+
$prior = getenv('CREATE_SNAPSHOTS');
114+
putenv('CREATE_SNAPSHOTS=0');
115+
116+
$thrownException = null;
117+
118+
try {
119+
$this->assertMatchesXmlSnapshot($data);
120+
} catch (AssertionFailedError $e) {
121+
$thrownException = $e;
122+
} finally {
123+
putenv('CREATE_SNAPSHOTS=' . $prior);
124+
}
125+
126+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
127+
$this->assertSame(
128+
'Snapshot array_snapshot_snapshots_are_not_created_for_create_snapshot_zero.xml does not exist.',
129+
$thrownException->getMessage(),
130+
);
131+
}
132+
133+
public function testCreateSnapshotEnvIsNotProcessedForOtherValue(): void
134+
{
135+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
136+
137+
$prior = getenv('CREATE_SNAPSHOTS');
138+
putenv('CREATE_SNAPSHOTS=true');
139+
140+
try {
141+
$this->assertMatchesXmlSnapshot($data);
142+
} catch (AssertionFailedError) {
143+
$this->fail('Unexpected fail() was called');
144+
} finally {
145+
putenv('CREATE_SNAPSHOTS=' . $prior);
146+
}
147+
}
148+
82149
/**
83150
* Overwrite function in using class to locate __snapshot__ directory correctly.
84151
*/

tests/Snapshot/HtmlSnapshotTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait;
88
use org\bovigo\vfs\vfsStream;
99
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\AssertionFailedError;
1011
use PHPUnit\Framework\ExpectationFailedException;
1112
use PHPUnit\Framework\TestCase;
1213

14+
use function getenv;
1315
use function Safe\file_put_contents;
16+
use function Safe\putenv;
1417

1518
/** @covers \Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait */
1619
final class HtmlSnapshotTest extends TestCase
@@ -78,6 +81,70 @@ public function testNamedHtmlAssertionFails(): void
7881
$this->fail('Assertion did not fail');
7982
}
8083

84+
public function testSnapshotsAreNotCreatedForCreateSnapshotFalse(): void
85+
{
86+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
87+
88+
$prior = getenv('CREATE_SNAPSHOTS');
89+
putenv('CREATE_SNAPSHOTS=false');
90+
91+
$thrownException = null;
92+
93+
try {
94+
$this->assertMatchesXmlSnapshot($data);
95+
} catch (AssertionFailedError $e) {
96+
$thrownException = $e;
97+
} finally {
98+
putenv('CREATE_SNAPSHOTS=' . $prior);
99+
}
100+
101+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
102+
$this->assertSame(
103+
'Snapshot html_snapshot_snapshots_are_not_created_for_create_snapshot_false.xml does not exist.',
104+
$thrownException->getMessage(),
105+
);
106+
}
107+
108+
public function testSnapshotsAreNotCreatedForCreateSnapshotZero(): void
109+
{
110+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
111+
112+
$prior = getenv('CREATE_SNAPSHOTS');
113+
putenv('CREATE_SNAPSHOTS=0');
114+
115+
$thrownException = null;
116+
117+
try {
118+
$this->assertMatchesXmlSnapshot($data);
119+
} catch (AssertionFailedError $e) {
120+
$thrownException = $e;
121+
} finally {
122+
putenv('CREATE_SNAPSHOTS=' . $prior);
123+
}
124+
125+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
126+
$this->assertSame(
127+
'Snapshot html_snapshot_snapshots_are_not_created_for_create_snapshot_zero.xml does not exist.',
128+
$thrownException->getMessage(),
129+
);
130+
}
131+
132+
public function testCreateSnapshotEnvIsNotProcessedForOtherValue(): void
133+
{
134+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
135+
136+
$prior = getenv('CREATE_SNAPSHOTS');
137+
putenv('CREATE_SNAPSHOTS=true');
138+
139+
try {
140+
$this->assertMatchesXmlSnapshot($data);
141+
} catch (AssertionFailedError) {
142+
$this->fail('Unexpected fail() was called');
143+
} finally {
144+
putenv('CREATE_SNAPSHOTS=' . $prior);
145+
}
146+
}
147+
81148
/**
82149
* Overwrite function in using class to locate __snapshot__ directory correctly.
83150
*/

tests/Snapshot/JsonLdSnapshotTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait;
88
use org\bovigo\vfs\vfsStream;
99
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\AssertionFailedError;
1011
use PHPUnit\Framework\ExpectationFailedException;
1112
use PHPUnit\Framework\TestCase;
1213

14+
use function getenv;
1315
use function Safe\file_get_contents;
1416
use function Safe\file_put_contents;
17+
use function Safe\putenv;
1518

1619
/** @covers \Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait */
1720
final class JsonLdSnapshotTest extends TestCase
@@ -199,6 +202,70 @@ public function testNamedJsonLdAssertionFails(): void
199202
$this->fail('Assertion did not fail');
200203
}
201204

205+
public function testSnapshotsAreNotCreatedForCreateSnapshotFalse(): void
206+
{
207+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
208+
209+
$prior = getenv('CREATE_SNAPSHOTS');
210+
putenv('CREATE_SNAPSHOTS=false');
211+
212+
$thrownException = null;
213+
214+
try {
215+
$this->assertMatchesXmlSnapshot($data);
216+
} catch (AssertionFailedError $e) {
217+
$thrownException = $e;
218+
} finally {
219+
putenv('CREATE_SNAPSHOTS=' . $prior);
220+
}
221+
222+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
223+
$this->assertSame(
224+
'Snapshot json_ld_snapshot_snapshots_are_not_created_for_create_snapshot_false.xml does not exist.',
225+
$thrownException->getMessage(),
226+
);
227+
}
228+
229+
public function testSnapshotsAreNotCreatedForCreateSnapshotZero(): void
230+
{
231+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
232+
233+
$prior = getenv('CREATE_SNAPSHOTS');
234+
putenv('CREATE_SNAPSHOTS=0');
235+
236+
$thrownException = null;
237+
238+
try {
239+
$this->assertMatchesXmlSnapshot($data);
240+
} catch (AssertionFailedError $e) {
241+
$thrownException = $e;
242+
} finally {
243+
putenv('CREATE_SNAPSHOTS=' . $prior);
244+
}
245+
246+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
247+
$this->assertSame(
248+
'Snapshot json_ld_snapshot_snapshots_are_not_created_for_create_snapshot_zero.xml does not exist.',
249+
$thrownException->getMessage(),
250+
);
251+
}
252+
253+
public function testCreateSnapshotEnvIsNotProcessedForOtherValue(): void
254+
{
255+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
256+
257+
$prior = getenv('CREATE_SNAPSHOTS');
258+
putenv('CREATE_SNAPSHOTS=true');
259+
260+
try {
261+
$this->assertMatchesXmlSnapshot($data);
262+
} catch (AssertionFailedError) {
263+
$this->fail('Unexpected fail() was called');
264+
} finally {
265+
putenv('CREATE_SNAPSHOTS=' . $prior);
266+
}
267+
}
268+
202269
/**
203270
* Overwrite function in using class to locate __snapshot__ directory correctly.
204271
*/

tests/Snapshot/JsonSnapshotTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait;
88
use org\bovigo\vfs\vfsStream;
99
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\AssertionFailedError;
1011
use PHPUnit\Framework\ExpectationFailedException;
1112
use PHPUnit\Framework\TestCase;
1213

14+
use function getenv;
1315
use function Safe\file_put_contents;
16+
use function Safe\putenv;
1417

1518
/** @covers \Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait */
1619
final class JsonSnapshotTest extends TestCase
@@ -91,6 +94,70 @@ public function testNamedJsonAssertionFails(): void
9194
$this->fail('Assertion did not fail');
9295
}
9396

97+
public function testSnapshotsAreNotCreatedForCreateSnapshotFalse(): void
98+
{
99+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
100+
101+
$prior = getenv('CREATE_SNAPSHOTS');
102+
putenv('CREATE_SNAPSHOTS=false');
103+
104+
$thrownException = null;
105+
106+
try {
107+
$this->assertMatchesXmlSnapshot($data);
108+
} catch (AssertionFailedError $e) {
109+
$thrownException = $e;
110+
} finally {
111+
putenv('CREATE_SNAPSHOTS=' . $prior);
112+
}
113+
114+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
115+
$this->assertSame(
116+
'Snapshot json_snapshot_snapshots_are_not_created_for_create_snapshot_false.xml does not exist.',
117+
$thrownException->getMessage(),
118+
);
119+
}
120+
121+
public function testSnapshotsAreNotCreatedForCreateSnapshotZero(): void
122+
{
123+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
124+
125+
$prior = getenv('CREATE_SNAPSHOTS');
126+
putenv('CREATE_SNAPSHOTS=0');
127+
128+
$thrownException = null;
129+
130+
try {
131+
$this->assertMatchesXmlSnapshot($data);
132+
} catch (AssertionFailedError $e) {
133+
$thrownException = $e;
134+
} finally {
135+
putenv('CREATE_SNAPSHOTS=' . $prior);
136+
}
137+
138+
$this->assertInstanceOf(AssertionFailedError::class, $thrownException, 'Snapshot test did not fail.');
139+
$this->assertSame(
140+
'Snapshot json_snapshot_snapshots_are_not_created_for_create_snapshot_zero.xml does not exist.',
141+
$thrownException->getMessage(),
142+
);
143+
}
144+
145+
public function testCreateSnapshotEnvIsNotProcessedForOtherValue(): void
146+
{
147+
$data = '<foo><bar>1</bar><baz>test</baz></foo>';
148+
149+
$prior = getenv('CREATE_SNAPSHOTS');
150+
putenv('CREATE_SNAPSHOTS=true');
151+
152+
try {
153+
$this->assertMatchesXmlSnapshot($data);
154+
} catch (AssertionFailedError) {
155+
$this->fail('Unexpected fail() was called');
156+
} finally {
157+
putenv('CREATE_SNAPSHOTS=' . $prior);
158+
}
159+
}
160+
94161
/**
95162
* Overwrite function in using class to locate __snapshot__ directory correctly.
96163
*/

0 commit comments

Comments
 (0)