list('p1') === []); $doc = ['revision' => 4, 'project' => ['id' => 'p1', 'name' => 'Game', 'assets' => [['id' => 'a']]]]; $first = $snapshots->create('p1', $doc, 'first'); $second = $snapshots->create('p1', ['revision' => 5] + $doc, 'second'); check('a snapshot keeps its name and the revision it copied', $first['name'] === 'first' && $first['revision'] === 4, $first); check('two snapshots get two ids', $first['id'] !== $second['id']); check('the summary carries no project', !isset($first['project'])); check('the snapshot file is not readable for the web server', (fileperms("{$dir}/p1/{$first['id']}.json") & 0o007) === 0); $listed = $snapshots->list('p1'); check( 'the list is newest first, also within one second', array_column($listed, 'name') === ['second', 'first'], array_column($listed, 'name'), ); // The ids are random, so two files come back from the folder in the right order half the time // anyway. With eight, the folder's own order matching by chance is one in 40320. $more = new SnapshotStore("{$dir}/order"); foreach (range(1, 8) as $n) { $more->create('p', $doc, "n{$n}"); } check( 'eight made in a row list in reverse order of making', array_column($more->list('p'), 'name') === ['n8', 'n7', 'n6', 'n5', 'n4', 'n3', 'n2', 'n1'], array_column($more->list('p'), 'name'), ); $loaded = $snapshots->load('p1', $first['id']); check('a snapshot loads the project as it was copied', $loaded !== null && $loaded['project'] === $doc['project'], $loaded); check('a snapshot of another project is not found', $snapshots->load('p2', $first['id']) === null); file_put_contents("{$dir}/p1/broken.json", '{"id":"broken"'); check('a broken file is left out of the list', count($snapshots->list('p1')) === 2, $snapshots->list('p1')); unlink("{$dir}/p1/broken.json"); check('deleting a snapshot says so', $snapshots->delete('p1', $first['id'])); check('it is gone from the list', array_column($snapshots->list('p1'), 'name') === ['second']); check('deleting it again says it was not there', !$snapshots->delete('p1', $first['id'])); $snapshots->create('p2', $doc, 'other'); $snapshots->deleteAll('p1'); check('deleting all removes the folder of that project', !is_dir("{$dir}/p1")); check('and leaves the snapshots of other projects', count($snapshots->list('p2')) === 1); exec('rm -rf ' . escapeshellarg($dir)); echo $failed === 0 ? "ALL PASSED – {$passed} checks\n" : "{$failed} FAILED, {$passed} passed\n"; exit($failed === 0 ? 0 : 1);