2
0

SelectTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Test\Michel\SqlMapper;
  3. use Michel\SqlMapper\QueryBuilder;
  4. use Michel\SqlMapper\Select;
  5. use Michel\UniTester\TestCase;
  6. class SelectTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. // TODO: Implement setUp() method.
  11. }
  12. protected function tearDown(): void
  13. {
  14. // TODO: Implement tearDown() method.
  15. }
  16. protected function execute(): void
  17. {
  18. $this->testToStringOnlyReturnsSqlString();
  19. $this->testComplexQuery();
  20. $this->testFrom();
  21. $this->testWhere();
  22. $this->testGroupBy();
  23. $this->testHaving();
  24. $this->testDistinct();
  25. $this->testCount();
  26. }
  27. public function testToStringOnlyReturnsSqlString()
  28. {
  29. $select = new Select(['field1']);
  30. $select->from('table1', 't1');
  31. $select->where('condition1', 'condition2');
  32. $select->groupBy('groupField');
  33. $expectedSql = 'SELECT field1 FROM table1 AS t1 WHERE condition1 AND condition2 GROUP BY groupField';
  34. $this->assertEquals($expectedSql, (string) $select);
  35. }
  36. public function testComplexQuery()
  37. {
  38. $select = new Select(['field1']);
  39. $select->from('table1', 't1');
  40. $select->leftJoin('table2 t2 ON t1.id = t2.t1_id');
  41. $select->where('condition1', 'condition2');
  42. $select->orderBy('field2', 'DESC');
  43. $select->limit(10);
  44. $expectedSql = 'SELECT field1 FROM table1 AS t1 LEFT JOIN table2 t2 ON t1.id = t2.t1_id WHERE condition1 AND condition2 ORDER BY field2 DESC LIMIT 10';
  45. $this->assertEquals($expectedSql, (string) $select);
  46. $count = $select->count()
  47. ->on('t1.id', 'total', true)
  48. ->on('t2.id', 'total2')
  49. ;
  50. $this->assertEquals("SELECT COUNT(DISTINCT t1.id) AS total, COUNT(t2.id) AS total2 FROM table1 AS t1 LEFT JOIN table2 t2 ON t1.id = t2.t1_id WHERE condition1 AND condition2", (string) $count);
  51. }
  52. public function testFrom()
  53. {
  54. $select = new Select(['field1']);
  55. $select->from('table1', 't1');
  56. $this->assertEquals('SELECT field1 FROM table1 AS t1', (string)$select);
  57. }
  58. public function testWhere()
  59. {
  60. $select = new Select(['field1']);
  61. $select->where('condition1', 'condition2');
  62. $this->expectException(\LogicException::class , function () use ($select) {
  63. $select->__toString();
  64. });
  65. }
  66. public function testHaving()
  67. {
  68. $query = QueryBuilder::select('category_id', 'COUNT(*) as count')
  69. ->from('products')
  70. ->groupBy('category_id')
  71. ->having('COUNT(*) > 5');
  72. $this->assertEquals('SELECT category_id, COUNT(*) as count FROM products GROUP BY category_id HAVING COUNT(*) > 5', (string) $query);
  73. }
  74. public function testGroupBy()
  75. {
  76. $query = QueryBuilder::select('category_id', 'COUNT(*) as count')
  77. ->from('products')
  78. ->groupBy('category_id');
  79. $this->assertEquals('SELECT category_id, COUNT(*) as count FROM products GROUP BY category_id', (string) $query);
  80. }
  81. public function testDistinct()
  82. {
  83. $query = QueryBuilder::select('name', 'email')
  84. ->distinct()
  85. ->from('users')
  86. ->where('status = "active"')
  87. ->orderBy('name')
  88. ->limit(10);
  89. $this->assertEquals('SELECT DISTINCT name, email FROM users WHERE status = "active" ORDER BY name ASC LIMIT 10', (string) $query);
  90. }
  91. private function testCount()
  92. {
  93. $query = QueryBuilder::select('COUNT(p.id)')
  94. ->from('products', 'p')
  95. ->where('status = "active"')
  96. ->leftJoin('users u ON p.user_id = u.id');
  97. $this->assertEquals(
  98. 'SELECT COUNT(p.id) FROM products AS p LEFT JOIN users u ON p.user_id = u.id WHERE status = "active"',
  99. (string) $query
  100. );
  101. $query = QueryBuilder::select('COUNT(p.id)')
  102. ->distinct()
  103. ->from('products', 'p')
  104. ->where('status = "active"')
  105. ->leftJoin('users u ON p.user_id = u.id');
  106. $this->assertEquals(
  107. 'SELECT COUNT(DISTINCT p.id) FROM products AS p LEFT JOIN users u ON p.user_id = u.id WHERE status = "active"',
  108. (string) $query
  109. );
  110. $query = QueryBuilder::select('COUNT(p.id) AS total')
  111. ->distinct()
  112. ->from('products', 'p')
  113. ->where('status = "active"');
  114. $this->assertEquals(
  115. 'SELECT COUNT(DISTINCT p.id) AS total FROM products AS p WHERE status = "active"',
  116. (string) $query
  117. );
  118. $query = QueryBuilder::select('COUNT(DISTINCT p.id)')
  119. ->from('products', 'p')
  120. ->where('status = "active"');
  121. $this->assertEquals(
  122. 'SELECT COUNT(DISTINCT p.id) FROM products AS p WHERE status = "active"',
  123. (string) $query
  124. );
  125. $query = QueryBuilder::select('COUNT(DISTINCT p.id)')
  126. ->distinct()
  127. ->from('products', 'p')
  128. ->where('status = "active"');
  129. $this->assertEquals(
  130. 'SELECT COUNT(DISTINCT p.id) FROM products AS p WHERE status = "active"',
  131. (string) $query
  132. );
  133. }
  134. }