ReadmeTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 132
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testPhpCode() 0 15 3
B parseReadme() 0 38 6
A getBuildValidationCode() 0 6 1
A getBuildValidatorCode() 0 6 1
A getBuildJmsSerializerCode() 0 6 1
A getBuildImportRunnerCode() 0 6 1
A getBuildImportRunCode() 0 7 1
A getBuildImportCode() 0 7 1
A getBuildImporterCode() 0 10 1
A getBuildTargetStorage() 0 7 1
A getBuildFFSP() 0 6 1
1
<?php
2
namespace Mathielen\ImportEngine;
3
4
class ReadmeTest extends \PHPUnit_Framework_TestCase
5
{
6
7
    /**
8
     * @dataProvider parseReadme
9
     */
10
    public function testPhpCode($startLine, $code)
11
    {
12
        if (strpos($code, '...')) {
13
            $this->markTestSkipped("Cannot test with insufficient info.");
14
        }
15
16
        ob_start();
17
        eval($code);
18
        $output = ob_get_contents();
19
        ob_end_clean();
20
21
        if (!empty($output)) {
22
            $this->fail("Error in README.me at line $startLine:\n".$output);
23
        }
24
    }
25
26
    public function parseReadme()
27
    {
28
        $chunks = array();
29
        $readme = file(__DIR__ . '/../../../../README.md');
30
31
        $startLine = $endLine = null;
32
        $i = 0;
33
        foreach ($readme as $line) {
34
            if ('```php' == trim($line)) {
35
                $startLine = $i;
36
                $endLine = null;
37
            } elseif ('```' == trim($line)) {
38
                $endLine = $i;
39
            }
40
41
            if ($startLine && $endLine) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $startLine of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $endLine of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
42
                $startLine++;
43
                $code = join('', array_slice($readme, $startLine, $endLine-$startLine));
44
45
                $code = str_replace('$ffsp = ...', $this->getBuildFFSP(), $code);
46
                $code = str_replace('$targetStorage = ...', $this->getBuildTargetStorage(), $code);
47
                $code = str_replace('$importRunner = ...', $this->getBuildImportRunnerCode(), $code);
48
                $code = str_replace('$importRun = ...', $this->getBuildImportRunCode(), $code);
49
                $code = str_replace('$importer = ...', $this->getBuildImporterCode(), $code);
50
                $code = str_replace('$import = ...', $this->getBuildImportCode(), $code);
51
                $code = str_replace('$validator = ...', $this->getBuildValidatorCode(), $code);
52
                $code = str_replace('$validation = ...', $this->getBuildValidationCode(), $code);
53
                $code = str_replace('$jms_serializer = ...', $this->getBuildJmsSerializerCode(), $code);
54
55
                $chunks[] = array($startLine, $code);
56
                $startLine = $endLine = null;
57
            }
58
59
            ++$i;
60
        }
61
62
        return $chunks;
63
    }
64
65
    private function getBuildValidationCode()
66
    {
67
        return $this->getBuildValidatorCode() . '
68
            $validation = new Mathielen\ImportEngine\Validation\ValidatorValidation($validator);
69
        ';
70
    }
71
72
    private function getBuildValidatorCode()
73
    {
74
        return '
75
            $validator = $this->createMock("Symfony\Component\Validator\Validator\ValidatorInterface");
76
        ';
77
    }
78
79
    private function getBuildJmsSerializerCode()
80
    {
81
        return '
82
            $jms_serializer = $this->createMock("JMS\Serializer\Serializer", array(), array(), "", false);
83
        ';
84
    }
85
86
    private function getBuildImportRunnerCode()
87
    {
88
        return '
89
            $importRunner = new Mathielen\ImportEngine\Import\Run\ImportRunner(new Mathielen\ImportEngine\Import\Workflow\DefaultWorkflowFactory(new Symfony\Component\EventDispatcher\EventDispatcher()));
90
        ';
91
    }
92
93
    private function getBuildImportRunCode()
94
    {
95
        return $this->getBuildImportCode() . '
96
            $importConfiguration = new Mathielen\ImportEngine\ValueObject\ImportConfiguration();
97
            $importRun = $importConfiguration->toRun();
98
        ';
99
    }
100
101
    private function getBuildImportCode()
102
    {
103
        return $this->getBuildImporterCode() . '
104
            $a = array(array("field1"=>"data1"));
105
            $import = Mathielen\ImportEngine\Import\Import::build($importer, new Mathielen\ImportEngine\Storage\ArrayStorage($a));
106
        ';
107
    }
108
109
    private function getBuildImporterCode()
110
    {
111
        return
112
            $this->getBuildValidationCode() .
113
            $this->getBuildFFSP() .
114
            $this->getBuildTargetStorage() . '
115
            $importer = Mathielen\ImportEngine\Importer\Importer::build($targetStorage);
116
            $importer->validation($validation);
117
        ';
118
    }
119
120
    private function getBuildTargetStorage()
121
    {
122
        return '
123
            $a = array();
124
            $targetStorage = new Mathielen\ImportEngine\Storage\ArrayStorage($a);
125
        ';
126
    }
127
128
    private function getBuildFFSP()
129
    {
130
        return '
131
            $ffsp = new Mathielen\ImportEngine\Storage\Provider\FinderFileStorageProvider(Symfony\Component\Finder\Finder::create());
132
        ';
133
    }
134
135
}
136