ModuleTestingServiceProvider   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 7

13 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 15 2
A includeCodeceptionShim() 0 5 1
A registerConfig() 0 9 1
A registerHelpers() 0 6 2
A registerMacros() 0 11 1
B registerBaseTestClasses() 0 26 3
A registerTemplates() 0 20 1
A registerProviders() 0 4 1
A templatePath() 0 4 1
A register() 0 4 1
A provides() 0 4 1
A registerCommands() 0 22 1
A path() 0 4 1
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Providers;
5
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\ServiceProvider;
9
use SmartWeb\ModuleTesting\BaseAcceptanceCest;
10
use SmartWeb\ModuleTesting\BaseGraphQLCest;
11
use SmartWeb\ModuleTesting\BaseIntegrationCest;
12
use SmartWeb\ModuleTesting\BaseUnitTest;
13
use SmartWeb\ModuleTesting\Console\Command\GenerateCest;
14
use SmartWeb\ModuleTesting\Console\Command\GenerateSetup;
15
use SmartWeb\ModuleTesting\Console\Command\GenerateTest;
16
use SmartWeb\ModuleTesting\Generator\Test\BaseClassResolver;
17
use SmartWeb\ModuleTesting\Generator\Test\CodeceptionTestType;
18
use SmartWeb\ModuleTesting\Template\TemplateFactory;
19
use SmartWeb\ModuleTesting\Util\FileUtil;
20
use const DIRECTORY_SEPARATOR;
21
use function glob;
22
use function implode;
23
24
/**
25
 * Class ModuleTestingServiceProvider
26
 *
27
 * @package SmartWeb\ModuleTesting\Providers
28
 */
29
class ModuleTestingServiceProvider extends ServiceProvider
30
{
31
    
32
    /**
33
     * Indicates if loading of the provider is deferred.
34
     *
35
     * @var bool
36
     */
37
    protected $defer = false;
38
    
39
    /**
40
     * Boot the application events.
41
     */
42
    public function boot()
43
    {
44
        if ($this->app->environment() !== 'production') {
45
            $this->includeCodeceptionShim();
46
            
47
            $this->registerConfig();
48
            $this->registerHelpers();
49
            $this->registerMacros();
50
            
51
            $this->registerBaseTestClasses();
52
            $this->registerTemplates();
53
            
54
            $this->registerProviders();
55
        }
56
    }
57
    
58
    private function includeCodeceptionShim()
59
    {
60
        $vendorDir = FileUtil::findParentDir(__DIR__, 'vendor');
61
        require_once $vendorDir . '/codeception/codeception/shim.php';
62
    }
63
    
64
    /**
65
     * Register config.
66
     *
67
     * @return void
68
     */
69
    protected function registerConfig()
70
    {
71
        $this->publishes([
72
            __DIR__ . '/../Config/config.php' => config_path('testing.php'),
73
        ], 'config');
74
        $this->mergeConfigFrom(
75
            __DIR__ . '/../Config/config.php', 'testing'
76
        );
77
    }
78
    
79
    protected function registerHelpers()
80
    {
81
        foreach (glob($this->path('Helpers/*.php')) as $file) {
82
            File::requireOnce($file);
83
        }
84
    }
85
    
86
    protected function registerMacros()
87
    {
88
        Collection::macro('mapIntoWithValues', function (string $class) {
89
            /**
90
             * @var Collection $this
91
             */
92
            return $this->map(function ($value) use ($class) {
0 ignored issues
show
Bug introduced by
The method map() does not seem to exist on object<SmartWeb\ModuleTe...TestingServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
                return new $class($value);
94
            });
95
        });
96
    }
97
    
98
    protected function registerBaseTestClasses()
99
    {
100
        $baseClasses = [
101
            'test' => [
102
                'Unit' => BaseUnitTest::class,
103
            ],
104
            'cest' => [
105
                'Integration' => BaseIntegrationCest::class,
106
                'GraphQL'     => BaseGraphQLCest::class,
107
                'Acceptance'  => BaseAcceptanceCest::class,
108
            ],
109
            'cept' => [],
110
        ];
111
        
112
        foreach ($baseClasses as $type => $classesForType) {
113
            /**
114
             * @var CodeceptionTestType $type
115
             * @var string[]            $classesForType
116
             */
117
            $type = CodeceptionTestType::memberByKey($type);
118
            
119
            foreach ($classesForType as $suite => $baseClass) {
120
                BaseClassResolver::registerBaseClassForModule($type, $suite, BaseClassResolver::DEFAULT_MODULE, $baseClass);
121
            }
122
        }
123
    }
124
    
125
    protected function registerTemplates()
126
    {
127
        TemplateFactory::registerTemplate(
128
            CodeceptionTestType::test(),
129
            'Unit',
130
            $this->templatePath('test.unit.tpl')
131
        );
132
        
133
        TemplateFactory::registerTemplate(
134
            CodeceptionTestType::cest(),
135
            'Integration',
136
            $this->templatePath('cest.integration.tpl')
137
        );
138
        
139
        TemplateFactory::registerTemplate(
140
            CodeceptionTestType::cest(),
141
            'GraphQL',
142
            $this->templatePath('cest.graphql.tpl')
143
        );
144
    }
145
    
146
    protected function registerProviders()
147
    {
148
        $this->app->register(ExpressionLanguageServiceProvider::class);
149
    }
150
    
151
    /**
152
     * @param string $template
153
     *
154
     * @return string
155
     */
156
    protected function templatePath(string $template) : string
157
    {
158
        return TemplateFactory::DEFAULT_TEMPLATE_DIR . DIRECTORY_SEPARATOR . $template;
159
    }
160
    
161
    /**
162
     * Register the service provider.
163
     *
164
     * @return void
165
     */
166
    public function register()
167
    {
168
        $this->registerCommands();
169
    }
170
    
171
    /**
172
     * Get the services provided by the provider.
173
     *
174
     * @return array
175
     */
176
    public function provides()
177
    {
178
        return [];
179
    }
180
    
181
    protected function registerCommands()
182
    {
183
        $this->app->singleton(
184
            'command.smartweb.testing.generate-setup',
185
            GenerateSetup::class
186
        );
187
        
188
        $this->app->singleton(
189
            'command.smartweb.testing.generate-test',
190
            GenerateTest::class
191
        );
192
        
193
        $this->app->singleton(
194
            'command.smartweb.testing.generate-cest',
195
            GenerateCest::class
196
        );
197
        
198
        $this->commands(
199
            'command.smartweb.testing.generate-setup',
200
            'command.smartweb.testing.generate-test',
201
            'command.smartweb.testing.generate-cest');
202
    }
203
    
204
    /**
205
     * @param string[] ...$parts
206
     *
207
     * @return string
208
     */
209
    protected function path(...$parts) : string
210
    {
211
        return __DIR__ . '/../' . implode(DIRECTORY_SEPARATOR, $parts);
212
    }
213
}
214