Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

Setup::getModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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