Passed
Branch main (b4b05f)
by Sammy
03:19
created

RowTest::testSetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.9
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use HexMakina\Crudites\Row;
5
use HexMakina\Crudites\Connection;
6
7
8
class RowTest extends TestCase
9
{
10
    private Connection $connection;
11
12
    private string $table = 'users';
13
    private array $data_pk_match = ['id' => 1];
0 ignored issues
show
introduced by
The private property $data_pk_match is not used, and could be removed.
Loading history...
14
    private array $data_form = ['name' => 'Test', 'username' => 'john_doe'];
15
    private array $data_form_with_id = ['name' => 'Test', 'username' => 'john_doe'] + ['id' => 1];
16
    
17
    // setup
18
    public function setUp(): void
19
    {
20
        // code to execute before each test
21
        $this->connection = new Connection('mysql:host=localhost;dbname=crudites;charset=utf8', 'crudites', '2ce!fNe8(weVz3k4TN#');
22
    }
23
24
    public function testConstructor()
25
    {
26
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
27
        $this->assertEquals('users', $row->table());
28
        $this->assertEquals($this->data_form_with_id, $row->export());
29
    }
30
31
    public function testGet()
32
    {
33
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
34
        $this->assertEquals('Test', $row->get('name'));
35
        $this->assertNull($row->get('non_existing'));
36
    }
37
38
    public function testSetGet()
39
    {
40
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
41
        $row->set('name', 'New Name');
42
        $this->assertEquals('New Name', $row->get('name'));
43
44
        $row->set('non_existing', 'New Value');
45
        $this->assertEquals('New Value', $row->get('non_existing'));
46
47
        $row->set('no_param');
48
        $this->assertNull($row->get('no_param'));
49
50
        $row->set('null_param', null);
51
        $this->assertNull($row->get('null_param'));
52
53
        $row->set('empty_string', '');
54
        $this->assertEquals('', $row->get('empty_string'));
55
    }
56
57
    public function testIsNew()
58
    {
59
        $row = new Row($this->connection, $this->table, $this->data_form);
60
        $this->assertTrue($row->isNew());
61
        $row->load();
62
    }
63
64
    public function testIsAltered()
65
    {
66
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
67
        $this->assertFalse($row->isAltered());
68
        $row->set('name', 'New Name');
69
        $this->assertTrue($row->isAltered());
70
    }
71
72
    public function testExport()
73
    {
74
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
75
        $row->set('name', 'New Name');
76
        $expected_export = ['id' => 1, 'name' => 'New Name', 'username' => 'john_doe'];
77
        $this->assertEquals($expected_export, $row->export());
78
    }
79
80
    public function testLoad()
81
    {
82
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
83
        $row->set('name', 'New Name');
84
85
        $expected = [
86
            'id' => 1,
87
            'name' => 'New Name',
88
            'username' => 'john_doe',
89
            'email' => '[email protected]'
90
        ];
91
        
92
        $row->load();
93
94
        foreach ($expected as $key => $value) {
95
            $this->assertEquals($value, $row->get($key));
96
        }
97
98
        $this->assertFalse($row->isNew());
99
    }
100
101
    public function testAlter()
102
    {
103
        $row = new Row($this->connection, $this->table, $this->data_form_with_id);
104
        $row->alter(['name' => 'New Name']);
105
        $this->assertFalse($row->isAltered());
106
        $this->assertEquals('Test', $row->get('name'));
107
        
108
        $row->alter(['username' => __FUNCTION__, 'email' => __FUNCTION__, 'invalid' => __FUNCTION__]);
109
        $this->assertTrue($row->isAltered());
110
        $this->assertEquals(__FUNCTION__, $row->get('username'));
111
        $this->assertEquals(__FUNCTION__, $row->get('email'));
112
        $this->assertNull($row->get('invalid'));
113
    }
114
}
115