Completed
Push — master ( bc2781...c6b0bc )
by Andrii
03:12
created

PhpunitController::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * PHPUnit plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-phpunit
7
 * @package   hidev-phpunit
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\phpunit\controllers;
13
14
use hidev\handlers\BaseHandler;
15
use Yii;
16
17
/**
18
 * Goal for Phpunit.
19
 */
20
class PhpunitController extends \hidev\controllers\CommonController
21
{
22
    protected $_before = ['phpunit.xml.dist'];
23
24
    protected $_version;
25
26
    public $force;
27
    public $colors;
28
    public $coverageText;
29
    public $coverageClover;
30
31
    public function getConfiguration()
32
    {
33
        return $this->getGoal('phpunit.xml.dist');
34
    }
35
36
    public function actionMake()
37
    {
38
        return $this->actionRun();
39
    }
40
41
    public function actionRun()
42
    {
43
        $args = [];
44
        if ($this->coverageText) {
45
            $args[] = '--coverage-text';
46
        }
47
        if ($this->coverageClover) {
48
            $args[] = '--coverage-clover=' . (is_string($this->coverageClover) ? $this->coverageClover : 'coverage.clover');
49
        }
50
        if ($this->colors) {
51
            $args[] = '--colors' . (version_compare($this->getVersion(), '4.7.0', '>=') ? '=' . $this->colors : '');
52
        }
53
54
        return $this->passthru('phpunit', $args);
55
    }
56
57
    public function getVersion()
58
    {
59
        if ($this->_version === null) {
60
            $lines = $this->exec('phpunit', ['--version']);
61
            $a = explode(' ', $lines[0], 3)[1];
62
            $this->_version = $a;
63
        }
64
65
        return $this->_version;
66
    }
67
68 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...
69
    {
70
        $path = $this->buildFakePath($file);
71
        if (!$this->force && file_exists($path)) {
72
            Yii::warning("already exists: $path");
73
            return 1;
74
        }
75
76
        return $this->genFake($file, $path);
77
    }
78
79
    public function genFake($file, $path)
80
    {
81
        $text = $this->getView()->render('phpunit/fake.twig', [
82
            'class'         => $this->buildClass($file),
83
            'namespace'     => $this->buildTestNamespace(),
84
            'name'          => $file,
85
        ]);
86
        BaseHandler::write($path, $text);
87
    }
88
89 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...
90
    {
91
        $path = $this->buildTestPath($file);
92
        if (!$this->force && file_exists($path)) {
93
            Yii::warning("already exists: $path");
94
            return 1;
95
        }
96
97
        return $this->genSkel($file);
98
    }
99
100
    public function genSkel($file)
101
    {
102
        return $this->passthru('phpunit-skelgen', [
103
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
104
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $this->buildTestPath($file),
105
        ]);
106
    }
107
108
    public function buildNamespace($dir = '')
109
    {
110
        return $this->package->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
111
    }
112
113
    public function buildTestNamespace($dir = 'tests\\unit')
114
    {
115
        return $this->buildNamespace($dir);
116
    }
117
118
    public function buildClass($file, $dir = '')
119
    {
120
        return $this->buildNamespace($dir) . '\\' . strtr($file, '/', '\\');
121
    }
122
123
    public function buildTestClass($file, $dir = 'tests\\unit')
124
    {
125
        return $this->buildClass($file, $dir);
126
    }
127
128
    public function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
129
    {
130
        return $dir . DIRECTORY_SEPARATOR . $prefix . $file . $postfix . '.php';
131
    }
132
133
    public function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
134
    {
135
        return $this->buildPath($file, $dir, $prefix, $postfix);
136
    }
137
138
    public function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
139
    {
140
        return $this->buildPath($file, $dir, $prefix, $postfix);
141
    }
142
}
143