HexMakina /
Crudites
| 1 | <?php |
||
| 2 | |||
| 3 | use PHPUnit\Framework\TestCase; |
||
| 4 | |||
| 5 | use HexMakina\Crudites\Grammar\Query\Select; |
||
| 6 | use HexMakina\Crudites\Grammar\Predicate; |
||
| 7 | |||
| 8 | class SelectTest extends TestCase |
||
| 9 | { |
||
| 10 | public function testConstructor() |
||
| 11 | { |
||
| 12 | $columns = ['id', 'contact' => 'email', 'person' => ['name']]; |
||
| 13 | $table = 'users'; |
||
| 14 | $table_alias = 'u'; |
||
| 15 | |||
| 16 | $select = new Select($columns, $table, $table_alias); |
||
| 17 | |||
| 18 | $this->assertEquals($table, $select->table()); |
||
| 19 | $this->assertEquals($table_alias, $select->alias()); |
||
| 20 | $this->assertEquals($table_alias, $select->base()); |
||
| 21 | $this->assertEquals('SELECT id,email AS `contact`,`name` AS `person` FROM `users` `u`', (string)$select); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testStatementWithTable() |
||
| 25 | { |
||
| 26 | $columns = ['id', 'name']; |
||
| 27 | $table = 'users'; |
||
| 28 | $select = new Select($columns, $table); |
||
| 29 | |||
| 30 | $expected = 'SELECT id,name FROM `users`'; |
||
| 31 | $this->assertEquals($expected, $select->statement()); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function testSelectAlso() |
||
| 35 | { |
||
| 36 | $columns = ['id', 'name']; |
||
| 37 | $table = 'users'; |
||
| 38 | $select = new Select($columns, $table); |
||
| 39 | |||
| 40 | $additional_columns = ['email', 'age']; |
||
| 41 | $select->selectAlso($additional_columns); |
||
| 42 | |||
| 43 | $expected = 'SELECT id,name,email,age FROM `users`'; |
||
| 44 | $this->assertEquals($expected, $select->statement()); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testSelectAlsoWithEmptyArray() |
||
| 48 | { |
||
| 49 | $this->expectException(\InvalidArgumentException::class); |
||
| 50 | $this->expectExceptionMessage('EMPTY_SETTER_ARRAY'); |
||
| 51 | |||
| 52 | $columns = ['id', 'name']; |
||
| 53 | $table = 'users'; |
||
| 54 | $select = new Select($columns, $table); |
||
| 55 | $select->selectAlso([]); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testSelectWhere() |
||
| 59 | { |
||
| 60 | $columns = ['id', 'name']; |
||
| 61 | $table = 'users'; |
||
| 62 | $select = new Select($columns, $table); |
||
| 63 | $where = $select->where([new Predicate('id', '=', 1)]); |
||
| 64 | |||
| 65 | $expected = 'SELECT id,name FROM `users` WHERE id = 1'; |
||
| 66 | $this->assertEquals($expected, (string)$select); |
||
| 67 | $this->assertEmpty($select->bindings()); |
||
| 68 | |||
| 69 | $this->expectException(\InvalidArgumentException::class); |
||
| 70 | $this->expectExceptionMessage('PREDICATE_REQUIRES_A_BIND_LABEL'); |
||
| 71 | $where->andPredicate((new Predicate('name', 'LIKE'))->withValue('John%')); |
||
| 72 | |||
| 73 | $where->andPredicate((new Predicate('name', 'LIKE'))->withValue('John%'), 'search_name'); |
||
|
0 ignored issues
–
show
|
|||
| 74 | $expected_bindings = ['name' => 'John%']; |
||
| 75 | $expected .= ' AND name LIKE :name'; |
||
| 76 | $this->assertEquals($expected, (string)$select); |
||
| 77 | $this->assertEquals($expected_bindings, $where->bindings()); |
||
| 78 | $this->assertEquals($expected_bindings, $select->bindings()); |
||
| 79 | |||
| 80 | |||
| 81 | $where->andIsNull(['email']); |
||
| 82 | $expected .= ' AND `email` IS NULL'; |
||
| 83 | |||
| 84 | $this->assertEquals($expected, (string)$select); |
||
| 85 | $this->assertEquals($expected_bindings, $where->bindings()); |
||
| 86 | $this->assertEquals($expected_bindings, $select->bindings()); |
||
| 87 | |||
| 88 | $where->andFields(['age' => 23]); |
||
| 89 | $expected_bindings['andFields_age'] = 23; |
||
| 90 | $expected .= ' AND `age` = :andFields_age'; |
||
| 91 | $this->assertEquals($expected, (string)$select); |
||
| 92 | $this->assertEquals($expected_bindings, $where->bindings()); |
||
| 93 | $this->assertEquals($expected_bindings, $select->bindings()); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testListingOrderItemsWithProduct() |
||
| 97 | { |
||
| 98 | $expected = 'SELECT quantity,price AS `sold_price` FROM `order_items` `oi`'; |
||
| 99 | $query = new Select(['quantity', 'sold_price' => 'price'], 'order_items', 'oi'); |
||
| 100 | $this->assertEquals($expected, (string)$query); |
||
| 101 | |||
| 102 | $query->join('products', 'p')->on('product_id', 'oi', 'product_id'); |
||
| 103 | $this->assertEquals($expected .' JOIN `products` `p` ON `p`.`product_id` = `oi`.`product_id`', (string)$query); |
||
| 104 | |||
| 105 | $query->selectAlso(['productName' => ['p', 'name']]); |
||
| 106 | $expected = 'SELECT quantity,price AS `sold_price`,`p`.`name` AS `productName` FROM `order_items` `oi` JOIN `products` `p` ON `p`.`product_id` = `oi`.`product_id`'; |
||
| 107 | $this->assertEquals($expected, (string)$query); |
||
| 108 | |||
| 109 | $query->join('orders', 'o')->on('order_id','oi', 'order_id'); |
||
| 110 | $expected .= ' JOIN `orders` `o` ON `o`.`order_id` = `oi`.`order_id`'; |
||
| 111 | $this->assertEquals($expected, (string)$query); |
||
| 112 | |||
| 113 | $query->selectAlso(['orderDate' => ['o', 'created_at']]); |
||
| 114 | } |
||
| 115 | } |
||
| 116 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.