Test   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 88
ccs 21
cts 42
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actAs() 0 9 2
A tearDown() 0 4 1
A mock() 0 12 2
B __construct() 0 42 8
A setUp() 0 4 1
1
<?php
2
3
namespace Basis;
4
5
use Basis\Converter;
6
use Basis\Service;
7
use PHPUnit\Framework\TestCase;
8
use ReflectionProperty;
9
use Tarantool\Mapper\Mapper;
10
use Tarantool\Mapper\Pool;
11
12
abstract class Test extends TestCase
13
{
14
    use Toolkit;
15
16
    public $params = [];
17
    public $disableRemote = true;
18
19
    public $mocks = [];
20
    public $data = [];
21
22 3
    public function actAs($context)
23
    {
24 3
        if (is_numeric($context)) {
25
            $context = [
26 3
                'person' => $context,
27
            ];
28
        }
29 3
        return $this->get(Context::class)->reset($context);
30
    }
31
32 3
    public function __construct()
33
    {
34
        parent::__construct();
35
36
        $this->app = new Test\Application($this);
37
38
        foreach ($this->mocks as [$method, $params, $result]) {
39
            $this->mock($method, $params)->willReturn($result);
0 ignored issues
show
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
        }
41
42
        $serviceData = [];
43
        foreach ($this->data as $space => $data) {
44
            [$service, $space] = explode('.', $space);
0 ignored issues
show
Bug introduced by
The variable $service seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
45
            if (!array_key_exists($service, $serviceData)) {
0 ignored issues
show
Bug introduced by
The variable $service seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
46
                $serviceData[$service] = [];
0 ignored issues
show
Bug introduced by
The variable $service seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
47
            }
48
            $serviceData[$service][$space] = $data;
0 ignored issues
show
Bug introduced by
The variable $service seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
49
        }
50
51
        $pool = $this->app->get(Pool::class);
52
        $property = new ReflectionProperty(Pool::class, 'resolvers');
53
        $property->setAccessible(true);
54
        $property->setValue($pool, []);
55
56
        $service = $this->app->get(Service::class)->getName();
57
        $pool->register($service, function() {
58 1
            return $this->app->get(Mapper::class);
59
        });
60
61
        $pool->registerResolver(function($service) use ($serviceData) {
62 3
            if (array_key_exists($service, $serviceData)) {
63 1
                return new Test\Mapper($this, $service);
64
            }
65 3
        });
66
67
        foreach ($this->data ?: [] as $space => $rows) {
68
            $this->data[$space] = [];
69
            foreach ($rows as $row) {
70
                $pool->getRepository($space)->create($row)->save();
71
            }
72
        }
73
    }
74
75 56
    public function setUp(): void
76
    {
77 56
        $this->dispatch('tarantool.migrate');
78 56
    }
79
80 56
    public function tearDown(): void
81
    {
82 56
        $this->dispatch('tarantool.clear');
83 56
    }
84
85
    public $mockInstances = [];
86
87 5
    public function mock(string $job, array $params = [])
88
    {
89 5
        if (!array_key_exists($job, $this->mockInstances)) {
90 4
            $this->mockInstances[$job] = [];
91
        }
92
93 5
        $mock = new Test\Mock($this->get(Converter::class), $this, $params);
94
95 5
        $this->mockInstances[$job][] = $mock;
96
97 5
        return $mock;
98
    }
99
}
100