Passed
Pull Request — master (#63)
by Eugene
02:59
created

ExamplesTest::provideExampleData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 3
b 0
f 0
nc 4
nop 0
dl 0
loc 14
rs 10
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
        $info = self::parseFile($filename);
24
25
        if (isset($info['tarantool_version'])) {
26
            [$major, $minor, $patch] = \sscanf($info['tarantool_version'], '%d.%d.%d');
27
            $requiredVersionId = $major * 10000 + $minor * 100 + $patch;
28
            if (self::getTarantoolVersionId() < $requiredVersionId) {
29
                self::markTestSkipped(sprintf('Tarantool >= %s is required.', $info['tarantool_version']));
30
            }
31
        }
32
33
        $uri = ClientBuilder::createFromEnv()->getUri();
34
35
        exec("php $filename $uri", $output, $exitCode);
36
37
        self::assertSame(0, $exitCode, implode("\n", $output));
38
39
        if (isset($info['output'])) {
40
            self::assertSame($info['output'], implode("\n", $output));
41
        }
42
    }
43
44
    public function provideExampleData() : iterable
45
    {
46
        $dir = dirname(__DIR__, 2).'/examples';
47
        foreach (glob("$dir/{**/*,*}.php", GLOB_BRACE) as $filename) {
48
            $basename = basename($filename, '.php');
49
            if ('bootstrap' === $basename) {
50
                continue;
51
            }
52
            // ignore classes
53
            if (strtolower($basename[0]) !== $basename[0]) {
54
                continue;
55
            }
56
57
            yield [$filename];
58
        }
59
    }
60
61
    private static function parseFile(string $filename) : array
62
    {
63
        $content = file_get_contents($filename);
64
65
        $result = [];
66
        if (preg_match('~@requires\s+?tarantool\s+?(?<version>[.\d]+)~i', $content, $matches)) {
67
            $result['tarantool_version'] = $matches['version'];
68
        }
69
        if (preg_match('~\/\*\s*?OUTPUT\b(.+?)\*\/~s', $content, $matches)) {
70
            $result['output'] = trim($matches[1]);
71
        }
72
73
        return $result;
74
    }
75
}
76