Passed
Pull Request — master (#63)
by Eugene
06:42
created

ExamplesTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 45
rs 10
c 3
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A provideExampleData() 0 14 4
A assertOutput() 0 6 2
A testExample() 0 14 4
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration;
15
16
final class ExamplesTest extends TestCase
17
{
18
    /**
19
     * @dataProvider provideExampleData
20
     */
21
    public function testExample(string $filename) : void
22
    {
23
        if (strpos($filename, '/execute.php') && self::getTarantoolVersionId() < 20000) {
24
            self::markTestSkipped(sprintf('This version of Tarantool (%d) does not support sql.', self::getTarantoolVersionId()));
25
        }
26
27
        $uri = ClientBuilder::createFromEnv()->getUri();
28
29
        exec("php $filename $uri", $output, $exitCode);
30
31
        self::assertSame(0, $exitCode, implode("\n", $output));
32
33
        if ($output) {
34
            self::assertOutput($filename, implode("\n", $output));
35
        }
36
    }
37
38
    public function provideExampleData() : iterable
39
    {
40
        $dir = dirname(__DIR__, 2).'/examples';
41
        foreach (glob("$dir/{**/*,*}.php", GLOB_BRACE) as $filename) {
42
            $basename = basename($filename, '.php');
43
            if ('bootstrap' === $basename) {
44
                continue;
45
            }
46
            // ignore classes
47
            if (strtolower($basename[0]) !== $basename[0]) {
48
                continue;
49
            }
50
51
            yield [$filename];
52
        }
53
    }
54
55
    private static function assertOutput(string $filename, string $output) : void
56
    {
57
        $content = file_get_contents($filename);
58
59
        if (preg_match('~\/\*\s*?OUTPUT\b(.+?)\*\/~s', $content, $matches)) {
60
            self::assertSame(trim($matches[1]), $output);
61
        }
62
    }
63
}
64