]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Updater; | |
3 | ||
4 | use Exception; | |
5 | use Shaarli\Config\ConfigManager; | |
6 | ||
7 | require_once 'tests/updater/DummyUpdater.php'; | |
8 | require_once 'tests/utils/ReferenceLinkDB.php'; | |
9 | require_once 'inc/rain.tpl.class.php'; | |
10 | ||
11 | /** | |
12 | * Class UpdaterTest. | |
13 | * Runs unit tests against the updater class. | |
14 | */ | |
15 | class UpdaterTest extends \PHPUnit\Framework\TestCase | |
16 | { | |
17 | /** | |
18 | * @var string Path to test datastore. | |
19 | */ | |
20 | protected static $testDatastore = 'sandbox/datastore.php'; | |
21 | ||
22 | /** | |
23 | * @var string Config file path (without extension). | |
24 | */ | |
25 | protected static $configFile = 'sandbox/config'; | |
26 | ||
27 | /** | |
28 | * @var ConfigManager | |
29 | */ | |
30 | protected $conf; | |
31 | ||
32 | /** | |
33 | * Executed before each test. | |
34 | */ | |
35 | public function setUp() | |
36 | { | |
37 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); | |
38 | $this->conf = new ConfigManager(self::$configFile); | |
39 | } | |
40 | ||
41 | /** | |
42 | * Test UpdaterUtils::read_updates_file with an empty/missing file. | |
43 | */ | |
44 | public function testReadEmptyUpdatesFile() | |
45 | { | |
46 | $this->assertEquals(array(), UpdaterUtils::read_updates_file('')); | |
47 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
48 | touch($updatesFile); | |
49 | $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile)); | |
50 | unlink($updatesFile); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Test read/write updates file. | |
55 | */ | |
56 | public function testReadWriteUpdatesFile() | |
57 | { | |
58 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
59 | $updatesMethods = array('m1', 'm2', 'm3'); | |
60 | ||
61 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); | |
62 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | |
63 | $this->assertEquals($readMethods, $updatesMethods); | |
64 | ||
65 | // Update | |
66 | $updatesMethods[] = 'm4'; | |
67 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); | |
68 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | |
69 | $this->assertEquals($readMethods, $updatesMethods); | |
70 | unlink($updatesFile); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. | |
75 | * | |
76 | * @expectedException Exception | |
77 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | |
78 | */ | |
79 | public function testWriteEmptyUpdatesFile() | |
80 | { | |
81 | UpdaterUtils::write_updates_file('', array('test')); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. | |
86 | * | |
87 | * @expectedException Exception | |
88 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | |
89 | */ | |
90 | public function testWriteUpdatesFileNotWritable() | |
91 | { | |
92 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
93 | touch($updatesFile); | |
94 | chmod($updatesFile, 0444); | |
95 | try { | |
96 | @UpdaterUtils::write_updates_file($updatesFile, array('test')); | |
97 | } catch (Exception $e) { | |
98 | unlink($updatesFile); | |
99 | throw $e; | |
100 | } | |
101 | } | |
102 | ||
103 | /** | |
104 | * Test the update() method, with no update to run. | |
105 | * 1. Everything already run. | |
106 | * 2. User is logged out. | |
107 | */ | |
108 | public function testNoUpdates() | |
109 | { | |
110 | $updates = array( | |
111 | 'updateMethodDummy1', | |
112 | 'updateMethodDummy2', | |
113 | 'updateMethodDummy3', | |
114 | 'updateMethodException', | |
115 | ); | |
116 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
117 | $this->assertEquals(array(), $updater->update()); | |
118 | ||
119 | $updater = new DummyUpdater(array(), array(), $this->conf, false); | |
120 | $this->assertEquals(array(), $updater->update()); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Test the update() method, with all updates to run (except the failing one). | |
125 | */ | |
126 | public function testUpdatesFirstTime() | |
127 | { | |
128 | $updates = array('updateMethodException',); | |
129 | $expectedUpdates = array( | |
130 | 'updateMethodDummy1', | |
131 | 'updateMethodDummy2', | |
132 | 'updateMethodDummy3', | |
133 | ); | |
134 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
135 | $this->assertEquals($expectedUpdates, $updater->update()); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Test the update() method, only one update to run. | |
140 | */ | |
141 | public function testOneUpdate() | |
142 | { | |
143 | $updates = array( | |
144 | 'updateMethodDummy1', | |
145 | 'updateMethodDummy3', | |
146 | 'updateMethodException', | |
147 | ); | |
148 | $expectedUpdate = array('updateMethodDummy2'); | |
149 | ||
150 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
151 | $this->assertEquals($expectedUpdate, $updater->update()); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Test Update failed. | |
156 | * | |
157 | * @expectedException \Exception | |
158 | */ | |
159 | public function testUpdateFailed() | |
160 | { | |
161 | $updates = array( | |
162 | 'updateMethodDummy1', | |
163 | 'updateMethodDummy2', | |
164 | 'updateMethodDummy3', | |
165 | ); | |
166 | ||
167 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
168 | $updater->update(); | |
169 | } | |
170 | } |