Completed
Push — master ( c32bd7...f916e0 )
by Hannes
02:12
created

AssertReadme   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A assertFile() 0 16 4
A assertReadme() 0 4 1
1
<?php
2
3
namespace hanneskod\readmetester\PHPUnit;
4
5
use hanneskod\readmetester\ReadmeTester;
6
use hanneskod\readmetester\Format\FormatFactory;
7
8
class AssertReadme
9
{
10
    /**
11
     * @var \PHPUnit_Framework_TestCase
12
     */
13
    private $testCase;
14
15
    /**
16
     * @var ReadmeTester
17
     */
18
    private $tester;
19
20
    /**
21
     * @var FormatFactory
22
     */
23
    private $formatFactory;
24
25
    public function __construct(
26
        \PHPUnit_Framework_TestCase $testCase,
27
        ReadmeTester $tester = null,
28
        FormatFactory $formatFactory = null
29
    ) {
30
        $this->testCase = $testCase;
31
        $this->tester = $tester ?: new ReadmeTester;
32
        $this->formatFactory = $formatFactory ?: new FormatFactory;
33
    }
34
35
    /**
36
     * Validate code examples in $file
37
     *
38
     * @param  \SplFileObject $file
39
     * @param  string         $formatIdentifier
40
     * @return void
41
     */
42
    public function assertFile(\SplFileObject $file, $formatIdentifier = '')
43
    {
44
        $format = $this->formatFactory->createFormat($formatIdentifier ?: $file->getExtension());
45
        $result = $this->testCase->getTestResultObject();
46
47
        foreach ($this->tester->test($file, $format) as $example => $returnObj) {
48
            $this->testCase->addToAssertionCount(1);
49
            if ($returnObj->isFailure()) {
50
                $result->addFailure(
51
                    $this->testCase,
52
                    new \PHPUnit_Framework_AssertionFailedError("Example $example: {$returnObj->getMessage()}"),
53
                    0.0
54
                );
55
            }
56
        }
57
    }
58
59
    public function assertReadme($filename, $formatIdentifier = '')
60
    {
61
        return $this->assertFile(new \SplFileObject($filename), $formatIdentifier);
62
    }
63
}
64