|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
use HexMakina\Crudites\Table\Row; |
|
5
|
|
|
use HexMakina\Crudites\Connection; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class RowTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private Row $row; |
|
11
|
|
|
|
|
12
|
|
|
// setup |
|
13
|
|
|
public function setUp(): void |
|
14
|
|
|
{ |
|
15
|
|
|
// code to execute before each test |
|
16
|
|
|
$connection = new Connection('mysql:host=localhost;dbname=crudites;charset=utf8', 'crudites', '2ce!fNe8(weVz3k4TN#'); |
|
17
|
|
|
|
|
18
|
|
|
$this->row = new Row($connection, 'users', ['id' => 1, 'name' => 'Test']); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testConstructor() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->assertEquals('users', $this->row->table()); |
|
24
|
|
|
$this->assertEquals(['id' => 1, 'name' => 'Test'], $this->row->export()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testGet() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->assertEquals('Test', $this->row->get('name')); |
|
30
|
|
|
$this->assertNull($this->row->get('non_existing')); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testSet() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->row->set('name', 'New Name'); |
|
36
|
|
|
$this->assertEquals('New Name', $this->row->get('name')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testIsNew() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertTrue($this->row->isNew()); |
|
42
|
|
|
$this->row->load(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testIsAltered() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->assertFalse($this->row->isAltered()); |
|
48
|
|
|
$this->row->set('name', 'New Name'); |
|
49
|
|
|
$this->assertTrue($this->row->isAltered()); |
|
50
|
|
|
} |
|
51
|
|
|
public function testExport() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->row->set('name', 'New Name'); |
|
54
|
|
|
$this->assertEquals(['id' => 1, 'name' => 'New Name'], $this->row->export()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testLoad() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->row->set('name', 'New Name'); |
|
60
|
|
|
|
|
61
|
|
|
$expected = [ |
|
62
|
|
|
'id' => 1, |
|
63
|
|
|
'name' => 'New Name', |
|
64
|
|
|
'username' => 'john_doe', |
|
65
|
|
|
'email' => '[email protected]' |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
$this->row->load(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($expected as $key => $value) { |
|
71
|
|
|
$this->assertEquals($value, $this->row->get($key)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->assertFalse($this->row->isNew()); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|