-
Notifications
You must be signed in to change notification settings - Fork 4
/
AutomatedTeardownTest.php
71 lines (63 loc) · 1.59 KB
/
AutomatedTeardownTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
class AutomatedTeardownTest extends PHPUnit_Framework_TestCase
{
/**
* @var array Paths to all files created in the tests.
*/
private $files;
public function testTheSutSumsANumberToTheNumbersInTheFile()
{
$file = $this->createTextFile("1\n2\n3\n");
$sut = new Raiser(10);
$sut->raise($file);
// make your assertions...
}
/**
* Wrapped creation of resources, in this case files
*/
private function createTextFile($content)
{
$file = uniqid('temp') . '.txt';
file_put_contents($file, $content);
$this->files[] = $file;
return $file;
}
/**
* Hook for executing the teardown at the end of each test.
* You can also execute it manually: the automation resides in not having
* to specify where are the resources to clean up.
*/
public function teardown()
{
$this->cleanUpAllFiles();
}
/**
* Automated Teardown implementation
*/
private function cleanUpAllFiles()
{
foreach ($this->files as $filePath) {
unlink($filePath);
}
}
}
class Raiser
{
private $delta;
public function __construct($delta)
{
$this->delta = $delta;
}
public function raise($file)
{
$content = file_get_contents($file);
$lines = explode("\n", $content);
foreach ($lines as $line) {
if (is_numeric($line)) {
$line += $this->delta;
}
}
$content = implode("\n", $lines);
file_put_contents($file, $content);
}
}