CustomFindsBehaviorTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. App::uses('CustomFindsBehavior', 'Tools.Model/Behavior');
  3. App::uses('AppModel', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. class CustomFindsBehaviorTest extends MyCakeTestCase {
  6. public function setUp() {
  7. parent::setUp();
  8. $this->CustomFinds = new CustomFindsBehavior();
  9. $this->Model = new CustomFindsTest();
  10. $this->Model->customFinds = [
  11. 'topSellers' => [
  12. 'fields' => ['Product.name', 'Product.price'],
  13. 'contain' => ['ProductImage.source'],
  14. 'conditions' => ['Product.countSeller >' => 20, 'Product.is_active' => 1],
  15. 'recursive' => 1,
  16. //All other find options
  17. ]
  18. ];
  19. }
  20. public function tearDown() {
  21. parent::tearDown();
  22. }
  23. public function testObject() {
  24. $this->assertInstanceOf('CustomFindsBehavior', $this->CustomFinds);
  25. }
  26. public function testModify() {
  27. $query = [
  28. 'custom' => 'topSellers',
  29. 'recursive' => 0,
  30. 'conditions' => ['Product.count >' => 0],
  31. ];
  32. $res = $this->Model->Behaviors->CustomFinds->beforeFind($this->Model, $query);
  33. //pr($res);
  34. $queryResult = $this->Model->customFinds['topSellers'];
  35. $queryResult['recursive'] = 0;
  36. $queryResult['conditions']['Product.count >'] = 0;
  37. $this->assertTrue(!empty($res));
  38. $this->assertSame($queryResult['recursive'], $res['recursive']);
  39. $this->assertSame($queryResult['conditions'], $res['conditions']);
  40. }
  41. public function testModifyWithRemove() {
  42. $query = [
  43. 'custom' => 'topSellers',
  44. 'conditions' => ['Product.count >' => 0],
  45. 'remove' => ['conditions']
  46. ];
  47. $res = $this->Model->Behaviors->CustomFinds->beforeFind($this->Model, $query);
  48. //pr($res);
  49. $queryResult = $this->Model->customFinds['topSellers'];
  50. $queryResult['conditions'] = ['Product.count >' => 0];
  51. $this->assertTrue(!empty($res));
  52. $this->assertSame($queryResult['recursive'], $res['recursive']);
  53. $this->assertSame($queryResult['conditions'], $res['conditions']);
  54. $query = [
  55. 'custom' => 'topSellers',
  56. 'conditions' => ['Product.count >' => 0],
  57. 'remove' => ['conditions' => ['Product.countSeller >']]
  58. ];
  59. $res = $this->Model->Behaviors->CustomFinds->beforeFind($this->Model, $query);
  60. //pr($res);
  61. $queryResult = $this->Model->customFinds['topSellers'];
  62. unset($queryResult['conditions']['Product.countSeller >']);
  63. $queryResult['conditions']['Product.count >'] = 0;
  64. $this->assertTrue(!empty($res));
  65. $this->assertSame($queryResult['conditions'], $res['conditions']);
  66. }
  67. }
  68. class CustomFindsTest extends AppModel {
  69. public $useTable = false;
  70. public $actsAs = ['Tools.CustomFinds'];
  71. }