xref: /plugin/struct/_test/StructTest.php (revision 2831817714ad52fdee03e148f5ba86326a3064ab)
1<?php
2
3namespace plugin\struct\test;
4
5use plugin\struct\meta\SchemaData;
6use plugin\struct\meta\SchemaImporter;
7spl_autoload_register(array('action_plugin_struct_autoloader', 'autoloader'));
8
9/**
10 * Base class for all struct tests
11 *
12 * It cleans up the database in teardown and provides some useful helper methods
13 *
14 * @package plugin\struct\test
15 */
16abstract class StructTest extends \DokuWikiTest {
17
18    /** @var array alway enable the needed plugins */
19    protected $pluginsEnabled = array('struct', 'sqlite');
20
21    /**
22     * Default teardown
23     *
24     * we always make sure the database is clear
25     */
26    protected function tearDown() {
27        parent::tearDown();
28        /** @var \helper_plugin_struct_db $sqlite */
29        $sqlite = plugin_load('helper', 'struct_db');
30        $sqlite->resetDB();
31    }
32
33    /**
34     * Creates a schema from one of the available schema files
35     *
36     * @param string $schema
37     * @param string $json base name of the JSON file optional, defaults to $schema
38     */
39    protected function loadSchemaJSON($schema, $json='') {
40        if(!$json) $json = $schema;
41        $file = __DIR__ . "/json/$json.struct.json";
42        if(!file_exists($file)) {
43            throw new \RuntimeException("$file does not exist");
44        }
45
46        $importer = new SchemaImporter($schema, file_get_contents($file));
47
48        if(!$importer->build()) {
49            throw new \RuntimeException("build of $schema from $file failed");
50        }
51    }
52
53    /**
54     * This waits until a new second has passed
55     *
56     * The very first call will return immeadiately, proceeding calls will return
57     * only after at least 1 second after the last call has passed
58     *
59     * @return int new timestamp
60     */
61    protected function waitForTick() {
62        static $last = 0;
63        while ( $last === $now = time() ) {
64            usleep(100000); //recheck in a 10th of a second
65        }
66        $last = $now;
67        return $now;
68    }
69
70    /**
71     * Saves struct data for given page and schema
72     *
73     * @param string $page
74     * @param string $schema
75     * @param array $data
76     */
77    protected function saveData($page, $schema, $data) {
78        saveWikiText($page, "test for $page", "saved for testing");
79        $schemaData = new SchemaData($schema, $page, time());
80        $schemaData->saveData($data);
81    }
82}
83