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