Mongodb.test.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
  4. * @author Georges.L (Geolim4) <contact@geolim4.com>
  5. */
  6. use phpFastCache\CacheManager;
  7. use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
  8. use phpFastCache\Exceptions\phpFastCacheDriverException;
  9. use phpFastCache\Helper\TestHelper;
  10. chdir(__DIR__);
  11. require_once __DIR__ . '/../vendor/autoload.php';
  12. $testHelper = new TestHelper('Mongodb driver');
  13. try{
  14. $cacheInstance = CacheManager::getInstance('Mongodb', [
  15. 'databaseName' => 'pfc_test',
  16. 'username' => 'travis',
  17. 'password' => 'test',
  18. ]);
  19. $cacheKey = str_shuffle(uniqid('pfc', true));
  20. $cacheValue = str_shuffle(uniqid('pfc', true));
  21. try{
  22. $item = $cacheInstance->getItem($cacheKey);
  23. $item->set($cacheValue)->expiresAfter(300);
  24. $cacheInstance->save($item);
  25. $testHelper->printPassText('Successfully saved a new cache item into Mongodb server');
  26. }catch(phpFastCacheDriverException $e){
  27. $testHelper->printFailText('Failed to save a new cache item into Mongodb server with exception: ' . $e->getMessage());
  28. }
  29. try{
  30. unset($item);
  31. $cacheInstance->detachAllItems();
  32. $item = $cacheInstance->getItem($cacheKey);
  33. if($item->get() === $cacheValue){
  34. $testHelper->printPassText('Getter returned expected value: ' . $cacheValue);
  35. }else{
  36. $testHelper->printFailText('Getter returned unexpected value, expecting "' . $cacheValue . '", got "' . $item->get() . '"');
  37. }
  38. }catch(phpFastCacheDriverException $e){
  39. $testHelper->printFailText('Failed to save a new cache item into Mongodb server with exception: ' . $e->getMessage());
  40. }
  41. try{
  42. unset($item);
  43. $cacheInstance->detachAllItems();
  44. $cacheInstance->clear();
  45. $item = $cacheInstance->getItem($cacheKey);
  46. if(!$item->isHit()){
  47. $testHelper->printPassText('Successfully cleared the Mongodb server, no cache item found');
  48. }else{
  49. $testHelper->printFailText('Failed to clear the Mongodb server, a cache item has been found');
  50. }
  51. }catch(phpFastCacheDriverException $e){
  52. $testHelper->printFailText('Failed to clear the Mongodb server with exception: ' . $e->getMessage());
  53. }
  54. try{
  55. $item = $cacheInstance->getItem($cacheKey);
  56. $item->set($cacheValue)->expiresAfter(300);
  57. $cacheInstance->save($item);
  58. if($cacheInstance->deleteItem($item->getKey())){
  59. $testHelper->printPassText('Deleter successfully removed the item from cache');
  60. }else{
  61. $testHelper->printFailText('Deleter failed to remove the item from cache');
  62. }
  63. }catch(phpFastCacheDriverException $e){
  64. $testHelper->printFailText('Failed to remove a cache item from Mongodb server with exception: ' . $e->getMessage());
  65. }
  66. }catch (phpFastCacheDriverCheckException $e){
  67. // Must be HHVM, right ?
  68. $testHelper->printSkipText('Ignored test since Mongodb driver since to be unavailable at the moment: ' . $e->getMessage());
  69. }
  70. $testHelper->terminateTest();