Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
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\Contracts\Filesystem\Filesystem;
9
use Illuminate\Support\Str;
10
use SmartWeb\ModuleTesting\Codeception\SetupInterface;
11
use SmartWeb\ModuleTesting\Codeception\Template\TemplateFactory;
12
use const DIRECTORY_SEPARATOR;
13
use function array_merge;
14
15
16
/**
17
 * Class BaseTestGenerator
18
 *
19
 * @package SmartWeb\Testing\Generator
20
 */
21
abstract class BaseTestGenerator
22
{
23
    
24
    /**
25
     * @var CodeceptionTestType
26
     */
27
    protected $type;
28
    
29
    /**
30
     * @var Filesystem
31
     */
32
    protected $disk;
33
    
34
    /**
35
     * @var SetupInterface
36
     */
37
    protected $setup;
38
    
39
    /**
40
     * @var string
41
     */
42
    protected $suite;
43
    
44
    /**
45
     * @var string
46
     */
47
    protected $name;
48
    
49
    /**
50
     * @var array
51
     */
52
    protected $settings;
53
    
54
    /**
55
     * @var string
56
     */
57
    protected $templatePath;
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->templatePath = $this->type . '.' . Str::lower($this->suite);
79
    }
80
    
81
    /**
82
     * @param array $settings
83
     *
84
     * @return array
85
     */
86
    protected function prepareSettings(array $settings) : array
87
    {
88
        return array_merge($this->getBaseSettings(), $settings);
89
    }
90
    
91
    /**
92
     * @return array
93
     */
94
    abstract protected function getBaseSettings() : array;
95
    
96
    /**
97
     * @param string $suite
98
     *
99
     * @return array
100
     * @throws \Exception
101
     */
102
    protected function getSuiteSettings(string $suite) : array
103
    {
104
        return Configuration::suiteSettings($suite, $this->getSettingsForModule());
105
    }
106
    
107
    /**
108
     * @return array
109
     * @throws \Codeception\Exception\ConfigurationException
110
     */
111
    protected function getSettingsForModule()
112
    {
113
        return Configuration::config($this->setup->getWorkDir());
114
    }
115
    
116
    /**
117
     * @throws FileNotFoundException
118
     */
119 View Code Duplication
    public function generate()
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...
120
    {
121
        $factory = new TemplateFactory();
122
        $fileContents = $factory->getTemplate($this->templatePath)->produceWith($this->prepareSettings($this->settings));
123
        
124
        $path = $this->suite . DIRECTORY_SEPARATOR . $this->name . Str::ucfirst($this->type) . '.php';
125
        
126
        $this->createFile($path, $fileContents);
127
    }
128
    
129
    /**
130
     * @param string $name
131
     *
132
     * @return string
133
     */
134
    protected function prepareName(string $name) : string
135
    {
136
        return Str::endsWith($name, Str::ucfirst($this->type))
137
            ? Str::replaceLast(Str::ucfirst($this->type), '', $name)
138
            : $name;
139
    }
140
    
141
    /**
142
     * @param string $path
143
     * @param        $contents
144
     *
145
     * @return bool
146
     */
147 View Code Duplication
    protected function createFile(string $path, $contents) : bool
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...
148
    {
149
        $path = $this->getPath($path);
150
        
151
        if (!$this->disk->exists($path)) {
152
            return $this->disk->put($path, $contents);
153
        }
154
        
155
        return false;
156
    }
157
    
158
    /**
159
     * @param string $path
160
     *
161
     * @return string
162
     */
163
    protected function getPath(string $path) : string
164
    {
165
        return $this->setup->getWorkDir() . DIRECTORY_SEPARATOR . $this->setup->getTestsDir() . DIRECTORY_SEPARATOR . $path;
166
    }
167
}