Completed
Push — master ( 51cd60...609211 )
by Nicolai
01:43
created

ModuleTestingServiceProvider   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 15 2
A includeCodeceptionShim() 0 4 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 const DIRECTORY_SEPARATOR;
20
use function glob;
21
use function implode;
22
23
/**
24
 * Class ModuleTestingServiceProvider
25
 *
26
 * @package SmartWeb\ModuleTesting\Providers
27
 */
28
class ModuleTestingServiceProvider extends ServiceProvider
29
{
30
    
31
    /**
32
     * Indicates if loading of the provider is deferred.
33
     *
34
     * @var bool
35
     */
36
    protected $defer = false;
37
    
38
    /**
39
     * Boot the application events.
40
     */
41
    public function boot()
42
    {
43
        if ($this->app->environment() !== 'production') {
44
            $this->includeCodeceptionShim();
45
            
46
            $this->registerConfig();
47
            $this->registerHelpers();
48
            $this->registerMacros();
49
            
50
            $this->registerBaseTestClasses();
51
            $this->registerTemplates();
52
            
53
            $this->registerProviders();
54
        }
55
    }
56
    
57
    private function includeCodeceptionShim()
58
    {
59
        require_once __DIR__ . '/../../vendor/codeception/codeception/shim.php';
60
    }
61
    
62
    /**
63
     * Register config.
64
     *
65
     * @return void
66
     */
67
    protected function registerConfig()
68
    {
69
        $this->publishes([
70
            __DIR__ . '/../Config/config.php' => config_path('testing.php'),
71
        ], 'config');
72
        $this->mergeConfigFrom(
73
            __DIR__ . '/../Config/config.php', 'testing'
74
        );
75
    }
76
    
77
    protected function registerHelpers()
78
    {
79
        foreach (glob($this->path('Helpers/*.php')) as $file) {
80
            File::requireOnce($file);
81
        }
82
    }
83
    
84
    protected function registerMacros()
85
    {
86
        Collection::macro('mapIntoWithValues', function (string $class) {
87
            /**
88
             * @var Collection $this
89
             */
90
            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...
91
                return new $class($value);
92
            });
93
        });
94
    }
95
    
96
    protected function registerBaseTestClasses()
97
    {
98
        $baseClasses = [
99
            'test' => [
100
                'Unit' => BaseUnitTest::class,
101
            ],
102
            'cest' => [
103
                'Integration' => BaseIntegrationCest::class,
104
                'GraphQL'     => BaseGraphQLCest::class,
105
                'Acceptance'  => BaseAcceptanceCest::class,
106
            ],
107
            'cept' => [],
108
        ];
109
        
110
        foreach ($baseClasses as $type => $classesForType) {
111
            /**
112
             * @var CodeceptionTestType $type
113
             * @var string[]            $classesForType
114
             */
115
            $type = CodeceptionTestType::memberByKey($type);
116
            
117
            foreach ($classesForType as $suite => $baseClass) {
118
                BaseClassResolver::registerBaseClassForModule($type, $suite, BaseClassResolver::DEFAULT_MODULE, $baseClass);
119
            }
120
        }
121
    }
122
    
123
    protected function registerTemplates()
124
    {
125
        TemplateFactory::registerTemplate(
126
            CodeceptionTestType::test(),
127
            'Unit',
128
            $this->templatePath('test.unit.tpl')
129
        );
130
        
131
        TemplateFactory::registerTemplate(
132
            CodeceptionTestType::cest(),
133
            'Integration',
134
            $this->templatePath('cest.integration.tpl')
135
        );
136
        
137
        TemplateFactory::registerTemplate(
138
            CodeceptionTestType::cest(),
139
            'GraphQL',
140
            $this->templatePath('cest.graphql.tpl')
141
        );
142
    }
143
    
144
    protected function registerProviders()
145
    {
146
        $this->app->register(ExpressionLanguageServiceProvider::class);
147
    }
148
    
149
    /**
150
     * @param string $template
151
     *
152
     * @return string
153
     */
154
    protected function templatePath(string $template) : string
155
    {
156
        return TemplateFactory::DEFAULT_TEMPLATE_DIR . DIRECTORY_SEPARATOR . $template;
157
    }
158
    
159
    /**
160
     * Register the service provider.
161
     *
162
     * @return void
163
     */
164
    public function register()
165
    {
166
        $this->registerCommands();
167
    }
168
    
169
    /**
170
     * Get the services provided by the provider.
171
     *
172
     * @return array
173
     */
174
    public function provides()
175
    {
176
        return [];
177
    }
178
    
179
    protected function registerCommands()
180
    {
181
        $this->app->singleton(
182
            'command.smartweb.testing.generate-setup',
183
            GenerateSetup::class
184
        );
185
        
186
        $this->app->singleton(
187
            'command.smartweb.testing.generate-test',
188
            GenerateTest::class
189
        );
190
        
191
        $this->app->singleton(
192
            'command.smartweb.testing.generate-cest',
193
            GenerateCest::class
194
        );
195
        
196
        $this->commands(
197
            'command.smartweb.testing.generate-setup',
198
            'command.smartweb.testing.generate-test',
199
            'command.smartweb.testing.generate-cest');
200
    }
201
    
202
    /**
203
     * @param string[] ...$parts
204
     *
205
     * @return string
206
     */
207
    protected function path(...$parts) : string
208
    {
209
        return __DIR__ . '/../' . implode(DIRECTORY_SEPARATOR, $parts);
210
    }
211
}
212