SubqueryTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  3. App::uses('Controller', 'Controller');
  4. /**
  5. *
  6. * @link http://bakery.cakephp.org/articles/lucaswxp/2011/02/11/easy_and_simple_subquery_cakephp
  7. * @link http://www.sql-und-xml.de/sql-tutorial/subqueries-unterabfragen.html
  8. */
  9. class SubqueryTest extends MyCakeTestCase {
  10. public $fixtures = array('plugin.tools.country', 'plugin.tools.country_province');
  11. public $Model;
  12. public function setUp() {
  13. $this->Model = ClassRegistry::init('Tools.Country');
  14. parent::setUp();
  15. }
  16. /**
  17. * SubqueryTest::testSubquery()
  18. *
  19. * @return void
  20. */
  21. public function testSubquery() {
  22. $res = $this->Model->find('all', array('conditions' => array()));
  23. $this->debug(count($res));
  24. $this->assertEquals(10, count($res));
  25. $res = $this->Model->subquery('count');
  26. $this->debug($res);
  27. $res = $this->Model->find('all', array('conditions' => array('lat <=' => $this->Model->subquery('count'))));
  28. $this->debug(count($res));
  29. $this->assertEquals(0, count($res));
  30. $subqueryOptions = array('fields' => array('MAX(lat)'));
  31. $res = $this->Model->subquery('all', $subqueryOptions);
  32. $this->debug($res);
  33. $subqueryOptions = array('fields' => array('MAX(lat)'));
  34. $res = $this->Model->find('all', array('conditions' => array('lat <=' => $this->Model->subquery('first', $subqueryOptions))));
  35. $this->debug(count($res));
  36. $this->assertEquals(0, count($res));
  37. $subqueryOptions = array('fields' => array('id'), 'conditions' => array('id'=>1));
  38. $res = $this->Model->subquery('first', $subqueryOptions);
  39. $this->debug($res);
  40. $res = $this->Model->find('all', array('conditions' => array('id NOT IN '. $this->Model->subquery('all', $subqueryOptions))));
  41. $this->debug(count($res));
  42. $this->debug($res);
  43. $this->assertEquals(9, count($res));
  44. }
  45. /**
  46. * SubqueryTest::testSubqueryPaginated()
  47. *
  48. * @return void
  49. */
  50. public function testSubqueryPaginated() {
  51. $Controller = new CountriesTestsController(new CakeRequest(null, false), new CakeResponse());
  52. $Controller->constructClasses();
  53. $source = $Controller->Country->getDataSource();
  54. $database = $source->config['database'];
  55. $subquery = $Controller->Country->subquery('list', array('conditions' => array('NOT' => array('SubCountry.id' => array(1, 2, 3)))));
  56. $expected = '(SELECT SubCountry.id FROM `' . $database . '`.`countries` AS `SubCountry` WHERE NOT (`SubCountry`.`id` IN (1, 2, 3)))';
  57. $this->assertEquals($expected, $subquery);
  58. $res = $Controller->Country->query($subquery);
  59. $this->assertTrue(count($res) === 7);
  60. $Controller->paginate = array(
  61. 'conditions' => array('Country.id IN ' . $subquery)
  62. );
  63. $res = $Controller->paginate();
  64. $this->assertTrue(count($res) === 7);
  65. $this->debug($res);
  66. }
  67. }
  68. class CountriesTestsController extends Controller {
  69. public $uses = array('Tools.Country');
  70. }