Passed
Push — master ( 8f00fc...54b7bc )
by Eugene
05:22
created

ExamplesTest::checkExampleRequirements()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 9
nc 18
nop 1
dl 0
loc 16
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public const EXIT_CODE_SKIP = 42;
19
20
    /**
21
     * @dataProvider provideExampleData
22
     */
23
    public function testExample(string $filename) : void
24
    {
25
        $uri = ClientBuilder::createFromEnv()->getUri();
26
27
        exec("php $filename $uri", $output, $exitCode);
28
29
        if (self::EXIT_CODE_SKIP === $exitCode) {
30
            self::markTestSkipped(implode("\n", $output));
31
        }
32
33
        self::assertSame(0, $exitCode, implode("\n", $output));
34
35
        $expectedOutput = self::parseFile($filename);
36
        if (null !== $expectedOutput) {
37
            self::assertSame($expectedOutput, implode("\n", $output));
38
        }
39
    }
40
41
    public function provideExampleData() : iterable
42
    {
43
        $dir = dirname(__DIR__, 2).'/examples';
44
        foreach (glob("$dir/{**/*,*}.php", GLOB_BRACE) as $filename) {
45
            $basename = basename($filename, '.php');
46
            if ('bootstrap' === $basename) {
47
                continue;
48
            }
49
            // ignore classes
50
            if (strtolower($basename[0]) !== $basename[0]) {
51
                continue;
52
            }
53
54
            yield [$filename];
55
        }
56
    }
57
58
    private static function parseFile(string $filename) : ?string
59
    {
60
        $content = file_get_contents($filename);
61
        if (preg_match('~\/\*\s*?OUTPUT\b(.+?)\*\/~s', $content, $matches)) {
62
            return trim($matches[1]);
63
        }
64
65
        return null;
66
    }
67
}
68