MongoDBCacheTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /**
  8. * @requires extension mongo
  9. */
  10. class MongoDBCacheTest extends CacheTest
  11. {
  12. /**
  13. * @var MongoCollection
  14. */
  15. private $collection;
  16. protected function setUp()
  17. {
  18. if ( ! version_compare(phpversion('mongo'), '1.3.0', '>=')) {
  19. $this->markTestSkipped('Mongo >= 1.3.0 is required.');
  20. }
  21. $mongo = new MongoClient();
  22. $this->collection = $mongo->selectCollection('doctrine_common_cache', 'test');
  23. }
  24. protected function tearDown()
  25. {
  26. if ($this->collection instanceof MongoCollection) {
  27. $this->collection->drop();
  28. }
  29. }
  30. public function testGetStats()
  31. {
  32. $cache = $this->_getCacheDriver();
  33. $stats = $cache->getStats();
  34. $this->assertNull($stats[Cache::STATS_HITS]);
  35. $this->assertNull($stats[Cache::STATS_MISSES]);
  36. $this->assertGreaterThan(0, $stats[Cache::STATS_UPTIME]);
  37. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  38. $this->assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]);
  39. }
  40. /**
  41. * @group 108
  42. */
  43. public function testMongoCursorExceptionsDoNotBubbleUp()
  44. {
  45. /* @var $collection \MongoCollection|\PHPUnit_Framework_MockObject_MockObject */
  46. $collection = $this->getMock('MongoCollection', array(), array(), '', false);
  47. $collection->expects(self::once())->method('update')->willThrowException(new \MongoCursorException());
  48. $cache = new MongoDBCache($collection);
  49. self::assertFalse($cache->save('foo', 'bar'));
  50. }
  51. protected function _getCacheDriver()
  52. {
  53. return new MongoDBCache($this->collection);
  54. }
  55. }