Completed
Push — master ( 2e22d6...16a402 )
by Nicolai
01:59
created

BaseTestGenerator::createFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Generator;
5
6
use Codeception\Configuration;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Support\Str;
9
use SmartWeb\ModuleTesting\Codeception\SetupInterface;
10
use SmartWeb\ModuleTesting\Template\TemplateFactory;
11
use SmartWeb\ModuleTesting\Util\InteractsWithFiles;
12
use SmartWeb\ModuleTesting\Util\Template;
13
use const DIRECTORY_SEPARATOR;
14
use function array_merge;
15
16
17
/**
18
 * Class BaseTestGenerator
19
 *
20
 * @package SmartWeb\ModuleTesting\Generator
21
 */
22
abstract class BaseTestGenerator
23
{
24
    
25
    use InteractsWithFiles;
26
    
27
    /**
28
     * @var CodeceptionTestType
29
     */
30
    protected $type;
31
    
32
    /**
33
     * @var SetupInterface
34
     */
35
    protected $setup;
36
    
37
    /**
38
     * @var string
39
     */
40
    protected $suite;
41
    
42
    /**
43
     * @var string
44
     */
45
    protected $name;
46
    
47
    /**
48
     * @var array
49
     */
50
    protected $settings;
51
    
52
    /**
53
     * Key used to identify the template to use for generating test files.
54
     *
55
     * @var string
56
     */
57
    protected $templateKey;
58
    
59
    /**
60
     * BaseTestGenerator constructor.
61
     *
62
     * @param CodeceptionTestType $type
63
     * @param SetupInterface      $setup
64
     * @param string              $suite
65
     * @param string              $name
66
     * @param array               $settings
67
     *
68
     * @throws \Exception
69
     */
70
    public function __construct(CodeceptionTestType $type, SetupInterface $setup, string $suite, string $name, array $settings = [])
71
    {
72
        $this->type = $type;
73
        $this->setup = $setup;
74
        $this->disk = $setup->getDisk();
75
        $this->suite = $suite;
76
        $this->name = $this->prepareName($name);
77
        $this->settings = $settings;
78
        $this->templateKey = $this->type . '.' . Str::lower($this->suite);
79
    }
80
    
81
    /**
82
     * @return string
83
     */
84
    final protected function getBaseClass() : string
85
    {
86
        $prefix = 'SmartWeb\ModuleTesting\Base';
87
        
88
        return Template::getBaseTestClass($this->suite, $this->type, $prefix);
89
    }
90
    
91
    /**
92
     * @param array $settings
93
     *
94
     * @return array
95
     */
96
    final protected function prepareSettings(array $settings) : array
97
    {
98
        return array_merge($this->getBaseSettings(), $settings);
99
    }
100
    
101
    /**
102
     * @return array
103
     */
104
    abstract protected function getBaseSettings() : array;
105
    
106
    /**
107
     * @param string $suite
108
     *
109
     * @return array
110
     * @throws \Exception
111
     */
112
    final protected function getSuiteSettings(string $suite) : array
113
    {
114
        return Configuration::suiteSettings($suite, $this->getSettingsForModule());
115
    }
116
    
117
    /**
118
     * @return array
119
     * @throws \Codeception\Exception\ConfigurationException
120
     */
121
    final protected function getSettingsForModule()
122
    {
123
        return Configuration::config($this->setup->getWorkDir());
124
    }
125
    
126
    /**
127
     * @throws FileNotFoundException
128
     */
129
    final public function generate()
130
    {
131
        $factory = new TemplateFactory();
132
        $fileContents = $factory->getTemplate($this->templateKey)->produceWith($this->prepareSettings($this->settings));
133
        
134
        $path = $this->suite . DIRECTORY_SEPARATOR . $this->name . Str::ucfirst($this->type) . '.php';
135
        
136
        $this->createFile($path, $fileContents);
137
    }
138
    
139
    /**
140
     * @param string $name
141
     *
142
     * @return string
143
     */
144
    final protected function prepareName(string $name) : string
145
    {
146
        return Str::endsWith($name, Str::ucfirst($this->type))
147
            ? Str::replaceLast(Str::ucfirst($this->type), '', $name)
148
            : $name;
149
    }
150
    
151
    /**
152
     * @param string $path
153
     * @param bool   $inBasePath
154
     *
155
     * @return string
156
     */
157 View Code Duplication
    final protected function getPath(string $path, bool $inBasePath = false) : string
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...
158
    {
159
        $pathPrefix = $this->setup->getWorkDir() . DIRECTORY_SEPARATOR;
160
        
161
        return $inBasePath
162
            ? $pathPrefix . $path
163
            : $pathPrefix . $this->setup->getTestsDir() . DIRECTORY_SEPARATOR . $path;
164
    }
165
}
166