Completed
Push — master ( d7e067...d709bf )
by Andrii
11:31
created

PhpunitController::buildTestClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
    public $force;
25
    public $colors;
26
    public $coverageText;
27
    public $coverageClover;
28
29
    public function getConfiguration()
30
    {
31
        return $this->getGoal('phpunit.xml.dist');
32
    }
33
34
    public function actionMake()
35
    {
36
        return $this->actionRun();
37
    }
38
39
    public function actionRun()
40
    {
41
        $args = [];
42
        if ($this->coverageText) {
43
            $args[] = '--coverage-text';
44
        }
45
        if ($this->coverageClover) {
46
            $args[] = '--coverage-clover=' . (is_string($this->coverageClover) ? $this->coverageClover : 'coverage.clover');
47
        }
48
        if ($this->colors) {
49
            $args[] = '--colors=' . $this->colors;
50
        }
51
52
        return $this->passthru('phpunit', $args);
53
    }
54
55 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...
56
    {
57
        $path = $this->buildFakePath($file);
58
        if (!$this->force && file_exists($path)) {
59
            Yii::warning("already exists: $path");
60
            return 1;
61
        }
62
63
        return $this->genFake($file, $path);
64
    }
65
66
    public function genFake($file, $path)
67
    {
68
        $text = $this->getView()->render('phpunit/fake.twig', [
69
            'class'         => $this->buildClass($file),
70
            'namespace'     => $this->buildTestNamespace(),
71
            'name'          => $file,
72
        ]);
73
        BaseHandler::write($path, $text);
74
    }
75
76 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...
77
    {
78
        $path = $this->buildTestPath($file);
79
        if (!$this->force && file_exists($path)) {
80
            Yii::warning("already exists: $path");
81
            return 1;
82
        }
83
84
        return $this->genSkel($file);
85
    }
86
87
    public function genSkel($file)
88
    {
89
        return $this->passthru('phpunit-skelgen', [
90
            'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
91
            $this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $this->buildTestPath($file),
92
        ]);
93
    }
94
95
    public function buildNamespace($dir = '')
96
    {
97
        return $this->package->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
98
    }
99
100
    public function buildTestNamespace($dir = 'tests\\unit')
101
    {
102
        return $this->buildNamespace($dir);
103
    }
104
105
    public function buildClass($file, $dir = '')
106
    {
107
        return $this->buildNamespace($dir) . '\\' . strtr($file, '/', '\\');
108
    }
109
110
    public function buildTestClass($file, $dir = 'tests\\unit')
111
    {
112
        return $this->buildClass($file, $dir);
113
    }
114
115
    public function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
116
    {
117
        return $dir . DIRECTORY_SEPARATOR . $prefix . $file . $postfix . '.php';
118
    }
119
120
    public function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
121
    {
122
        return $this->buildPath($file, $dir, $prefix, $postfix);
123
    }
124
125
    public function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
126
    {
127
        return $this->buildPath($file, $dir, $prefix, $postfix);
128
    }
129
}
130