Completed
Push — master ( 36644b...bc2be6 )
by Andrii
09:15
created

src/console/PhpunitController.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * HiDev plugin for PHPUnit.
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\handlers\BaseHandler;
14
use Yii;
15
16
/**
17
 * PHPunit.
18
 */
19
class PhpunitController extends \hidev\base\Controller
20
{
21
    protected $_before = ['phpunit.xml.dist'];
22
23
    public $force;
24
25
    public function getComponent()
26
    {
27
        return $this->take('phpunit');
28
    }
29
30
    /**
31
     * Generates `tests/_bootstrap.php` and runs tests.
32
     */
33
    public function actionIndex()
34
    {
35
        $this->getComponent()->touchBootstrap();
36
37
        return $this->doRun();
38
    }
39
40
    protected function doRun()
41
    {
42
        $args = $this->getComponent()->prepareArgs();
43
44
        return $this->passthru('phpunit', $args);
45
    }
46
47
    /**
48
     * Generates skeleton class for fake.
49
     */
50 View Code Duplication
    public function actionGenfake($file)
0 ignored issues
show
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...
51
    {
52
        $path = $this->buildFakePath($file);
53
        if (!$this->force && file_exists($path)) {
54
            Yii::warning("already exists: $path");
55
            return 1;
56
        }
57
58
        return $this->genFake($file, $path);
59
    }
60
61
    protected function genFake($file, $path)
62
    {
63
        $text = $this->getView()->render('phpunit/fake.twig', [
64
            'class'         => $this->buildClass($file),
65
            'namespace'     => $this->buildTestNamespace(),
66
            'name'          => $file,
67
        ]);
68
        BaseHandler::write($path, $text);
69
    }
70
71
    /**
72
     * Generates skeleton class for test.
73
     */
74 View Code Duplication
    public function actionGentest($file)
0 ignored issues
show
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...
75
    {
76
        $path = $this->buildTestPath($file);
77
        if (!$this->force && file_exists($path)) {
78
            Yii::warning("already exists: $path");
79
            return 1;
80
        }
81
82
        return $this->genSkel($file);
83
    }
84
85
    protected static function prepareFile($file)
86
    {
87
        return substr($file, -4) === '.php' ? substr($file, 0, -4) : $file;
88
    }
89
90
    protected function genSkel($file)
91
    {
92
        $destPath = $this->buildTestPath($file);
93
        $dir = dirname($destPath);
94
        if (!file_exists($dir)) {
95
            mkdir($dir, 0777, true);
96
        }
97
98
        return $this->passthru('phpunit-skelgen', [
99
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
100
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $destPath,
101
        ]);
102
    }
103
104
    protected function buildNamespace($dir = '')
105
    {
106
        return $this->take('package')->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
107
    }
108
109
    protected function buildTestNamespace($dir = 'tests\\unit')
110
    {
111
        return $this->buildNamespace($dir);
112
    }
113
114
    protected function buildClass($file, $dir = '', $postfix = '')
115
    {
116
        return $this->buildNamespace($dir) . '\\' . strtr(static::prepareFile($file), '/', '\\') . $postfix;
117
    }
118
119
    protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test')
120
    {
121
        return $this->buildClass($file, $dir, $postfix);
122
    }
123
124
    protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
125
    {
126
        return $dir . DIRECTORY_SEPARATOR . $prefix . static::prepareFile($file) . $postfix . '.php';
127
128
        //## XXX getting absolute path, think if needed
129
        //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...
130
    }
131
132
    protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
133
    {
134
        return $this->buildPath($file, $dir, $prefix, $postfix);
135
    }
136
137
    protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
138
    {
139
        return $this->buildPath($file, $dir, $prefix, $postfix);
140
    }
141
}
142