HexMakina /
Crudites
| 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]; |
||
| 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->name); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 35 | $this->assertNull($row->non_existing); |
||
|
0 ignored issues
–
show
The property
non_existing does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 36 | } |
||
| 37 | |||
| 38 | public function testSetGet() |
||
| 39 | { |
||
| 40 | $row = new Row($this->connection, $this->table, $this->data_form_with_id); |
||
| 41 | |||
| 42 | $row->username = 'New Username'; |
||
|
0 ignored issues
–
show
The property
username does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 43 | $this->assertEquals('New Username', $row->username); |
||
|
0 ignored issues
–
show
The property
username does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 44 | |||
| 45 | $row->name = 'New Name'; // cannot set a non existing column |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 46 | $this->assertEquals('Test', $row->name); |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 47 | |||
| 48 | $row->non_existing = 'New Value'; |
||
|
0 ignored issues
–
show
The property
non_existing does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 49 | $this->assertNull($row->non_existing); |
||
|
0 ignored issues
–
show
The property
non_existing does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 50 | ; |
||
| 51 | $this->assertNull($row->no_param); |
||
|
0 ignored issues
–
show
The property
no_param does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 52 | |||
| 53 | $row->null_param = null; |
||
|
0 ignored issues
–
show
The property
null_param does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 54 | $this->assertNull($row->null_param); |
||
|
0 ignored issues
–
show
The property
null_param does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 55 | |||
| 56 | $row->empty_string = ''; |
||
|
0 ignored issues
–
show
The property
empty_string does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 57 | $this->assertEquals('', $row->empty_string); |
||
|
0 ignored issues
–
show
The property
empty_string does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 58 | } |
||
| 59 | |||
| 60 | public function testIsNew() |
||
| 61 | { |
||
| 62 | $row = new Row($this->connection, $this->table, $this->data_form); |
||
| 63 | $this->assertTrue($row->isNew()); |
||
| 64 | $row->load(); |
||
| 65 | $this->assertFalse($row->isNew()); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testIsAltered() |
||
| 69 | { |
||
| 70 | $row = new Row($this->connection, $this->table, $this->data_form_with_id); |
||
| 71 | $this->assertFalse($row->isAltered()); |
||
| 72 | $row->name = 'New Name'; |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 73 | $this->assertFalse($row->isAltered()); |
||
| 74 | $row->username = 'New Username'; |
||
|
0 ignored issues
–
show
The property
username does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 75 | $this->assertTrue($row->isAltered()); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function testExport() |
||
| 79 | { |
||
| 80 | $row = new Row($this->connection, $this->table, $this->data_form_with_id); |
||
| 81 | $row->name= 'New Name'; |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 82 | $expected_export = ['id' => 1, 'name' => 'Test', 'username' => 'john_doe']; |
||
| 83 | $this->assertEquals($expected_export, $row->export()); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function testLoad() |
||
| 87 | { |
||
| 88 | $row = new Row($this->connection, $this->table, $this->data_form_with_id); |
||
| 89 | $row->name = 'New Name'; |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 90 | |||
| 91 | $expected = [ |
||
| 92 | 'id' => 1, |
||
| 93 | 'name' => 'Test', |
||
| 94 | 'username' => 'john_doe', |
||
| 95 | 'email' => '[email protected]' |
||
| 96 | ]; |
||
| 97 | |||
| 98 | $row->load(); |
||
| 99 | |||
| 100 | foreach ($expected as $key => $value) { |
||
| 101 | $this->assertEquals($value, $row->$key); |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->assertFalse($row->isNew()); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function testImport() |
||
| 108 | { |
||
| 109 | $row = new Row($this->connection, $this->table, $this->data_form_with_id); |
||
| 110 | $row->import(['name' => 'New Name']); |
||
| 111 | $this->assertFalse($row->isAltered()); |
||
| 112 | $this->assertEquals('Test', $row->name); |
||
|
0 ignored issues
–
show
The property
name does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 113 | |||
| 114 | $row->import(['username' => __FUNCTION__, 'email' => __FUNCTION__, 'invalid' => __FUNCTION__]); |
||
| 115 | $this->assertTrue($row->isAltered()); |
||
| 116 | $this->assertEquals(__FUNCTION__, $row->username); |
||
|
0 ignored issues
–
show
The property
username does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 117 | $this->assertEquals(__FUNCTION__, $row->email); |
||
|
0 ignored issues
–
show
The property
email does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 118 | $this->assertNull($row->invalid); |
||
|
0 ignored issues
–
show
The property
invalid does not exist on HexMakina\Crudites\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 119 | } |
||
| 120 | } |
||
| 121 |