HexMakina /
Crudites
| 1 | <?php |
||
| 2 | use PHPUnit\Framework\TestCase; |
||
| 3 | use HexMakina\Crudites\Connection; |
||
| 4 | use HexMakina\Crudites\Schema\Schema; |
||
| 5 | use HexMakina\Crudites\Schema\SchemaAttribute; |
||
| 6 | use HexMakina\BlackBox\Database\SchemaAttributeInterface; |
||
|
0 ignored issues
–
show
|
|||
| 7 | |||
| 8 | |||
| 9 | class SchemaTest extends TestCase |
||
| 10 | { |
||
| 11 | private Connection $connection; |
||
| 12 | private Schema $schema; |
||
| 13 | // setup |
||
| 14 | public function setUp(): void |
||
| 15 | { |
||
| 16 | // code to execute before each test |
||
| 17 | |||
| 18 | $dsn = 'mysql:host=localhost;dbname=crudites;charset=utf8'; |
||
| 19 | $this->connection = new Connection($dsn, 'crudites', '2ce!fNe8(weVz3k4TN#'); |
||
| 20 | $this->schema = $this->connection->schema(); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testDatabase() |
||
| 24 | { |
||
| 25 | $this->assertEquals('crudites', $this->schema->database()); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testTables() |
||
| 29 | { |
||
| 30 | $schema = $this->connection->schema(); |
||
| 31 | $tables = $schema->tables(); |
||
| 32 | |||
| 33 | $this->assertIsArray($tables); |
||
| 34 | |||
| 35 | $this->assertEquals(6, count($tables)); |
||
| 36 | |||
| 37 | $this->assertContains('product_reviews', $tables); |
||
| 38 | $this->assertContains('users', $tables); |
||
| 39 | $this->assertContains('products', $tables); |
||
| 40 | $this->assertContains('data_types_table', $tables); |
||
| 41 | $this->assertContains('orders', $tables); |
||
| 42 | $this->assertContains('order_items', $tables); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testColumns() |
||
| 46 | { |
||
| 47 | $schema = $this->connection->schema(); |
||
| 48 | $columns = $schema->columns('users'); |
||
| 49 | |||
| 50 | $this->assertIsArray($columns); |
||
| 51 | |||
| 52 | $this->assertEquals(4, count($columns)); |
||
| 53 | |||
| 54 | $this->assertContains('id', $columns); |
||
| 55 | $this->assertContains('username', $columns); |
||
| 56 | $this->assertContains('email', $columns); |
||
| 57 | $this->assertContains('created_at', $columns); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testHasTable() |
||
| 61 | { |
||
| 62 | $schema = $this->connection->schema(); |
||
| 63 | $this->assertTrue($schema->hasTable('users')); |
||
| 64 | $this->assertFalse($schema->hasTable('non_existent_table')); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testHasColumn() |
||
| 68 | { |
||
| 69 | $schema = $this->connection->schema(); |
||
| 70 | $this->assertTrue($schema->hasColumn('users', 'id')); |
||
| 71 | $this->assertFalse($schema->hasColumn('users', 'non_existent_column')); |
||
| 72 | $this->assertFalse($schema->hasColumn('non_existent_table', 'non_existent_column')); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testColumn() |
||
| 76 | { |
||
| 77 | $column = $this->schema->column('users', 'id'); |
||
| 78 | $this->assertIsArray($column); |
||
| 79 | $this->assertArrayHasKey('table', $column); |
||
| 80 | $this->assertArrayHasKey('column', $column); |
||
| 81 | $this->assertEquals('users', $column['table']); |
||
| 82 | $this->assertEquals('id', $column['column']); |
||
| 83 | |||
| 84 | $this->expectException(\InvalidArgumentException::class); |
||
| 85 | $this->expectExceptionMessage('CANNOT FIND COLUMN non_existent_column IN TABLE users'); |
||
| 86 | $this->schema->column('users', 'non_existent_column'); |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | public function testAutoIncrementedPrimaryKey() |
||
| 91 | { |
||
| 92 | $this->assertEquals('id', $this->schema->autoIncrementedPrimaryKey('users')); |
||
| 93 | $this->assertEquals('product_id', $this->schema->autoIncrementedPrimaryKey('products')); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testPrimaryKeys() |
||
| 97 | { |
||
| 98 | $this->assertIsArray($this->schema->primaryKeys('users')); |
||
| 99 | $this->assertEquals(['review_id'], $this->schema->primaryKeys('product_reviews')); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testForeignKeys() |
||
| 103 | { |
||
| 104 | $res = $this->schema->foreignKeys('orders'); |
||
| 105 | $this->assertIsArray($res); |
||
| 106 | $this->assertEquals(['user_id' => ['users', 'id', 'NO ACTION', 'CASCADE']], $res); |
||
| 107 | |||
| 108 | $res = $this->schema->foreignKeys('order_items'); |
||
| 109 | $this->assertIsArray($res); |
||
| 110 | $this->assertEquals([ |
||
| 111 | 'order_id' => ['orders', 'order_id', 'CASCADE', 'CASCADE'], |
||
| 112 | 'product_id' => ['products', 'product_id', 'CASCADE', 'CASCADE']], $res); |
||
| 113 | |||
| 114 | $res = $this->schema->foreignKeys('products'); |
||
| 115 | $this->assertIsArray($res); |
||
| 116 | $this->assertEquals([], $res); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testUniqueKeys() |
||
| 120 | { |
||
| 121 | $res = $this->schema->uniqueKeys('users'); |
||
| 122 | |||
| 123 | $this->assertIsArray($res); |
||
| 124 | $this->assertArrayHasKey('username_unique', $res); |
||
| 125 | $this->assertArrayHasKey('email_unique', $res); |
||
| 126 | $this->assertEquals(['username'], $res['username_unique']); |
||
| 127 | $this->assertEquals(['email'], $res['email_unique']); |
||
| 128 | |||
| 129 | $res = $this->schema->uniqueKeys('products'); |
||
| 130 | $this->assertEquals([], $res); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testInsertValidData() |
||
| 134 | { |
||
| 135 | $valid_data = ['username' => 'test', 'email' => '[email protected]']; |
||
| 136 | $expected_bindings = ['users_username_0' => 'test', 'users_email_1' => '[email protected]']; |
||
| 137 | |||
| 138 | $insert = $this->schema->insert('users', $valid_data); |
||
| 139 | $this->assertEquals('INSERT INTO `users` (`username`,`email`) VALUES (:users_username_0,:users_email_1)', (string)$insert); |
||
| 140 | $this->assertEquals($expected_bindings, $insert->bindings()); |
||
| 141 | |||
| 142 | } |
||
| 143 | |||
| 144 | public function testInsertEmptyOrInvalidData() |
||
| 145 | { |
||
| 146 | $this->expectException(\InvalidArgumentException::class); |
||
| 147 | $this->expectExceptionMessage('EMPTY_DATA'); |
||
| 148 | $this->schema->insert('users', []); |
||
| 149 | |||
| 150 | // this is not ok as a test, some table might no require data for insert |
||
| 151 | // need a new table with all automated fields (auto_increment, current timestamp or nullable) |
||
| 152 | |||
| 153 | |||
| 154 | $this->expectException(\InvalidArgumentException::class); |
||
| 155 | $this->expectExceptionMessage('EMPTY_DATA'); |
||
| 156 | $this->schema->insert('users', ['non_existent_column' => 'test']); |
||
| 157 | |||
| 158 | } |
||
| 159 | } |
||
| 160 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths