Passed
Push — master ( b9a200...e484b3 )
by Jonathan
10:09
created

MySQLiteTest::testSoundex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Vectorface\Tests\MySQLite;
4
5
use InvalidArgumentException;
6
use PDO;
7
use PDOException;
8
use PHPUnit\Framework\TestCase;
9
use Vectorface\MySQLite\MySQLite;
10
use Vectorface\Tests\MySQLite\Util\FakePDO;
11
12
/**
13
 * Test MySQLite; This could be split up into individual function categories later.
14
 */
15
class MySQLiteTest extends TestCase
16
{
17
    /**
18
     * Test miscellaneous compatibility functions.
19
     */
20
    public function testCompatibilityFunctions()
21
    {
22
        /* Aggregate functions */
23
        $this->assertEquals(1 | 2 | 4, MySQLite::mysql_bit_or(1, 2, 4));
24
25
        /* Comparison functions */
26
        $this->assertEquals(1, MySQLite::mysql_least(1, 2, 3, 4));
27
        try {
28
            MySQLite::mysql_least();
29
            $this->fail("Least with no arguments is not valid");
30
        } catch (\InvalidArgumentException $e) {
31
            /* Expected */
32
        }
33
34
        /* Flow control functions */
35
        $this->assertEquals("foo", MySQLite::mysql_if(true, "foo", "bar"));
36
        $this->assertEquals("bar", MySQLite::mysql_if(false, "foo", "bar"));
37
38
        /* Numeric functions */
39
        $this->assertEquals(10, MySQLite::mysql_sqrt(100));
40
    }
41
42
    public function testDateTimeFunctions()
43
    {
44
        $this->assertEquals(date("Y-m-d H:i:s"), MySQLite::mysql_now());
45
        $this->assertEquals(365, MySQLite::mysql_to_days("0000-12-31"));
46
        $this->assertEquals(718613, MySQLite::mysql_to_days("1967-07-01"));
47
        $this->assertEquals(735599, MySQLite::mysql_to_days("2014-01-01"));
48
        $this->assertEquals(time(), MySQLite::mysql_unix_timestamp());
49
        $this->assertEquals(0, MySQLite::mysql_unix_timestamp("0000-00-00 00:00:00"));
50
        $this->assertEquals(0, MySQLite::mysql_unix_timestamp("0000-00-00"));
51
        $time = time();
52
        $this->assertEquals($time, MySQLite::mysql_unix_timestamp(date("Y-m-d H:i:s")));
53
    }
54
55
    /**
56
     * Test that createFunctions hooks the functions into a PDO object.
57
     */
58
    public function testCreateFunctions()
59
    {
60
        $fakepdo = new FakePDO();
61
        $fakepdo->attributes[PDO::ATTR_DRIVER_NAME] = 'mysql';
62
63
        try {
64
            MySQLite::createFunctions($fakepdo);
65
            $this->fail("Attempt to create functions with a driver other than SQLite should fail.");
66
        } catch (InvalidArgumentException $e) {
67
            /* Expected */
68
        }
69
70
        $pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
71
        try {
72
            $pdo->query("SELECT BIT_OR(1, 2)");
73
            $this->fail("Attempt to BIT_OR two values is expected to fail before the function is created.");
74
        } catch (PDOException $e) {
75
            /* Expected */
76
        }
77
78
        $this->assertSame($pdo, MySQLite::createFunctions($pdo));
79
        $this->assertEquals(3, $pdo->query("SELECT BIT_OR(1, 2)")->fetch(PDO::FETCH_COLUMN));
80
    }
81
82
    /**
83
     * Test that createFunctions is able to create only a limited subset of supported functions.
84
     */
85
    public function testSelectiveCreateFunctions()
86
    {
87
        $pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
88
        $this->assertSame($pdo, MySQLite::createFunctions($pdo, ['bit_or']));
89
        $this->assertEquals(3, $pdo->query("SELECT BIT_OR(1, 2)")->fetch(PDO::FETCH_COLUMN));
90
        try {
91
            $pdo->query("SELECT UNIX_TIMESTAMP()");
92
            $this->fail("UNIX_TIMESTAMP function is expected not to have been created.");
93
        } catch (PDOException $e) {
94
            /* Expected */
95
        }
96
    }
97
98
    /**
99
     * Test that registered functions are listed and available.
100
     */
101
    public function testGetFunctionList()
102
    {
103
        $this->assertContains("bit_or", MySQLite::getFunctionList());
104
        $this->assertContains("unix_timestamp", MySQLite::getFunctionList());
105
    }
106
107
    /**
108
     * Test the concat function
109
     */
110
    public function testConcat()
111
    {
112
        $expected = 'test1 test2 test4';
113
        $test = MySQLite::mysql_concat("test1", " ", "test2", " ", "test4");
114
        $this->assertEquals($expected, $test);
115
116
        $pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
117
        MySQLite::createFunctions($pdo);
118
        $result = $pdo->query('SELECT CONCAT("test1"," ","test2"," " ,"test4")')->fetch(PDO::FETCH_COLUMN);
119
        $this->assertEquals($expected, $result);
120
    }
121
122
    /**
123
     * Test the concat_ws function
124
     */
125
    public function testConcatWS()
126
    {
127
        $expected = 'test1|test2|test4';
128
        $test = MySQLite::mysql_concat_ws("|", "test1", "test2", "test4");
129
        $this->assertEquals($expected, $test);
130
131
        $pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
132
        MySQLite::createFunctions($pdo);
133
        $result = $pdo->query('SELECT CONCAT_WS("|","test1","test2","test4")')->fetch(PDO::FETCH_COLUMN);
134
        $this->assertEquals($expected, $result);
135
    }
136
137
     /**
138
     * Test the rand function
139
     */
140
    public function testRand()
141
    {
142
        $pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
143
        MySQLite::createFunctions($pdo);
144
145
        $pdo->exec("CREATE TABLE testing(id INT PRIMARY KEY NOT NULL)");
146
        $stmt = $pdo->prepare("INSERT INTO testing (id) VALUES (?)");
147
        for ($x = 0; $x <= 10; $x++) {
148
            $stmt->execute([$x]);
149
        }
150
151
        $results = [];
152
        for ($x = 0; $x < 20; $x++) {
153
            $results[] = $pdo->query('SELECT id FROM testing ORDER BY RAND() LIMIT 1')->fetch(PDO::FETCH_COLUMN);
154
        }
155
156
        $this->assertNotEquals(
157
            array_slice($results, 0, 10),
158
            array_slice($results, 10, 10)
159
        );
160
    }
161
162
    public function testFormat()
163
    {
164
        $expected = '12,312,312';
165
        $test = MySQLite::mysql_format("12312312.232", 0);
166
        $this->assertEquals($expected, $test);
167
168
        $expected = '12.2';
169
        $test = MySQLite::mysql_format("12.232", 1);
170
        $this->assertEquals($expected, $test);
171
    }
172
173
    public function testSoundex()
174
    {
175
        $this->assertEquals("F000", MySQLite::mysql_soundex("foo"));
176
    }
177
}
178