CacheTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class CacheTest extends BaseTest
  21. {
  22. public function testFile()
  23. {
  24. $dir = sys_get_temp_dir() . '/google-api-php-client/tests';
  25. $client = $this->getClient();
  26. $client->setClassConfig(
  27. 'Google_Cache_File',
  28. 'directory',
  29. $dir
  30. );
  31. $cache = new Google_Cache_File($client);
  32. $cache->set('foo', 'bar');
  33. $this->assertEquals($cache->get('foo'), 'bar');
  34. $this->getSetDelete($cache);
  35. }
  36. /**
  37. * @requires extension Memcache
  38. */
  39. public function testNull()
  40. {
  41. $client = $this->getClient();
  42. $cache = new Google_Cache_Null($client);
  43. $client->setCache($cache);
  44. $cache->set('foo', 'bar');
  45. $cache->delete('foo');
  46. $this->assertEquals(false, $cache->get('foo'));
  47. $cache->set('foo.1', 'bar.1');
  48. $this->assertEquals($cache->get('foo.1'), false);
  49. $cache->set('foo', 'baz');
  50. $this->assertEquals($cache->get('foo'), false);
  51. $cache->set('foo', null);
  52. $cache->delete('foo');
  53. $this->assertEquals($cache->get('foo'), false);
  54. }
  55. /**
  56. * @requires extension Memcache
  57. */
  58. public function testMemcache()
  59. {
  60. $client = $this->getClient();
  61. if (!$client->getClassConfig('Google_Cache_Memcache', 'host')) {
  62. $this->markTestSkipped('Test requires memcache host specified');
  63. }
  64. $cache = new Google_Cache_Memcache($client);
  65. $this->getSetDelete($cache);
  66. }
  67. /**
  68. * @requires extension APC
  69. */
  70. public function testAPC()
  71. {
  72. if (!ini_get('apc.enable_cli')) {
  73. $this->markTestSkipped('Test requires APC enabled for CLI');
  74. }
  75. $client = $this->getClient();
  76. $cache = new Google_Cache_Apc($client);
  77. $this->getSetDelete($cache);
  78. }
  79. public function getSetDelete($cache)
  80. {
  81. $cache->set('foo', 'bar');
  82. $cache->delete('foo');
  83. $this->assertEquals(false, $cache->get('foo'));
  84. $cache->set('foo.1', 'bar.1');
  85. $cache->delete('foo.1');
  86. $this->assertEquals($cache->get('foo.1'), false);
  87. $cache->set('foo', 'baz');
  88. $cache->delete('foo');
  89. $this->assertEquals($cache->get('foo'), false);
  90. $cache->set('foo', null);
  91. $cache->delete('foo');
  92. $this->assertEquals($cache->get('foo'), false);
  93. $obj = new stdClass();
  94. $obj->foo = 'bar';
  95. $cache->set('foo', $obj);
  96. $cache->delete('foo');
  97. $this->assertEquals($cache->get('foo'), false);
  98. $cache->set('foo.1', 'bar.1');
  99. $this->assertEquals($cache->get('foo.1'), 'bar.1');
  100. $cache->set('foo', 'baz');
  101. $this->assertEquals($cache->get('foo'), 'baz');
  102. $cache->set('foo', null);
  103. $this->assertEquals($cache->get('foo'), null);
  104. $cache->set('1/2/3', 'bar');
  105. $this->assertEquals($cache->get('1/2/3'), 'bar');
  106. $obj = new stdClass();
  107. $obj->foo = 'bar';
  108. $cache->set('foo', $obj);
  109. $this->assertEquals($cache->get('foo'), $obj);
  110. }
  111. }