1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Tests\Snapshotter\FileHandler\Services; |
4
|
|
|
|
5
|
|
|
use Spiral\Snapshotter\FileHandler\Entities\FileSnapshot; |
6
|
|
|
use Spiral\Snapshotter\FileHandler\Services\SnapshotService; |
7
|
|
|
use Spiral\Tests\BaseTest; |
8
|
|
|
|
9
|
|
|
class SnapshotServiceTest extends BaseTest |
10
|
|
|
{ |
11
|
|
View Code Duplication |
public function testGetSnapshots() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
14
|
|
|
$this->handleFileSnapshot($snapshot); |
15
|
|
|
|
16
|
|
|
/** @var SnapshotService $service */ |
17
|
|
|
$service = $this->container->get(SnapshotService::class); |
18
|
|
|
|
19
|
|
|
$this->assertCount(1, $service->getSnapshots()); |
20
|
|
|
|
21
|
|
|
sleep(1); |
22
|
|
|
$snapshot = $this->makeSnapshot('Message2', 456); |
23
|
|
|
$this->handleFileSnapshot($snapshot); |
24
|
|
|
|
25
|
|
|
$this->assertCount(2, $service->getSnapshots()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetSnapshot() |
29
|
|
|
{ |
30
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
31
|
|
|
$this->handleFileSnapshot($snapshot); |
32
|
|
|
|
33
|
|
|
/** @var SnapshotService $service */ |
34
|
|
|
$service = $this->container->get(SnapshotService::class); |
35
|
|
|
|
36
|
|
|
/** @var FileSnapshot $file */ |
37
|
|
|
$file = current($service->getSnapshots()->iterate()); |
38
|
|
|
$this->assertInstanceOf(FileSnapshot::class, $file); |
39
|
|
|
$filename = $file->id(); |
40
|
|
|
|
41
|
|
|
$this->assertNotEmpty($service->getSnapshot($filename)); |
42
|
|
|
$this->assertInstanceOf(FileSnapshot::class, $service->getSnapshot($filename)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testRead() |
46
|
|
|
{ |
47
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
48
|
|
|
$this->handleFileSnapshot($snapshot); |
49
|
|
|
|
50
|
|
|
/** @var SnapshotService $service */ |
51
|
|
|
$service = $this->container->get(SnapshotService::class); |
52
|
|
|
|
53
|
|
|
/** @var FileSnapshot $file */ |
54
|
|
|
$file = current($service->getSnapshots()->iterate()); |
55
|
|
|
|
56
|
|
|
$this->assertNotEmpty($service->read($file)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExists() |
60
|
|
|
{ |
61
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
62
|
|
|
$this->handleFileSnapshot($snapshot); |
63
|
|
|
|
64
|
|
|
/** @var SnapshotService $service */ |
65
|
|
|
$service = $this->container->get(SnapshotService::class); |
66
|
|
|
|
67
|
|
|
/** @var FileSnapshot $file */ |
68
|
|
|
$file = current($service->getSnapshots()->iterate()); |
69
|
|
|
|
70
|
|
|
$this->assertTrue($service->exists($file->id())); |
71
|
|
|
$this->assertFalse($service->exists('some name')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testDelete() |
75
|
|
|
{ |
76
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
77
|
|
|
$this->handleFileSnapshot($snapshot); |
78
|
|
|
|
79
|
|
|
/** @var SnapshotService $service */ |
80
|
|
|
$service = $this->container->get(SnapshotService::class); |
81
|
|
|
|
82
|
|
|
/** @var FileSnapshot $file */ |
83
|
|
|
$file = current($service->getSnapshots()->iterate()); |
84
|
|
|
|
85
|
|
|
$this->assertTrue($service->exists($file->id())); |
86
|
|
|
|
87
|
|
|
$service->deleteSnapshot($file); |
88
|
|
|
$this->assertFalse($service->exists($file->id())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function testDeleteSnapshots() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$snapshot = $this->makeSnapshot('Message', 123); |
94
|
|
|
$this->handleFileSnapshot($snapshot); |
95
|
|
|
|
96
|
|
|
sleep(1); |
97
|
|
|
$snapshot = $this->makeSnapshot('Message2', 456); |
98
|
|
|
$this->handleFileSnapshot($snapshot); |
99
|
|
|
|
100
|
|
|
/** @var SnapshotService $service */ |
101
|
|
|
$service = $this->container->get(SnapshotService::class); |
102
|
|
|
|
103
|
|
|
$this->assertCount(2, $service->getSnapshots()); |
104
|
|
|
|
105
|
|
|
$service->deleteSnapshots(); |
106
|
|
|
|
107
|
|
|
$this->assertCount(0, $service->getSnapshots()); |
108
|
|
|
} |
109
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.