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_getBoolean() { |
32
|
|
|
$record = new DatabaseRecord([ |
33
|
|
|
'name_true' => 'TRUE', |
34
|
|
|
'name_false' => 'FALSE', |
35
|
|
|
]); |
36
|
|
|
self::assertSame(true, $record->getBoolean('name_true')); |
37
|
|
|
self::assertSame(false, $record->getBoolean('name_false')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function test_getBoolean_with_non_boolean_field() { |
41
|
|
|
$record = new DatabaseRecord(['name' => 'NONBOOLEAN']); |
42
|
|
|
$this->expectException(\UnhandledMatchError::class); |
43
|
|
|
$record->getBoolean('name'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_getInt() { |
47
|
|
|
$record = new DatabaseRecord(['name' => '3']); |
48
|
|
|
self::assertSame(3, $record->getInt('name')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test_getFloat() { |
52
|
|
|
$record = new DatabaseRecord(['name' => '3.14']); |
53
|
|
|
self::assertSame(3.14, $record->getFloat('name')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function test_getMicrotime() { |
57
|
|
|
// Construct a record with a numeric string |
58
|
|
|
$record = new DatabaseRecord(['name' => '123456789']); |
59
|
|
|
self::assertSame('123.456789', $record->getMicrotime('name')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_getObject() { |
63
|
|
|
// Construct a record with various types of objects |
64
|
|
|
$record = new DatabaseRecord([ |
65
|
|
|
'name' => serialize(new \ArrayObject(['a' => 1, 'b' => 2])), |
66
|
|
|
'name_null' => null, |
67
|
|
|
'name_compressed' => gzcompress(serialize(['c', 'd'])), |
68
|
|
|
]); |
69
|
|
|
// Class objects must be compared here with Equals instead of Same |
70
|
|
|
// since the two objects will not be the same instance. |
71
|
|
|
self::assertEquals(new \ArrayObject(['a' => 1, 'b' => 2]), $record->getObject('name')); |
72
|
|
|
self::assertSame(null, $record->getObject('name_null', nullable: true)); |
73
|
|
|
self::assertSame(['c', 'd'], $record->getObject('name_compressed', compressed: true)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function test_getRow() { |
77
|
|
|
// getRow returns the entire record |
78
|
|
|
$record = new DatabaseRecord(['name1' => '1', 'name2' => '2']); |
79
|
|
|
self::assertSame(['name1' => '1', 'name2' => '2'], $record->getRow()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|