Completed
Push — master ( 63a63d...b00238 )
by Andrii
10:56
created

PhpunitController::buildTestNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * PHPUnit plugin for HiDev.
4
 *
5
 * @link      https://github.com/hiqdev/hidev-phpunit
6
 * @package   hidev-phpunit
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\phpunit\console;
12
13
use hidev\base\File;
14
use hidev\handlers\BaseHandler;
15
use Yii;
16
17
/**
18
 * PHPunit.
19
 */
20
class PhpunitController extends \hidev\base\Controller
21
{
22
    protected $_before = ['phpunit.xml.dist'];
23
24
    public $force;
25
26
    public function getComponent()
27
    {
28
        return $this->take('phpunit');
0 ignored issues
show
Documentation Bug introduced by
The method take does not exist on object<hidev\phpunit\console\PhpunitController>? 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...
29
    }
30
31
    /**
32
     * Generates `tests/_bootstrap.php` and runs tests.
33
     */
34
    public function actionIndex()
35
    {
36
        $this->getComponent()->touchBootstrap();
37
38
        return $this->doRun();
39
    }
40
41
    protected function doRun()
42
    {
43
        $args = $this->getComponent()->prepareArgs();
44
45
        return $this->passthru('phpunit', $args);
0 ignored issues
show
Documentation Bug introduced by
The method passthru does not exist on object<hidev\phpunit\console\PhpunitController>? 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...
46
    }
47
48
    /**
49
     * Generates skeleton class for fake.
50
     */
51 View Code Duplication
    public function actionGenfake($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $path = $this->buildFakePath($file);
54
        if (!$this->force && file_exists($path)) {
55
            Yii::warning("already exists: $path");
56
            return 1;
57
        }
58
59
        return $this->genFake($file, $path);
60
    }
61
62
    protected function genFake($file, $path)
63
    {
64
        $text = $this->getView()->render('phpunit/fake.twig', [
65
            'class'         => $this->buildClass($file),
66
            'namespace'     => $this->buildTestNamespace(),
67
            'name'          => $file,
68
        ]);
69
        BaseHandler::write($path, $text);
70
    }
71
72
    /**
73
     * Generates skeleton class for test.
74
     */
75 View Code Duplication
    public function actionGentest($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $path = $this->buildTestPath($file);
78
        if (!$this->force && file_exists($path)) {
79
            Yii::warning("already exists: $path");
80
            return 1;
81
        }
82
83
        return $this->genSkel($file);
84
    }
85
86
    protected static function prepareFile($file)
87
    {
88
        return substr($file, -4) === '.php' ? substr($file, 0, -4) : $file;
89
    }
90
91
    protected function genSkel($file)
92
    {
93
        $destPath = $this->buildTestPath($file);
94
        $dir = dirname($destPath);
95
        if (!file_exists($dir)) {
96
            mkdir($dir, 0777, true);
97
        }
98
99
        return $this->passthru('phpunit-skelgen', [
0 ignored issues
show
Documentation Bug introduced by
The method passthru does not exist on object<hidev\phpunit\console\PhpunitController>? 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...
100
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
101
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $destPath,
102
        ]);
103
    }
104
105
    protected function buildNamespace($dir = '')
106
    {
107
        return $this->take('package')->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
0 ignored issues
show
Documentation Bug introduced by
The method take does not exist on object<hidev\phpunit\console\PhpunitController>? 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...
108
    }
109
110
    protected function buildTestNamespace($dir = 'tests\\unit')
111
    {
112
        return $this->buildNamespace($dir);
113
    }
114
115
    protected function buildClass($file, $dir = '', $postfix = '')
116
    {
117
        return $this->buildNamespace($dir) . '\\' . strtr(static::prepareFile($file), '/', '\\') . $postfix;
118
    }
119
120
    protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test')
121
    {
122
        return $this->buildClass($file, $dir, $postfix);
123
    }
124
125
    protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
126
    {
127
        return $dir . DIRECTORY_SEPARATOR . $prefix . static::prepareFile($file) . $postfix . '.php';
128
129
        //## XXX getting absolute path, think if needed
130
        //return strncmp($path, '/', 1) === 0 ? $path : Yii::getAlias("@root/$path");
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
    }
132
133
    protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
134
    {
135
        return $this->buildPath($file, $dir, $prefix, $postfix);
136
    }
137
138
    protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
139
    {
140
        return $this->buildPath($file, $dir, $prefix, $postfix);
141
    }
142
}
143