BaseTest::setUp()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 0
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
        clearstatcache();
85
86
        //Open application scope
87
        TestApplication::shareContainer($this->app->container);
88
    }
89
90
    /**
91
     * This method performs full destroy of spiral environment.
92
     */
93
    public function tearDown()
94
    {
95
        \Mockery::close();
96
97
        TestApplication::shareContainer(null);
98
99
        //Forcing destruction
100
        $this->app = null;
101
102
        gc_collect_cycles();
103
        clearstatcache();
104
    }
105
106
    /**
107
     * @return \Spiral\Core\ContainerInterface
108
     */
109
    protected function iocContainer()
110
    {
111
        return $this->app->container;
112
    }
113
114
115
    /**
116
     * @param string $message
117
     * @param int    $code
118
     * @return Snapshot
119
     */
120
    protected function makeSnapshot(string $message, int $code): Snapshot
121
    {
122
        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...
123
            '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...
124
        ]);
125
    }
126
}