anonymous//tests/BaseTest.php$0   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 21
c 0
b 0
f 0
rs 10
1
<?php
2
3
namespace Spiral\Tests;
4
5
use Monolog\Handler\NullHandler;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LoggerTrait;
9
use Psr\Log\LogLevel;
10
use Spiral\Core\Core;
11
use Spiral\Core\Traits\SharedTrait;
12
use Spiral\Statistics\Database\Sources\OccurrenceSource;
13
use Spiral\Statistics\DatetimeConverter;
14
use Spiral\Statistics\Extract;
15
use Spiral\Statistics\Track;
16
17
/**
18
 * @property \Spiral\Core\MemoryInterface             $memory
19
 * @property \Spiral\Core\ContainerInterface          $container
20
 * @property \Spiral\Debug\LogsInterface              $logs
21
 * @property \Spiral\Http\HttpDispatcher              $http
22
 * @property \Spiral\Console\ConsoleDispatcher        $console
23
 * @property \Spiral\Console\ConsoleDispatcher        $commands
24
 * @property \Spiral\Files\FilesInterface             $files
25
 * @property \Spiral\Tokenizer\TokenizerInterface     $tokenizer
26
 * @property \Spiral\Tokenizer\ClassesInterface       $locator
27
 * @property \Spiral\Tokenizer\InvocationsInterface   $invocationLocator
28
 * @property \Spiral\Views\ViewManager                $views
29
 * @property \Spiral\Translator\Translator            $translator
30
 * @property \Spiral\Database\DatabaseManager         $dbal
31
 * @property \Spiral\ORM\ORM                          $orm
32
 * @property \Spiral\Encrypter\EncrypterInterface     $encrypter
33
 * @property \Spiral\Database\Entities\Database       $db
34
 * @property \Spiral\Http\Cookies\CookieQueue         $cookies
35
 * @property \Spiral\Http\Routing\RouterInterface     $router
36
 * @property \Spiral\Pagination\PaginatorsInterface   $paginators
37
 * @property \Psr\Http\Message\ServerRequestInterface $request
38
 * @property \Spiral\Http\Request\InputManager        $input
39
 * @property \Spiral\Http\Response\ResponseWrapper    $response
40
 * @property \Spiral\Http\Routing\RouteInterface      $route
41
 * @property \Spiral\Security\PermissionsInterface    $permissions
42
 * @property \Spiral\Security\RulesInterface          $rules
43
 * @property \Spiral\Security\ActorInterface          $actor
44
 * @property \Spiral\Session\SessionInterface         $session
45
 */
46
abstract class BaseTest extends TestCase
47
{
48
    use SharedTrait;
49
50
    /**
51
     * @var TestApplication|Core
52
     */
53
    protected $app;
54
55
    public function setUp()
56
    {
57
        $root = __DIR__ . '/-app-/';
58
        $this->app = TestApplication::init(
0 ignored issues
show
Documentation Bug introduced by
It seems like \Spiral\Tests\TestApplic.../'), null, null, false) of type object<self> is incompatible with the declared type object<Spiral\Tests\Test...bject<Spiral\Core\Core> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
            [
60
                'root'        => $root,
61
                'libraries'   => dirname(__DIR__) . '/vendor/',
62
                'application' => $root,
63
                'framework'   => dirname(__DIR__) . '/source/',
64
                'runtime'     => $root . 'runtime/',
65
                'cache'       => $root . 'runtime/cache/',
66
            ],
67
            null,
68
            null,
69
            false
70
        );
71
72
        //Monolog love to write to CLI when no handler set
73
        $this->app->logs->debugHandler(new NullHandler());
74
        $this->app->container->bind('factory', $this->app->container);
75
76
        $files = $this->app->files;
77
78
        //Ensure runtime is clean
79
        foreach ($files->getFiles($this->app->directory('runtime')) as $filename) {
80
            //If exception is thrown here this will mean that application wasn't correctly
81
            //destructed and there is open resources kept
82
            $files->delete($filename);
83
        }
84
85
        $builder = $this->orm->schemaBuilder(true);
86
        $builder->renderSchema();
87
        $builder->pushSchema();
88
        $this->orm->setSchema($builder);
89
90
        if ($this->app->getEnvironment()->get('DEBUG')) {
91
            $this->app->db->getDriver()->setLogger(new class implements LoggerInterface
92
            {
93
                use LoggerTrait;
94
95
                public function log($level, $message, array $context = [])
96
                {
97
                    if ($level == LogLevel::ERROR) {
98
                        echo " \n! \033[31m" . $message . "\033[0m";
99
                    } elseif ($level == LogLevel::ALERT) {
100
                        echo " \n! \033[35m" . $message . "\033[0m";
101
                    } elseif (strpos($message, 'PRAGMA') === 0) {
102
                        echo " \n> \033[34m" . $message . "\033[0m";
103
                    } else {
104
                        if (strpos($message, 'SELECT') === 0) {
105
                            echo " \n> \033[32m" . $message . "\033[0m";
106
                        } else {
107
                            echo " \n> \033[33m" . $message . "\033[0m";
108
                        }
109
                    }
110
                }
111
            });
112
        }
113
114
        clearstatcache();
115
116
        //Open application scope
117
        TestApplication::shareContainer($this->app->container);
118
    }
119
120
    /**
121
     * This method performs full destroy of spiral environment.
122
     */
123
    public function tearDown()
124
    {
125
        \Mockery::close();
126
127
        TestApplication::shareContainer(null);
128
129
        //Forcing destruction
130
        $this->app = null;
131
132
        gc_collect_cycles();
133
        clearstatcache();
134
    }
135
136
    /**
137
     * @return \Spiral\Core\ContainerInterface
138
     */
139
    protected function iocContainer()
140
    {
141
        return $this->app->container;
142
    }
143
144
    /**
145
     * @return Track
146
     */
147
    protected function getTrack(): Track
148
    {
149
        return $this->container->get(Track::class);
150
    }
151
152
    /**
153
     * @return Extract
154
     */
155
    protected function getExtract(): Extract
156
    {
157
        return $this->container->get(Extract::class);
158
    }
159
160
    /**
161
     * @return DatetimeConverter
162
     */
163
    protected function getConverter(): DatetimeConverter
164
    {
165
        return $this->container->get(DatetimeConverter::class);
166
    }
167
168
    /**
169
     * @return OccurrenceSource
170
     */
171
    protected function getSource(): OccurrenceSource
172
    {
173
        return $this->container->get(OccurrenceSource::class);
174
    }
175
}