-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathFunctionEvaluatorTest.php
More file actions
122 lines (103 loc) · 3.95 KB
/
Copy pathFunctionEvaluatorTest.php
File metadata and controls
122 lines (103 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace Vimeo\MysqlEngine\Tests;
use PHPUnit\Framework\TestCase;
class FunctionEvaluatorTest extends TestCase
{
public function tearDown() : void
{
\Vimeo\MysqlEngine\Server::reset();
}
/**
* @dataProvider maxValueProvider
*/
public function testSqlMax(string $sql, ?string $expected, bool $is_db_number) : void
{
$query = self::getConnectionToFullDB()->prepare($sql);
$query->execute();
/** @var array<array<string, string|null>> $result */
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
if ($is_db_number) {
$this->assertNotEmpty($result);
$this->assertNotNull($result[0]['max']);
} else {
$this->assertSame([['max' => $expected]], $result);
}
}
public static function maxValueProvider(): array
{
return [
'null when no rows' => [
'sql' => 'SELECT MAX(null) as `max` FROM `video_game_characters`',
'expected' => null,
'is_db_number' => false,
],
'max of scalar values' => [
'sql' => 'SELECT MAX(10) as `max` FROM `video_game_characters`',
'expected' => '10',
'is_db_number' => false,
],
'max in DB values' => [
'sql' => 'SELECT MAX(id) as `max` FROM `video_game_characters`',
'expected' => '',
'is_db_number' => true,
],
];
}
/**
* @dataProvider minValueProvider
*/
public function testSqlMin(string $sql, ?string $expected, bool $is_db_number) : void
{
$query = self::getConnectionToFullDB()->prepare($sql);
$query->execute();
/** @var array<array<string, string|null>> $result */
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
if ($is_db_number) {
$this->assertNotEmpty($result);
$this->assertNotNull($result[0]['min']);
} else {
$this->assertSame([['min' => $expected]], $result);
}
}
public static function minValueProvider(): array
{
return [
'null when no rows' => [
'sql' => 'SELECT MIN(null) as `min` FROM `video_game_characters`',
'expected' => null,
'is_db_number' => false,
],
'min of scalar values' => [
'sql' => 'SELECT MIN(10) as `min` FROM `video_game_characters`',
'expected' => '10',
'is_db_number' => false,
],
'min in DB values' => [
'sql' => 'SELECT MIN(id) as `min` FROM `video_game_characters`',
'expected' => '',
'is_db_number' => true,
],
];
}
private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
{
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];
if (\PHP_MAJOR_VERSION === 8) {
return new \Vimeo\MysqlEngine\Php8\FakePdo($connection_string, '', '', $options);
}
return new \Vimeo\MysqlEngine\Php7\FakePdo($connection_string, '', '', $options);
}
private static function getConnectionToFullDB(bool $emulate_prepares = true, bool $strict_mode = false) : \PDO
{
$pdo = self::getPdo('mysql:foo;dbname=test;', $strict_mode);
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $emulate_prepares);
// create table
$pdo->prepare(file_get_contents(__DIR__ . '/fixtures/create_table.sql'))->execute();
// insertData
$pdo->prepare(file_get_contents(__DIR__ . '/fixtures/bulk_character_insert.sql'))->execute();
$pdo->prepare(file_get_contents(__DIR__ . '/fixtures/bulk_enemy_insert.sql'))->execute();
$pdo->prepare(file_get_contents(__DIR__ . '/fixtures/bulk_tag_insert.sql'))->execute();
return $pdo;
}
}