Passed
Pull Request — master (#35)
by Michel
04:04
created

CursorTest::insertBigDocuments()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 28
nc 3
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Query;
5
6
use TBolier\RethinkQL\Response\Cursor;
7
use TBolier\RethinkQL\Response\ResponseInterface;
8
9
class CursorTest extends AbstractTableTest
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function testCursor()
15
    {
16
        $this->insertDocuments();
17
18
        $response = $this->r()
19
            ->table('tabletest')
20
            ->count()
21
            ->run();
22
23
        $this->assertEquals(1000, $response->getData());
24
    }
25
26
    /**
27
     * @throws \Exception
28
     */
29
    public function testCursorBigDocuments()
30
    {
31
        $res = $this->r()->db()->tableList()->run();
32
        if (\is_array($res->getData()) && !\in_array('tabletest_big', $res->getData(), true)) {
33
            $this->r()->db()->tableCreate('tabletest_big')->run();
34
        }
35
36
        $this->insertBigDocuments();
37
38
        /** @var Cursor $response */
39
        $response = $this->r()->table('tabletest_big')->run();
40
41
        $documents = [];
42
43
        foreach ($response as $result) {
44
            $documents[] = $result;
45
        }
46
        $this->assertCount(10000, $documents);
47
48
        $res = $this->r()->db()->tableList()->run();
49
        if (\is_array($res->getData()) && \in_array('tabletest_big', $res->getData(), true)) {
50
            $this->r()->db()->tableDrop('tabletest_big')->run();
51
        }
52
    }
53
54
    /**
55
     * @return ResponseInterface
56
     * @throws \Exception
57
     */
58
    private function insertDocuments(): ResponseInterface
59
    {
60
        $documents = [];
61
62
        for ($i = 1; $i <= 1000; $i++) {
63
            $documents[] = [
64
                'id' => $i,
65
                'title' => 'Test document',
66
                'description' => 'My first document.',
67
            ];
68
        }
69
70
        /** @var ResponseInterface $res */
71
        $res = $this->r()
72
            ->table('tabletest')
73
            ->insert($documents)
74
            ->run();
75
76
        $this->assertEquals(1000, $res->getData()['inserted']);
77
78
        return $res;
79
    }
80
81
    /**
82
     * @return ResponseInterface
83
     * @throws \Exception
84
     */
85
    private function insertBigDocuments(): ResponseInterface
86
    {
87
        for ($i = 1; $i <= 100; $i++) {
88
            $documents = [];
89
90
            for ($x = 1; $x <= 100; $x++) {
91
                $documents[] = [
92
                    'Professor X' => 'Charles Francis Xavier',
93
                    'Cyclops' => 'Scott Summers',
94
                    'Iceman' => 'Robert Louis "Bobby" Drake',
95
                    'Angel' => 'Warren Kenneth Worthington III',
96
                    'Beast' => 'Henry Philip "Hank" McCoy',
97
                    'Marvel Girl/Phoenix' => 'Jean Elaine Grey/Jean Elaine Grey-Summers',
98
                    'Magnetrix/Polaris' => 'Lorna Sally Dane',
99
                    'Nightcrawler' => 'Kurt Wagner',
100
                    'Wolverine' => 'James "Logan" Howlett',
101
                    'Storm' => 'Ororo Monroe',
102
                    'Colossus' => 'Piotr Nikolaievitch "Peter" Rasputin',
103
                    'Sprite/Ariel/Shadowcat' => 'Katherine Anne "Kitty" Pryde',
104
                    'Rogue' => 'Anna Marie',
105
                    'Phoenix/Marvel Girl/Prestige' => 'Rachel Anne Grey-Summers',
106
                    'Psylocke' => 'Elizabeth "Betsy" Braddock',
107
                    'Gambit' => 'Rémy Etienne LeBeau',
108
                    'Jubilee' => 'Jubilation Lee',
109
                    'Bishop' => 'Lucas Bishop',
110
                ];
111
            }
112
113
            /** @var ResponseInterface $res */
114
            $res = $this->r()
115
                ->table('tabletest_big')
116
                ->insert($documents)
117
                ->run();
118
119
            $this->assertEquals(100, $res->getData()['inserted']);
120
        }
121
122
        return $res;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.
Loading history...
123
    }
124
}
125