Test Setup Failed
Push — master ( 0cc29f...691446 )
by Php Easy Api
04:02
created

Test::publish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 16
rs 10
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 = ['create'=>['test']];
30
31
    /**
32
     * @method generate
33
     * @return mixed
34
     */
35
    public function create()
36
    {
37
        $type = (isset($this->argument['type'])) ? $this->argument['type'] : 'Unit';
38
39
        if(!file_exists(app()->path()->tests())){
40
            $this->directory['test'] = app()->path()->tests();
41
            $this->file->makeDirectory($this);
42
        }
43
44
        $dirWithType = app()->path()->tests().'/'.$type;
45
46
47
        if(!file_exists($dirWithType)){
48
            $this->directory['testType'] = $dirWithType;
49
            $this->file->makeDirectory($this);
50
        }
51
52
        $this->argument['testPath'] = app()->namespace()->tests();
53
        $this->argument['testDirPath'] = app()->namespace()->tests().'\\'.$type;
54
        $this->argument['testNamespace'] = ucfirst($this->argument['test']).'Test';
55
        $this->argument['projectName'] = strtolower($this->projectName());
56
57
        $this->touch['test/test']= $dirWithType.'/'.$this->argument['testNamespace'].'.php';
58
59
60
        $this->file->touch($this);
61
62
        echo $this->classical(' > Test file called as "'.$this->argument['test'].'" has been successfully created in the '.$dirWithType.'');
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function publish()
69
    {
70
        $phpunit = ''.root.'/phpunit.xml';
71
72
        $xml = new SimpleXmlManager($phpunit);
73
74
        $array = $xml->toArray();
75
76
        $new = (new PhpUnitManager($array))->add(strtolower($this->projectName()),'directory',
77
            str_replace(root.''.DIRECTORY_SEPARATOR,"",app()->path()->tests()));
78
79
        $newDataXml = $xml->toXml($new);
80
81
        app()->get('fileSystem')->writeFile($phpunit,$newDataXml);
82
83
        echo $this->classical(' > phpunit.xml file has been updated');
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89
    public function run()
90
    {
91
        $process = new Process(['vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'phpunit','--group',strtolower($this->projectName())]);
92
        $process->setTty(true);
93
94
        try {
95
            $process->mustRun();
96
97
            echo $process->getOutput();
98
        } catch (ProcessFailedException $exception) {
99
            echo $exception->getMessage();
100
        }
101
    }
102
103
    public function phpUnit()
104
    {
105
106
    }
107
}