summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_service_storage.php
blob: 83284ea4bc9218302fe2047119fb4b34acbbf4ed (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
 * Copyright 2016 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests;

class test_service_storage extends \test\Test {

	private $tempdir;

	public function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
		$this->tempdir = trim((new \libraries\ProcRunner(['mktemp', '-d']))->execSafe()['stdout']);
	}

	public function cleanup()
	{
		rmdir($this->tempdir);
	}

	public function test_normalCase()
	{
		$file = $this->tempdir.'/testfile1';
		$storage = new \service\storage($file);

		$this->t->is($storage->get_file(), $file, "get_file returns correct path");

		$a = $storage->begin();
		file_put_contents($a, "teststring1");
		$this->t->is(file_exists($file), false, "Test file doesn't exist yet");
		$this->t->is($storage->exists(), false, "Test file doesn't exist yet");

		$storage->commit();
		$this->t->is(file_exists($file), true, "Test file has been created");
		$this->t->is(file_get_contents($file), 'teststring1', "Test file has correct content");

		unlink($file);
	}

	public function test_existingFile()
	{
		$file = $this->tempdir.'/testfile-existing-file';
		file_put_contents($file, "teststring-old");

		$storage = new \service\storage($file);

		$a = $storage->begin();
		file_put_contents($a, "teststring-changed");
		$this->t->is(file_exists($file), true, "Test file already exists");
		$this->t->is(file_get_contents($file), 'teststring-old', "Test file has old content");

		$storage->commit();
		$this->t->is(file_exists($file), true, "Test file still exists");
		$this->t->is(file_get_contents($file), 'teststring-changed', "Test file has updated content");

		unlink($file);
	}

	public function test_rollback()
	{
		$file = $this->tempdir.'/testfile-rollback';
		file_put_contents($file, "teststring-old");

		$storage = new \service\storage($file);

		$a = $storage->begin();
		file_put_contents($a, "teststring-changed");
		$this->t->is(file_exists($file), true, "Test file already exists");
		$this->t->is(file_get_contents($file), 'teststring-old', "Test file has old content");

		$storage->rollback();
		$this->t->is(file_exists($file), true, "Test file still exists");
		$this->t->is(file_get_contents($file), 'teststring-old', "Test file still has old content");

		unlink($file);
	}

	public function test_gzip_compress()
	{
		$file = $this->tempdir.'/testfile-gzip';
		file_put_contents($file, "teststring-old");

		$storage = new \service\storage($file);

		$a = $storage->begin();
		$new_string = str_repeat("teststring-changed", 500);
		file_put_contents($a, $new_string);

		$ret = $storage->gzip_compress();
		$this->t->is($ret, true, "Compression succeeded");

		$this->t->is(file_exists($file), true, "Test file still exists");
		$this->t->is(file_get_contents($file), 'teststring-old', "Test file still has old content");

		$storage->commit();

		ob_start();
		readgzfile($file);
		$file_content = ob_get_clean();

		$this->t->is_deeply($new_string, $file_content, "File is compressed and has correct content");

		unlink($file);
	}

	public function test_unlink()
	{
		$file = $this->tempdir.'/testfile-unlink';
		file_put_contents($file, "teststring-old");

		$storage = new \service\storage($file);
		$this->t->is(file_exists($file), true, "Test file exists");
		$storage->unlink();
		$this->t->is(file_exists($file), false, "Test file has been removed");
	}

	public function test_unlink_missingFile()
	{
		$file = $this->tempdir.'/testfile-unlink';

		$storage = new \service\storage($file);
		$this->t->is(file_exists($file), false, "Test file does nto exist");
		$storage->unlink();
		$this->t->is(file_exists($file), false, "Test file still doesn't exist");
	}

	public function test_begin_calledMultipleTimes()
	{
		$file = $this->tempdir.'/testfile-begin-multi';
		file_put_contents($file, "teststring-old");

		$storage = new \service\storage($file);
		$a = $storage->begin();
		file_put_contents($a, "blub");

		$b = $storage->begin();
		file_put_contents($b, "second write");

		$storage->commit();
		$this->t->is(file_get_contents($file), "second write", "File contains second write");

		unlink($file);
	}

	public function test_begin_creationOfDir()
	{
		$dir = $this->tempdir.'/testdir/';
		$file = $dir.'testfile';
		$storage = new \service\storage($file);

		$this->t->is(is_dir($dir), false, "Directory does not exist");

		$a = $storage->begin();

		$this->t->is(is_dir($dir), true, "Directory exists");

		$storage->rollback();
		rmdir($dir);
	}

}