QueryCacherTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Datasource;
  16. use Cake\Cache\Cache;
  17. use Cake\Datasource\QueryCacher;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Query cacher test
  21. */
  22. class QueryCacherTest extends TestCase {
  23. /**
  24. * Setup method
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->engine = $this->getMock('Cake\Cache\CacheEngine');
  31. $this->engine->expects($this->any())
  32. ->method('init')
  33. ->will($this->returnValue(true));
  34. Cache::config('queryCache', $this->engine);
  35. Cache::enable();
  36. }
  37. /**
  38. * Teardown method
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. parent::tearDown();
  44. Cache::drop('queryCache');
  45. }
  46. /**
  47. * Test fetching with a function to generate the key.
  48. *
  49. * @return void
  50. */
  51. public function testFetchFunctionKey() {
  52. $this->_mockRead('my_key', 'A winner');
  53. $query = $this->getMock('stdClass');
  54. $cacher = new QueryCacher(function($q) use ($query) {
  55. $this->assertSame($query, $q);
  56. return 'my_key';
  57. }, 'queryCache');
  58. $result = $cacher->fetch($query);
  59. $this->assertEquals('A winner', $result);
  60. }
  61. /**
  62. * Test fetching with a function to generate the key but the function is poop.
  63. *
  64. * @expectedException \RuntimeException
  65. * @expectedExceptionMessage Cache key functions must return a string. Got false.
  66. * @return void
  67. */
  68. public function testFetchFunctionKeyNoString() {
  69. $this->_mockRead('my_key', 'A winner');
  70. $query = $this->getMock('stdClass');
  71. $cacher = new QueryCacher(function($q) {
  72. return false;
  73. }, 'queryCache');
  74. $cacher->fetch($query);
  75. }
  76. /**
  77. * Test fetching with a cache instance.
  78. *
  79. * @return void
  80. */
  81. public function testFetchCacheHitStringEngine() {
  82. $this->_mockRead('my_key', 'A winner');
  83. $cacher = new QueryCacher('my_key', 'queryCache');
  84. $query = $this->getMock('stdClass');
  85. $result = $cacher->fetch($query);
  86. $this->assertEquals('A winner', $result);
  87. }
  88. /**
  89. * Test fetching with a cache hit.
  90. *
  91. * @return void
  92. */
  93. public function testFetchCacheHit() {
  94. $this->_mockRead('my_key', 'A winner');
  95. $cacher = new QueryCacher('my_key', $this->engine);
  96. $query = $this->getMock('stdClass');
  97. $result = $cacher->fetch($query);
  98. $this->assertEquals('A winner', $result);
  99. }
  100. /**
  101. * Test fetching with a cache miss.
  102. *
  103. * @return void
  104. */
  105. public function testFetchCacheMiss() {
  106. $this->_mockRead('my_key', false);
  107. $cacher = new QueryCacher('my_key', $this->engine);
  108. $query = $this->getMock('stdClass');
  109. $result = $cacher->fetch($query);
  110. $this->assertNull($result, 'Cache miss should not have an isset() return.');
  111. }
  112. /**
  113. * Helper for building mocks.
  114. */
  115. protected function _mockRead($key, $value = false) {
  116. $this->engine->expects($this->any())
  117. ->method('read')
  118. ->with($key)
  119. ->will($this->returnValue($value));
  120. }
  121. }