1<?php
2
3require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Constants.php"));
4require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "ByteBuffer.php"));
5require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "FlatbufferBuilder.php"));
6require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Table.php"));
7require join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), "php", "Struct.php"));
8
9require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Attacker.php'));
10require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'BookReader.php'));
11require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Character.php'));
12require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "php", 'Movie.php'));
13
14class Assert {
15    public function ok($result, $message = "") {
16        if (!$result){
17            throw new Exception(!empty($message) ? $message : "{$result} is not true.");
18        }
19    }
20
21    public function Equal($result, $expected, $message = "") {
22        if ($result != $expected) {
23            throw new Exception(!empty($message) ? $message : "given the result {$result} is not equals as {$expected}");
24        }
25    }
26
27
28    public function strictEqual($result, $expected, $message = "") {
29        if ($result !== $expected) {
30            throw new Exception(!empty($message) ? $message : "given the result {$result} is not strict equals as {$expected}");
31        }
32    }
33
34    public function Throws($class, Callable $callback) {
35        try {
36            $callback();
37
38            throw new \Exception("passed statement don't throw an exception.");
39        } catch (\Exception $e) {
40            if (get_class($e) != get_class($class)) {
41                throw new Exception("passed statement doesn't throw " . get_class($class) . ". throwws " . get_class($e));
42            }
43        }
44    }
45}
46
47function main()
48{
49    $assert = new Assert();
50
51    $fbb = new Google\FlatBuffers\FlatBufferBuilder(1);
52
53    $charTypes = [
54        Character::Belle,
55        Character::MuLan,
56        Character::BookFan,
57    ];
58
59    Attacker::startAttacker($fbb);
60    Attacker::addSwordAttackDamage($fbb, 5);
61    $attackerOffset = Attacker::endAttacker($fbb);
62
63    $charTypesOffset = Movie::createCharactersTypeVector($fbb, $charTypes);
64    $charsOffset = Movie::createCharactersVector(
65        $fbb,
66        [
67            BookReader::createBookReader($fbb, 7),
68            $attackerOffset,
69            BookReader::createBookReader($fbb, 2),
70        ]
71    );
72
73    Movie::startMovie($fbb);
74    Movie::addCharactersType($fbb, $charTypesOffset);
75    Movie::addCharacters($fbb, $charsOffset);
76    Movie::finishMovieBuffer($fbb, Movie::endMovie($fbb));
77
78    $buf = Google\FlatBuffers\ByteBuffer::wrap($fbb->dataBuffer()->data());
79
80    $movie = Movie::getRootAsMovie($buf);
81
82    $assert->strictEqual($movie->getCharactersTypeLength(), count($charTypes));
83    $assert->strictEqual($movie->getCharactersLength(), $movie->getCharactersTypeLength());
84
85    for ($i = 0; $i < count($charTypes); ++$i) {
86        $assert->strictEqual($movie->getCharactersType($i), $charTypes[$i]);
87    }
88
89    $bookReader7 = $movie->getCharacters(0, new BookReader());
90    $assert->strictEqual($bookReader7->getBooksRead(), 7);
91
92    $attacker = $movie->getCharacters(1, new Attacker());
93    $assert->strictEqual($attacker->getSwordAttackDamage(), 5);
94
95    $bookReader2 = $movie->getCharacters(2, new BookReader());
96    $assert->strictEqual($bookReader2->getBooksRead(), 2);
97}
98
99try {
100    main();
101    exit(0);
102} catch(Exception $e) {
103    printf("Fatal error: Uncaught exception '%s' with message '%s. in %s:%d\n", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
104    printf("Stack trace:\n");
105    echo $e->getTraceAsString() . PHP_EOL;
106    printf("  thrown in in %s:%d\n", $e->getFile(), $e->getLine());
107
108    die(-1);
109}
110