BaseTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ log() 0 16 5
B setUp() 0 64 6
A tearDown() 0 12 1
A iocContainer() 0 4 1
A makeSnapshot() 0 6 1
1
<?php
2
3
namespace Spiral\Tests;
4
5
use Monolog\Handler\NullHandler;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\UriInterface;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\LoggerTrait;
11
use Psr\Log\LogLevel;
12
use Spiral\Core\Traits\SharedTrait;
13
use Spiral\Debug\Snapshot;
14
use Zend\Diactoros\ServerRequest;
15
16
/**
17
 * @property \Spiral\Core\MemoryInterface             $memory
18
 * @property \Spiral\Core\ContainerInterface          $container
19
 * @property \Spiral\Debug\LogsInterface              $logs
20
 * @property \Spiral\Http\HttpDispatcher              $http
21
 * @property \Spiral\Console\ConsoleDispatcher        $console
22
 * @property \Spiral\Console\ConsoleDispatcher        $commands
23
 * @property \Spiral\Files\FilesInterface             $files
24
 * @property \Spiral\Tokenizer\TokenizerInterface     $tokenizer
25
 * @property \Spiral\Tokenizer\ClassesInterface       $locator
26
 * @property \Spiral\Tokenizer\InvocationsInterface   $invocationLocator
27
 * @property \Spiral\Views\ViewManager                $views
28
 * @property \Spiral\Translator\Translator            $translator
29
 * @property \Spiral\Database\DatabaseManager         $dbal
30
 * @property \Spiral\ORM\ORM                          $orm
31
 * @property \Spiral\Encrypter\EncrypterInterface     $encrypter
32
 * @property \Spiral\Database\Entities\Database       $db
33
 * @property \Spiral\Http\Cookies\CookieQueue         $cookies
34
 * @property \Spiral\Http\Routing\RouterInterface     $router
35
 * @property \Spiral\Pagination\PaginatorsInterface   $paginators
36
 * @property \Psr\Http\Message\ServerRequestInterface $request
37
 * @property \Spiral\Http\Request\InputManager        $input
38
 * @property \Spiral\Http\Response\ResponseWrapper    $response
39
 * @property \Spiral\Http\Routing\RouteInterface      $route
40
 * @property \Spiral\Security\PermissionsInterface    $permissions
41
 * @property \Spiral\Security\RulesInterface          $rules
42
 * @property \Spiral\Security\ActorInterface          $actor
43
 * @property \Spiral\Session\SessionInterface         $session
44
 */
45
abstract class BaseTest extends TestCase
46
{
47
    use SharedTrait;
48
49
    /**
50
     * @var TestApplication
51
     */
52
    protected $app;
53
54
    public function setUp()
55
    {
56
        $root = __DIR__ . '/-app-/';
57
        $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\TestApplication> 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...
58
            [
59
                'root'        => $root,
60
                'libraries'   => dirname(__DIR__) . '/vendor/',
61
                'application' => $root,
62
                'framework'   => dirname(__DIR__) . '/source/',
63
                'runtime'     => $root . 'runtime/',
64
                'cache'       => $root . 'runtime/cache/',
65
            ],
66
            null,
67
            null,
68
            false
69
        );
70
71
        //Monolog love to write to CLI when no handler set
72
        $this->app->logs->debugHandler(new NullHandler());
73
        $this->app->container->bind('factory', $this->app->container);
74
75
        $files = $this->app->files;
76
77
        //Ensure runtime is clean
78
        foreach ($files->getFiles($this->app->directory('runtime')) as $filename) {
79
            //If exception is thrown here this will mean that application wasn't correctly
80
            //destructed and there is open resources kept
81
            $files->delete($filename);
82
        }
83
84
        $builder = $this->orm->schemaBuilder(true);
85
        $builder->renderSchema();
86
        $builder->pushSchema();
87
        $this->orm->setSchema($builder);
88
89
        if ($this->app->getEnvironment()->get('DEBUG')) {
90
            $this->app->db->getDriver()->setLogger(new class implements LoggerInterface
91
            {
92
                use LoggerTrait;
93
94
                public function log($level, $message, array $context = [])
95
                {
96
                    if ($level == LogLevel::ERROR) {
97
                        echo " \n! \033[31m" . $message . "\033[0m";
98
                    } elseif ($level == LogLevel::ALERT) {
99
                        echo " \n! \033[35m" . $message . "\033[0m";
100
                    } elseif (strpos($message, 'PRAGMA') === 0) {
101
                        echo " \n> \033[34m" . $message . "\033[0m";
102
                    } else {
103
                        if (strpos($message, 'SELECT') === 0) {
104
                            echo " \n> \033[32m" . $message . "\033[0m";
105
                        } else {
106
                            echo " \n> \033[33m" . $message . "\033[0m";
107
                        }
108
                    }
109
                }
110
            });
111
        }
112
113
        clearstatcache();
114
115
        //Open application scope
116
        TestApplication::shareContainer($this->app->container);
117
    }
118
119
    /**
120
     * This method performs full destroy of spiral environment.
121
     */
122
    public function tearDown()
123
    {
124
        \Mockery::close();
125
126
        TestApplication::shareContainer(null);
127
128
        //Forcing destruction
129
        $this->app = null;
130
131
        gc_collect_cycles();
132
        clearstatcache();
133
    }
134
135
    /**
136
     * @return \Spiral\Core\ContainerInterface
137
     */
138
    protected function iocContainer()
139
    {
140
        return $this->app->container;
141
    }
142
143
144
    /**
145
     * @param string $message
146
     * @param int    $code
147
     * @return Snapshot
148
     */
149
    protected function makeSnapshot(string $message, int $code): Snapshot
150
    {
151
        return $this->factory->make(Snapshot::class, [
0 ignored issues
show
Documentation introduced by
The property factory does not exist on object<Spiral\Tests\BaseTest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
            'exception' => new \Error($message, $code)
0 ignored issues
show
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with $message.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
153
        ]);
154
    }
155
}