Test Setup Failed
Push — master ( 691446...0e5a58 )
by Php Easy Api
03:49
created

Test::create()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 32
nop 0
dl 0
loc 40
rs 8.6506
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Console\Source\Test;
4
5
use Resta\Support\PhpUnitManager;
6
use Resta\Support\SimpleXmlManager;
7
use Resta\Console\ConsoleOutputter;
8
use Resta\Console\ConsoleListAccessor;
9
use Symfony\Component\Process\Process;
10
use Symfony\Component\Process\Exception\ProcessFailedException;
11
12
class Test extends ConsoleOutputter
13
{
14
    use ConsoleListAccessor;
15
16
    /**
17
     * @var $type
0 ignored issues
show
Documentation Bug introduced by
The doc comment $type at position 0 could not be parsed: Unknown type name '$type' at position 0 in $type.
Loading history...
18
     */
19
    public $type = 'test';
20
21
    /**
22
     * @var $define
0 ignored issues
show
Documentation Bug introduced by
The doc comment $define at position 0 could not be parsed: Unknown type name '$define' at position 0 in $define.
Loading history...
23
     */
24
    public $define = 'creates test file for application';
25
26
    /**
27
     * @var $commandRule
0 ignored issues
show
Documentation Bug introduced by
The doc comment $commandRule at position 0 could not be parsed: Unknown type name '$commandRule' at position 0 in $commandRule.
Loading history...
28
     */
29
    public $commandRule = [];
30
31
    /**
32
     * @method generate
33
     * @return mixed
34
     */
35
    public function create()
36
    {
37
        if(isset($this->argument['controller'])){
38
            $this->argument['test'] = $this->argument['controller'];
39
        }
40
41
        $type = (isset($this->argument['type'])) ? $this->argument['type'] : 'Unit';
42
43
        if(!file_exists(app()->path()->tests())){
44
            $this->directory['test'] = app()->path()->tests();
45
            $this->file->makeDirectory($this);
46
        }
47
48
        $dirWithType = app()->path()->tests().'/'.$type;
49
50
51
        if(!file_exists($dirWithType)){
52
            $this->directory['testType'] = $dirWithType;
53
            $this->file->makeDirectory($this);
54
        }
55
56
        $this->argument['testPath'] = app()->namespace()->tests();
57
        $this->argument['testDirPath'] = app()->namespace()->tests().'\\'.$type;
58
        $this->argument['testNamespace'] = ucfirst($this->argument['test']).'Test';
59
        $this->argument['projectName'] = strtolower($this->projectName());
60
61
        if(isset($this->argument['controller']) && is_string($this->argument['controller'])){
62
63
            $this->argument['controllerForHttpRequest'] = strtolower($this->argument['controller']);
64
            $this->touch['test/testforcontroller']= $dirWithType.'/'.$this->argument['testNamespace'].'.php';
65
        }
66
        else{
67
            $this->touch['test/test']= $dirWithType.'/'.$this->argument['testNamespace'].'.php';
68
        }
69
70
71
72
        $this->file->touch($this);
73
74
        echo $this->classical(' > Test file called as "'.$this->argument['test'].'" has been successfully created in the '.$dirWithType.'');
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function publish()
81
    {
82
        $phpunit = ''.root.'/phpunit.xml';
83
84
        $xml = new SimpleXmlManager($phpunit);
85
86
        $array = $xml->toArray();
87
88
        $new = (new PhpUnitManager($array))->add(strtolower($this->projectName()),'directory',
89
            str_replace(root.''.DIRECTORY_SEPARATOR,"",app()->path()->tests()));
90
91
        $newDataXml = $xml->toXml($new);
92
93
        app()->get('fileSystem')->writeFile($phpunit,$newDataXml);
94
95
        echo $this->classical(' > phpunit.xml file has been updated');
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function run()
102
    {
103
        $process = new Process(['vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'phpunit','--group',strtolower($this->projectName())]);
104
        $process->setTty(true);
105
106
        try {
107
            $process->mustRun();
108
109
            echo $process->getOutput();
110
        } catch (ProcessFailedException $exception) {
111
            echo $exception->getMessage();
112
        }
113
    }
114
115
    public function phpUnit()
116
    {
117
118
    }
119
}