Setup   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDisk() 0 4 1
A getPath() 0 4 1
A getWorkDir() 0 4 1
A getTestsDir() 0 4 1
A resolveWorkDir() 0 4 1
A getNamespace() 0 10 1
A getPathRelativeToBasePath() 0 4 1
A getModule() 0 4 1
A resolveModule() 0 13 3
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Codeception\SmartWeb;
5
6
use Exception;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Str;
10
use Nwidart\Modules\Module;
11
use SmartWeb\ModuleTesting\Codeception\SetupInterface;
12
use const DIRECTORY_SEPARATOR;
13
use function base_path;
14
use function is_null;
15
use function is_string;
16
17
18
/**
19
 * Class SmartWebCodeceptionSetup
20
 *
21
 * @package SmartWeb\ModuleTesting\Codeception
22
 */
23
class Setup implements SetupInterface
24
{
25
    
26
    /**
27
     * @var string
28
     */
29
    protected static $configTemplate = <<<EOF
30
suites:
31
    unit:
32
        path: .
33
{{tester}}
34
settings:
35
    shuffle: true
36
paths:
37
    tests: {{dir}}
38
    output: {{dir}}/_output
39
    support: {{dir}}/_support
40
    data: {{dir}}
41
    
42
EOF;
43
    
44
    /**
45
     * @var string
46
     */
47
    protected static $testerAndModules = <<<EOF
48
        actor: UnitTester
49
        modules:
50
            enabled:
51
                # add more modules here
52
                - Asserts
53
EOF;
54
    
55
    /**
56
     * @var Module
57
     */
58
    protected $module;
59
    
60
    /**
61
     * @var Filesystem
62
     */
63
    protected $disk;
64
    
65
    /**
66
     * @var string
67
     */
68
    protected $workingDir;
69
    
70
    /**
71
     * SmartWebCodeceptionSetup constructor.
72
     *
73
     * @param Module|string $module
74
     * @param string        $disk
75
     *
76
     * @throws Exception
77
     */
78
    public function __construct($module, string $disk = 'solution')
79
    {
80
        $this->module = $this->resolveModule($module);
81
        $this->disk = Storage::disk($disk);
82
    }
83
    
84
    /**
85
     * @return Filesystem
86
     */
87
    public function getDisk() : Filesystem
88
    {
89
        return $this->disk;
90
    }
91
    
92
    /**
93
     * Get the path relative to the working directory of this Setup.
94
     *
95
     * @param string $path
96
     *
97
     * @return string
98
     */
99
    public function getPath(string $path) : string
100
    {
101
        return $this->getWorkDir() . DIRECTORY_SEPARATOR . $path;
102
    }
103
    
104
    /**
105
     * @return string
106
     */
107
    public function getWorkDir() : string
108
    {
109
        return $this->workingDir = $this->workingDir ?? $this->resolveWorkDir();
110
    }
111
    
112
    /**
113
     * @inheritDoc
114
     */
115
    public function getTestsDir() : string
116
    {
117
        return self::DEFAULT_TESTS_DIR;
118
    }
119
    
120
    /**
121
     * @return string
122
     */
123
    protected function resolveWorkDir() : string
124
    {
125
        return $this->getPathRelativeToBasePath($this->module->getPath());
126
    }
127
    
128
    /**
129
     * @return string
130
     */
131
    public function getNamespace() : string
132
    {
133
        $modulePath = $this->getModule()->getPath();
134
        $relativeToBasePath = $this->getPathRelativeToBasePath($modulePath);
135
        
136
        $properCase = Str::ucfirst($relativeToBasePath);
137
        $asNamespace = str_replace(DIRECTORY_SEPARATOR, '\\', $properCase);
138
        
139
        return $asNamespace;
140
    }
141
    
142
    /**
143
     * @param string $path
144
     *
145
     * @return string
146
     */
147
    protected function getPathRelativeToBasePath(string $path) : string
148
    {
149
        return str_replace(base_path('') . DIRECTORY_SEPARATOR, '', $path);
150
    }
151
    
152
    /**
153
     * @return Module
154
     */
155
    public function getModule() : Module
156
    {
157
        return $this->module;
158
    }
159
    
160
    /**
161
     * @param Module|string $module
162
     *
163
     * @return Module
164
     * @throws Exception
165
     */
166
    private function resolveModule($module) : Module
167
    {
168
        if (is_string($module)) {
169
            $moduleName = $module;
170
            $module = \Nwidart\Modules\Facades\Module::find($module);
171
            
172
            if (is_null($module)) {
173
                throw new Exception("Could not resolve module {$moduleName}.");
174
            }
175
        }
176
        
177
        return $module;
178
    }
179
    
180
}
181