|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use Smr\DatabaseRecord; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @covers \Smr\DatabaseRecord |
|
9
|
|
|
*/ |
|
10
|
|
|
class DatabaseRecordTest extends \PHPUnit\Framework\TestCase { |
|
11
|
|
|
|
|
12
|
|
|
public function test_hasField() { |
|
13
|
|
|
// Construct a record that has the field 'foo', but not 'bla' |
|
14
|
|
|
$record = new DatabaseRecord(['name' => 'value']); |
|
15
|
|
|
self::assertTrue($record->hasField('name')); |
|
16
|
|
|
self::assertFalse($record->hasField('does_not_exist')); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function test_getField() { |
|
20
|
|
|
// Construct a record with a string value |
|
21
|
|
|
$record = new DatabaseRecord(['name' => 'value_string']); |
|
22
|
|
|
self::assertSame('value_string', $record->getField('name')); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function test_getField_with_null_value() { |
|
26
|
|
|
// Construct a record with a null value |
|
27
|
|
|
$record = new DatabaseRecord(['name' => null]); |
|
28
|
|
|
self::assertSame(null, $record->getField('name')); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function test_getString() { |
|
32
|
|
|
// Construct a record with a string value |
|
33
|
|
|
$record = new DatabaseRecord(['name' => 'value_string']); |
|
34
|
|
|
self::assertSame('value_string', $record->getString('name')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function test_getString_with_null_value() { |
|
38
|
|
|
// Construct a record with a null value |
|
39
|
|
|
$record = new DatabaseRecord(['name' => null]); |
|
40
|
|
|
$this->expectException(\TypeError::class); |
|
41
|
|
|
$record->getString('name'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function test_getBoolean() { |
|
45
|
|
|
$record = new DatabaseRecord([ |
|
46
|
|
|
'name_true' => 'TRUE', |
|
47
|
|
|
'name_false' => 'FALSE', |
|
48
|
|
|
]); |
|
49
|
|
|
self::assertSame(true, $record->getBoolean('name_true')); |
|
50
|
|
|
self::assertSame(false, $record->getBoolean('name_false')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function test_getBoolean_with_non_boolean_field() { |
|
54
|
|
|
$record = new DatabaseRecord(['name' => 'NONBOOLEAN']); |
|
55
|
|
|
$this->expectException(\UnhandledMatchError::class); |
|
56
|
|
|
$record->getBoolean('name'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function test_getInt() { |
|
60
|
|
|
$record = new DatabaseRecord(['name' => '3']); |
|
61
|
|
|
self::assertSame(3, $record->getInt('name')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function test_getFloat() { |
|
65
|
|
|
$record = new DatabaseRecord(['name' => '3.14']); |
|
66
|
|
|
self::assertSame(3.14, $record->getFloat('name')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function test_getMicrotime() { |
|
70
|
|
|
// Construct a record with a numeric string |
|
71
|
|
|
$record = new DatabaseRecord(['name' => '123456789']); |
|
72
|
|
|
self::assertSame('123.456789', $record->getMicrotime('name')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function test_getObject() { |
|
76
|
|
|
// Construct a record with various types of objects |
|
77
|
|
|
$record = new DatabaseRecord([ |
|
78
|
|
|
'name' => serialize(new \ArrayObject(['a' => 1, 'b' => 2])), |
|
79
|
|
|
'name_null' => null, |
|
80
|
|
|
'name_compressed' => gzcompress(serialize(['c', 'd'])), |
|
81
|
|
|
]); |
|
82
|
|
|
// Class objects must be compared here with Equals instead of Same |
|
83
|
|
|
// since the two objects will not be the same instance. |
|
84
|
|
|
self::assertEquals(new \ArrayObject(['a' => 1, 'b' => 2]), $record->getObject('name')); |
|
85
|
|
|
self::assertSame(null, $record->getObject('name_null', nullable: true)); |
|
86
|
|
|
self::assertSame(['c', 'd'], $record->getObject('name_compressed', compressed: true)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function test_getRow() { |
|
90
|
|
|
// getRow returns the entire record |
|
91
|
|
|
$record = new DatabaseRecord(['name1' => '1', 'name2' => '2']); |
|
92
|
|
|
self::assertSame(['name1' => '1', 'name2' => '2'], $record->getRow()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|