Skip to content

Commit efbfef5

Browse files
Multiple assertions per test case #16
1 parent 10ba631 commit efbfef5

17 files changed

+35
-18
lines changed

src/MatchesSnapshots.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@
1010

1111
trait MatchesSnapshots
1212
{
13-
public function assertMatchesSnapshot($actual, Driver $driver = null)
13+
/** @var int */
14+
protected $snapshotIncrementor;
15+
16+
/** @before */
17+
public function setUpSnapshotIncrementor()
1418
{
15-
$snapshot = $this->createSnapshotWithDriver($driver ?? new VarDriver());
19+
$this->snapshotIncrementor = 0;
20+
}
1621

17-
$this->doSnapshotAssertion($snapshot, $actual);
22+
public function assertMatchesSnapshot($actual)
23+
{
24+
$this->doSnapshotAssertion($actual, new VarDriver());
1825
}
1926

2027
public function assertMatchesXmlSnapshot($actual)
2128
{
22-
$this->assertMatchesSnapshot($actual, new XmlDriver());
29+
$this->doSnapshotAssertion($actual, new XmlDriver());
2330
}
2431

2532
public function assertMatchesJsonSnapshot($actual)
2633
{
27-
$this->assertMatchesSnapshot($actual, new JsonDriver());
34+
$this->doSnapshotAssertion($actual, new JsonDriver());
2835
}
2936

3037
/**
@@ -35,7 +42,9 @@ public function assertMatchesJsonSnapshot($actual)
3542
*/
3643
protected function getSnapshotId(): string
3744
{
38-
return (new ReflectionClass($this))->getShortName().'__'.$this->getName();
45+
return (new ReflectionClass($this))->getShortName().'__'.
46+
$this->getName().'__'.
47+
$this->snapshotIncrementor;
3948
}
4049

4150
/**
@@ -66,17 +75,16 @@ protected function shouldUpdateSnapshots(): bool
6675
return in_array('--update-snapshots', $_SERVER['argv'], true);
6776
}
6877

69-
protected function createSnapshotWithDriver(Driver $driver): Snapshot
78+
protected function doSnapshotAssertion($actual, Driver $driver)
7079
{
71-
return Snapshot::forTestCase(
80+
$this->snapshotIncrementor++;
81+
82+
$snapshot = Snapshot::forTestCase(
7283
$this->getSnapshotId(),
7384
$this->getSnapshotDirectory(),
7485
$driver
7586
);
76-
}
7787

78-
protected function doSnapshotAssertion(Snapshot $snapshot, $actual)
79-
{
8088
if (! $snapshot->exists()) {
8189
$snapshot->create($actual);
8290

tests/Integration/AssertionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public function can_match_a_json_snapshot()
4040

4141
$this->assertMatchesJsonSnapshot($data);
4242
}
43+
44+
/** @test */
45+
public function can_do_multiple_snapshot_assertions()
46+
{
47+
$this->assertMatchesSnapshot('Foo');
48+
$this->assertMatchesSnapshot('Bar');
49+
}
4350
}

tests/Integration/MatchesSnapshotTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function it_can_create_a_snapshot_from_a_string()
3434
$mockTrait->assertMatchesSnapshot('Foo');
3535

3636
$this->assertSnapshotMatchesExample(
37-
'MatchesSnapshotTest__it_can_match_an_existing_string_snapshot.php',
37+
'MatchesSnapshotTest__it_can_match_an_existing_string_snapshot__1.php',
3838
'snapshot.php'
3939
);
4040
}
@@ -49,7 +49,7 @@ public function it_can_create_a_snapshot_from_xml()
4949
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
5050

5151
$this->assertSnapshotMatchesExample(
52-
'MatchesSnapshotTest__it_can_create_a_snapshot_from_xml.xml',
52+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_xml__1.xml',
5353
'snapshot.xml'
5454
);
5555
}
@@ -64,7 +64,7 @@ public function it_can_create_a_snapshot_from_json()
6464
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
6565

6666
$this->assertSnapshotMatchesExample(
67-
'MatchesSnapshotTest__it_can_create_a_snapshot_from_json.json',
67+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_json__1.json',
6868
'snapshot.json'
6969
);
7070
}
@@ -135,7 +135,7 @@ public function it_can_update_a_string_snapshot()
135135
$mockTrait->assertMatchesSnapshot('Foo');
136136

137137
$this->assertSnapshotMatchesExample(
138-
'MatchesSnapshotTest__it_can_update_a_string_snapshot.php',
138+
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.php',
139139
'snapshot.php'
140140
);
141141
}
@@ -152,7 +152,7 @@ public function it_can_update_a_xml_snapshot()
152152
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
153153

154154
$this->assertSnapshotMatchesExample(
155-
'MatchesSnapshotTest__it_can_update_a_xml_snapshot.xml',
155+
'MatchesSnapshotTest__it_can_update_a_xml_snapshot__1.xml',
156156
'snapshot.xml'
157157
);
158158
}
@@ -169,7 +169,7 @@ public function it_can_update_a_json_snapshot()
169169
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
170170

171171
$this->assertSnapshotMatchesExample(
172-
'MatchesSnapshotTest__it_can_update_a_json_snapshot.json',
172+
'MatchesSnapshotTest__it_can_update_a_json_snapshot__1.json',
173173
'snapshot.json'
174174
);
175175
}
@@ -201,7 +201,7 @@ protected function getMatchesSnapshotMock(): PHPUnit_Framework_MockObject_MockOb
201201
$matchesSnapshotMock
202202
->expects($this->any())
203203
->method('getSnapshotId')
204-
->willReturn('MatchesSnapshotTest__'.$this->getName());
204+
->willReturn('MatchesSnapshotTest__'.$this->getName().'__1');
205205

206206
$matchesSnapshotMock
207207
->expects($this->any())

0 commit comments

Comments
 (0)