GenerateMarkdownDocTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 94
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 4 1
B testGenerateDocumentation() 0 71 8
1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class GenerateMarkdownDocTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Collection\loadTasks;
11
    use Task\Development\loadTasks;
12
    use Task\File\loadTasks;
13
14
    protected $fixtures;
15
16
    public function setUp()
17
    {
18
        $this->fixtures = new Fixtures();
19
        $this->initTestTasksTrait();
20
        $this->fixtures->createAndCdToSandbox();
21
    }
22
23
    public function tearDown()
24
    {
25
        $this->fixtures->cleanup();
26
    }
27
28
    public function testGenerateDocumentation()
29
    {
30
        $sourceFile = $this->fixtures->dataFile('TestedRoboTask.php');
31
        $this->assertFileExists($sourceFile);
32
        include $sourceFile;
33
        $this->assertTrue(class_exists('TestedRoboTask'));
34
35
        $collection = $this->collectionBuilder();
36
        $taskGenerator = $collection->taskGenDoc("TestedRoboTask.md");
0 ignored issues
show
Documentation Bug introduced by
The method taskGenDoc does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
        $taskGenerator->filterClasses(function (\ReflectionClass $r) {
38
            return !($r->isAbstract() || $r->isTrait()) && $r->implementsInterface('Robo\Contract\TaskInterface');
39
        })->prepend("# TestedRoboTask Tasks");
40
        $taskGenerator->docClass('TestedRoboTask');
41
42
        $taskGenerator->filterMethods(
43
            function (\ReflectionMethod $m) {
44
                if ($m->isConstructor() || $m->isDestructor() || $m->isStatic()) {
45
                    return false;
46
                }
47
                $undocumentedMethods =
48
                [
49
                    '',
50
                    'run',
51
                    '__call',
52
                    'inflect',
53
                    'injectDependencies',
54
                    'getCommand',
55
                    'getPrinted',
56
                    'getConfig',
57
                    'setConfig',
58
                    'logger',
59
                    'setLogger',
60
                    'setProgressIndicator',
61
                    'progressIndicatorSteps',
62
                    'setBuilder',
63
                    'getBuilder',
64
                    'collectionBuilder',
65
                ];
66
                return !in_array($m->name, $undocumentedMethods) && $m->isPublic(); // methods are not documented
67
            }
68
        )->processClassSignature(
69
            function ($c) {
70
                return "## " . preg_replace('~Task$~', '', $c->getShortName()) . "\n";
71
            }
72
        )->processClassDocBlock(
73
            function (\ReflectionClass $c, $doc) {
74
                $doc = preg_replace('~@method .*?(.*?)\)~', '* `$1)` ', $doc);
75
                $doc = str_replace('\\'.$c->name, '', $doc);
76
                return $doc;
77
            }
78
        )->processMethodSignature(
79
            function (\ReflectionMethod $m, $text) {
80
                return str_replace('#### *public* ', '* `', $text) . '`';
81
            }
82
        )->processMethodDocBlock(
83
            function (\ReflectionMethod $m, $text) {
84
85
                return $text ? ' ' . trim(strtok($text, "\n"), "\n") : '';
86
            }
87
        );
88
89
        $result = $collection->run();
90
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
91
92
        $this->assertFileExists('TestedRoboTask.md');
93
94
        $contents = file_get_contents('TestedRoboTask.md');
95
        $this->assertContains('A test task file. Used for testig documentation generation.', $contents);
96
        $this->assertContains('taskTestedRoboTask', $contents);
97
        $this->assertContains('Set the destination file', $contents);
98
    }
99
100
}
101