CursorTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 67
dl 0
loc 134
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A insertDocuments() 0 20 2
A testCursor() 0 10 1
A tearDown() 0 7 3
A insertBigDocuments() 0 20 3
A setUp() 0 7 3
A testCursorBigDocuments() 0 41 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Operation;
5
6
use TBolier\RethinkQL\Response\Cursor;
7
use TBolier\RethinkQL\Response\ResponseInterface;
8
9
class CursorTest extends AbstractTableTest
10
{
11
    public function setUp()
12
    {
13
        parent::setUp();
14
15
        $res = $this->r()->db()->tableList()->run();
16
        if (\is_array($res->getData()) && !\in_array('tabletest_big', $res->getData(), true)) {
0 ignored issues
show
Bug introduced by
The method getData() does not exist on TBolier\RethinkQL\Response\Cursor. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        if (\is_array($res->/** @scrutinizer ignore-call */ getData()) && !\in_array('tabletest_big', $res->getData(), true)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
            $this->r()->db()->tableCreate('tabletest_big')->run();
18
        }
19
    }
20
21
    public function tearDown()
22
    {
23
        parent::tearDown();
24
25
        $res = $this->r()->db()->tableList()->run();
26
        if (\is_array($res->getData()) && \in_array('tabletest_big', $res->getData(), true)) {
27
            $this->r()->db()->tableDrop('tabletest_big')->run();
28
        }
29
    }
30
31
    /**
32
     * @throws \Exception
33
     */
34
    public function testCursor()
35
    {
36
        $this->insertDocuments();
37
38
        $response = $this->r()
39
            ->table('tabletest')
40
            ->count()
41
            ->run();
42
43
        $this->assertEquals(1000, $response->getData());
44
    }
45
46
    /**
47
     * @throws \Exception
48
     */
49
    public function testCursorBigDocuments()
50
    {
51
        $data = [
52
            'Professor X' => 'Charles Francis Xavier',
53
            'Cyclops' => 'Scott Summers',
54
            'Iceman' => 'Robert Louis "Bobby" Drake',
55
            'Angel' => 'Warren Kenneth Worthington III',
56
            'Beast' => 'Henry Philip "Hank" McCoy',
57
            'Marvel Girl/Phoenix' => 'Jean Elaine Grey/Jean Elaine Grey-Summers',
58
            'Magnetrix/Polaris' => 'Lorna Sally Dane',
59
            'Nightcrawler' => 'Kurt Wagner',
60
            'Wolverine' => 'James "Logan" Howlett',
61
            'Storm' => 'Ororo Monroe',
62
            'Colossus' => 'Piotr Nikolaievitch "Peter" Rasputin',
63
            'Sprite/Ariel/Shadowcat' => 'Katherine Anne "Kitty" Pryde',
64
            'Rogue' => 'Anna Marie',
65
            'Phoenix/Marvel Girl/Prestige' => 'Rachel Anne Grey-Summers',
66
            'Psylocke' => 'Elizabeth "Betsy" Braddock',
67
            'Gambit' => 'Rémy Etienne LeBeau',
68
            'Jubilee' => 'Jubilation Lee',
69
            'Bishop' => 'Lucas Bishop',
70
        ];
71
72
        $this->insertBigDocuments($data);
73
74
        $cursor = $this->r()->table('tabletest_big')->run();
75
76
        $i = 0;
77
        foreach ($cursor as $document) {
78
            // Assert the document every 1000s documents.
79
            if ($i % 1000 === 0) {
80
                $this->assertArraySubset($data, $document);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
                /** @scrutinizer ignore-deprecated */ $this->assertArraySubset($data, $document);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
81
            }
82
            $i++;
83
        }
84
85
        $this->assertEquals($i, $this->r()->table('tabletest_big')->count()->run()->getData());
86
87
        $this->assertInstanceOf(Cursor::class, $cursor);
88
89
        $this->r()->table('tabletest_big')->delete()->run();
90
    }
91
92
    /**
93
     * @return ResponseInterface
94
     * @throws \Exception
95
     */
96
    private function insertDocuments(): ResponseInterface
97
    {
98
        $documents = [];
99
        for ($i = 1; $i <= 1000; $i++) {
100
            $documents[] = [
101
                'id' => $i,
102
                'title' => 'Test document',
103
                'description' => 'My first document.',
104
            ];
105
        }
106
107
        /** @var ResponseInterface $res */
108
        $res = $this->r()
109
            ->table('tabletest')
110
            ->insert($documents)
111
            ->run();
112
113
        $this->assertEquals(1000, $res->getData()['inserted']);
114
115
        return $res;
116
    }
117
118
    /**
119
     * @param array $data
120
     * @return ResponseInterface
121
     * @throws \Exception
122
     */
123
    private function insertBigDocuments(array $data): ResponseInterface
124
    {
125
        $documents = [];
126
        for ($i = 1; $i <= 100; $i++) {
127
            $documents = [];
128
129
            for ($x = 1; $x <= 100; $x++) {
130
                $documents[] = $data;
131
            }
132
        }
133
        
134
        /** @var ResponseInterface $res */
135
        $res = $this->r()
136
            ->table('tabletest_big')
137
            ->insert($documents)
138
            ->run();
139
140
        $this->assertEquals(100, $res->getData()['inserted']);
141
142
        return $res;
143
    }
144
}
145