MongoDBCacheTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use Doctrine\Common\Cache\MongoDBCache;
  5. use MongoClient;
  6. use MongoCollection;
  7. class MongoDBCacheTest extends CacheTest
  8. {
  9. /**
  10. * @var MongoCollection
  11. */
  12. private $collection;
  13. public function setUp()
  14. {
  15. if ( ! version_compare(phpversion('mongo'), '1.3.0', '>=')) {
  16. $this->markTestSkipped('The ' . __CLASS__ .' requires the use of mongo >= 1.3.0');
  17. }
  18. $mongo = new MongoClient();
  19. $this->collection = $mongo->selectCollection('doctrine_common_cache', 'test');
  20. }
  21. public function tearDown()
  22. {
  23. if ($this->collection instanceof MongoCollection) {
  24. $this->collection->drop();
  25. }
  26. }
  27. public function testSaveWithNonUtf8String()
  28. {
  29. // Invalid 2-octet sequence
  30. $data = "\xc3\x28";
  31. $cache = $this->_getCacheDriver();
  32. $this->assertTrue($cache->save('key', $data));
  33. $this->assertEquals($data, $cache->fetch('key'));
  34. }
  35. public function testGetStats()
  36. {
  37. $cache = $this->_getCacheDriver();
  38. $stats = $cache->getStats();
  39. $this->assertNull($stats[Cache::STATS_HITS]);
  40. $this->assertNull($stats[Cache::STATS_MISSES]);
  41. $this->assertGreaterThan(0, $stats[Cache::STATS_UPTIME]);
  42. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  43. $this->assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]);
  44. }
  45. protected function _getCacheDriver()
  46. {
  47. return new MongoDBCache($this->collection);
  48. }
  49. }