Test Failed
Pull Request — master (#35)
by Timon
04:18
created

CursorTest::testCursor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
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
    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)) {
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
        $this->insertBigDocuments();
52
53
        $cursor = $this->r()->table('tabletest_big')->run();
54
55
        $i = 0;
56
        foreach ($cursor as $document) {
57
            $i++;
58
        }
59
60
        $this->assertEquals($i, $this->r()->table('tabletest_big')->count()->run()->getData());
61
62
        $this->assertInstanceOf(Cursor::class, $cursor);
63
64
        $this->r()->table('tabletest_big')->delete()->run();
65
    }
66
67
    public function testMultipleCusors()
68
    {
69
        $this->insertBigDocuments();
70
71
        $this->assertEquals(20000, $this->r()->table('tabletest_big')->count()->run()->getData());
72
73
        $this->r()->table('tabletest_big')->delete()->run();
74
    }
75
76
    /**
77
     * @return ResponseInterface
78
     * @throws \Exception
79
     */
80
    private function insertDocuments(): ResponseInterface
81
    {
82
        $documents = [];
83
84
        for ($i = 1; $i <= 1000; $i++) {
85
            $documents[] = [
86
                'id' => $i,
87
                'title' => 'Test document',
88
                'description' => 'My first document.',
89
            ];
90
        }
91
92
        /** @var ResponseInterface $res */
93
        $res = $this->r()
94
            ->table('tabletest')
95
            ->insert($documents)
96
            ->run();
97
98
        $this->assertEquals(1000, $res->getData()['inserted']);
99
100
        return $res;
101
    }
102
103
    /**
104
     * @return ResponseInterface
105
     * @throws \Exception
106
     */
107
    private function insertBigDocuments(): ResponseInterface
108
    {
109
        for ($i = 1; $i <= 100; $i++) {
110
            $documents = [];
111
112
            for ($x = 1; $x <= 100; $x++) {
113
                $documents[] = [
114
                    'Professor X' => 'Charles Francis Xavier',
115
                    'Cyclops' => 'Scott Summers',
116
                    'Iceman' => 'Robert Louis "Bobby" Drake',
117
                    'Angel' => 'Warren Kenneth Worthington III',
118
                    'Beast' => 'Henry Philip "Hank" McCoy',
119
                    'Marvel Girl/Phoenix' => 'Jean Elaine Grey/Jean Elaine Grey-Summers',
120
                    'Magnetrix/Polaris' => 'Lorna Sally Dane',
121
                    'Nightcrawler' => 'Kurt Wagner',
122
                    'Wolverine' => 'James "Logan" Howlett',
123
                    'Storm' => 'Ororo Monroe',
124
                    'Colossus' => 'Piotr Nikolaievitch "Peter" Rasputin',
125
                    'Sprite/Ariel/Shadowcat' => 'Katherine Anne "Kitty" Pryde',
126
                    'Rogue' => 'Anna Marie',
127
                    'Phoenix/Marvel Girl/Prestige' => 'Rachel Anne Grey-Summers',
128
                    'Psylocke' => 'Elizabeth "Betsy" Braddock',
129
                    'Gambit' => 'Rémy Etienne LeBeau',
130
                    'Jubilee' => 'Jubilation Lee',
131
                    'Bishop' => 'Lucas Bishop',
132
                ];
133
            }
134
135
            /** @var ResponseInterface $res */
136
            $res = $this->r()
137
                ->table('tabletest_big')
138
                ->insert($documents)
139
                ->run();
140
141
            $this->assertEquals(100, $res->getData()['inserted']);
142
        }
143
144
        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...
145
    }
146
}
147