• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / codeception / 7002289178

01 Aug 2023 06:33PM UTC coverage: 55.814% (+1.0%) from 54.857%
7002289178

push

github

f3l1x
fix: universal Nette\Caching\Storages\Journal cleaning (#37)

0 of 1 new or added line in 1 file covered. (0.0%)

96 of 172 relevant lines covered (55.81%)

1.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/Module/NetteDIModule.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Codeception\Module;
4

5
use Codeception\Module;
6
use Codeception\TestInterface;
7
use Nette\Bootstrap\Configurator;
8
use Nette\Caching\Cache;
9
use Nette\Caching\Storages\Journal;
10
use Nette\DI\Container;
11
use Nette\DI\Extensions\ExtensionsExtension;
12
use Nette\DI\MissingServiceException;
13
use Nette\Http\Session;
14
use Nette\Utils\FileSystem;
15

16
class NetteDIModule extends Module
17
{
18

19
        /** @var callable[] function(Container $configurator): void; */
20
        public $onCreateConfigurator = [];
21

22
        /** @var callable[] function(Container $container): void; */
23
        public $onCreateContainer = [];
24

25
        /** @var array<string, mixed> */
26
        protected array $config = [
27
                'configFiles' => [],
28
                'appDir' => null,
29
                'logDir' => null,
30
                'wwwDir' => null,
31
                'debugMode' => null,
32
                'removeDefaultExtensions' => false,
33
                'newContainerForEachTest' => false,
34
        ];
35

36
        /** @var string[] */
37
        protected array $requiredFields = [
38
                'tempDir',
39
        ];
40

41
        /** @var string */
42
        private $path;
43

44
        /** @var string[] */
45
        private $configFiles = [];
46

47
        /** @var Container|null */
48
        private $container;
49

50
        /**
51
         * @param array{path?: string} $settings
52
         * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
53
         */
54
        public function _beforeSuite(array $settings = []): void
55
        {
56
                assert(isset($settings['path']));
×
57

58
                $this->path = rtrim($settings['path'], '/');
×
59
                $this->clearTempDir();
×
60
        }
61

62
        public function _before(TestInterface $test): void
63
        {
64
                if ($this->config['newContainerForEachTest'] === true) {
×
65
                        $this->clearTempDir();
×
66
                        $this->container = null;
×
67
                        $this->configFiles = [];
×
68
                }
69
        }
70

71
        public function _afterSuite(): void
72
        {
73
                $this->stopContainer();
×
74
        }
75

76
        public function _after(TestInterface $test): void
77
        {
78
                if ($this->config['newContainerForEachTest'] === true) {
×
79
                        $this->stopContainer();
×
80
                }
81
        }
82

83
        /**
84
         * @param string[] $configFiles
85
         */
86
        public function useConfigFiles(array $configFiles): void
87
        {
88
                if ($this->config['newContainerForEachTest'] !== true) {
×
89
                        $this->fail('The useConfigFiles can only be used if the newContainerForEachTest option is set to true.');
×
90
                }
91

92
                if ($this->container !== null) {
×
93
                        $this->fail('Can\'t set configFiles after the container is created.');
×
94
                }
95

96
                $this->configFiles = $configFiles;
×
97
        }
98

99
        public function getContainer(): Container
100
        {
101
                if ($this->container === null) {
6✔
102
                        $this->createContainer();
1✔
103
                }
104

105
                return $this->container;
6✔
106
        }
107

108
        /**
109
         * @return object
110
         * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
111
         */
112
        public function grabService(string $service)
113
        {
114
                try {
115
                        /** @phpstan-var class-string $service */
116
                        return $this->getContainer()->getByType($service);
3✔
117
                } catch (MissingServiceException $e) {
×
118
                        $this->fail($e->getMessage());
×
119
                }
120
        }
121

122
        private function createContainer(): void
123
        {
124
                $configurator = new Configurator();
1✔
125
                if ($this->config['removeDefaultExtensions'] === true) {
1✔
126
                        $configurator->defaultExtensions = [
1✔
127
                                'extensions' => ExtensionsExtension::class,
1✔
128
                        ];
1✔
129
                }
130

131
                if ($this->config['logDir'] !== null) {
1✔
132
                        $logDir = $this->path . '/' . $this->config['logDir'];
×
133
                        FileSystem::createDir($logDir);
×
134
                        $configurator->enableTracy($logDir);
×
135
                }
136

137
                $configurator->addStaticParameters([
1✔
138
                        'appDir' => $this->path . ($this->config['appDir'] !== null ? '/' . $this->config['appDir'] : ''),
1✔
139
                        'wwwDir' => $this->path . ($this->config['wwwDir'] !== null ? '/' . $this->config['wwwDir'] : ''),
1✔
140
                ]);
1✔
141

142
                $this->clearTempDir();
1✔
143
                $tempDir = $this->getTempDir();
1✔
144
                $configurator->setTempDirectory($tempDir);
1✔
145

146
                if ($this->config['debugMode'] !== null) {
1✔
147
                        $configurator->setDebugMode((bool) $this->config['debugMode']);
×
148
                }
149

150
                /** @var iterable<string> $configFiles */
151
                $configFiles = $this->configFiles !== [] ? $this->configFiles : $this->config['configFiles'];
1✔
152
                foreach ($configFiles as $file) {
1✔
153
                        $configurator->addConfig(FileSystem::isAbsolute($file) ? $file : $this->path . '/' . $file);
1✔
154
                }
155

156
                foreach ($this->onCreateConfigurator as $callback) {
1✔
157
                        $callback($configurator);
×
158
                }
159

160
                $this->container = $configurator->createContainer();
1✔
161

162
                foreach ($this->onCreateContainer as $callback) {
1✔
163
                        $callback($this->container);
×
164
                }
165
        }
166

167

168
        private function getTempDir(): string
169
        {
170
                return $this->path . '/' . $this->config['tempDir'];
1✔
171
        }
172

173

174
        private function clearTempDir(): void
175
        {
176
                $this->deleteTempDir();
1✔
177

178
                $tempDir = $this->getTempDir();
1✔
179
                FileSystem::createDir($tempDir);
1✔
180
        }
181

182

183
        private function deleteTempDir(): void
184
        {
185
                $tempDir = $this->getTempDir();
1✔
186
                if (is_dir($tempDir)) {
1✔
187
                        FileSystem::delete(realpath($tempDir));
1✔
188
                }
189
        }
190

191

192
        private function stopContainer(): void
193
        {
194
                if ($this->container === null) {
×
195
                        return;
×
196
                }
197

198
                try {
199
                        $this->container->getByType(Session::class)->close();
×
200
                } catch (MissingServiceException $e) {
×
201
                        // Session is optional
202
                }
203

204
                try {
205
                        $journal = $this->container->getByType(Journal::class);
×
NEW
206
                        $journal->clean([Cache::All => true]);
×
207
                } catch (MissingServiceException $e) {
×
208
                        // IJournal is optional
209
                }
210

211
                $this->deleteTempDir();
×
212
        }
213

214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc