StashPoolTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Elgg\Cache;
  3. use PHPUnit_Framework_TestCase as TestCase;
  4. use Stash;
  5. class StashPoolTest extends TestCase implements PoolTestCase {
  6. public function testGetDoesNotRegenerateValueFromCallbackOnHit() {
  7. $pool = StashPool::createEphemeral();
  8. $pool->get('foo', function() { return 1; });
  9. $result = $pool->get('foo', function() { return 2; });
  10. $this->assertEquals(1, $result);
  11. }
  12. public function testGetRegeneratesValueFromCallbackOnMiss() {
  13. $pool = StashPool::createEphemeral();
  14. $result = $pool->get('foo', function() { return 1; });
  15. $this->assertEquals(1, $result);
  16. }
  17. public function testInvalidateForcesTheSpecifiedValueToBeRegenerated() {
  18. $pool = StashPool::createEphemeral();
  19. $result = $pool->get('foo', function() { return 1; });
  20. $this->assertEquals(1, $result);
  21. $pool->invalidate('foo');
  22. $result = $pool->get('foo', function() { return 2; });
  23. $this->assertEquals(2, $result);
  24. }
  25. public function testAcceptsStringAndIntKeys() {
  26. $pool = StashPool::createEphemeral();
  27. foreach (array('123', 123) as $key) {
  28. $pool->put($key, 'foo');
  29. $pool->get($key, function () { return 'foo'; });
  30. $pool->invalidate($key);
  31. }
  32. }
  33. /**
  34. * @dataProvider invalidKeyProvider
  35. */
  36. public function testPutComplainsAboutInvalidKeys($key) {
  37. $pool = StashPool::createEphemeral();
  38. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'assert');
  39. $pool->put($key, 'foo');
  40. }
  41. /**
  42. * @dataProvider invalidKeyProvider
  43. */
  44. public function testGetComplainsAboutInvalidKeys($key) {
  45. $pool = StashPool::createEphemeral();
  46. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'assert');
  47. $pool->get($key, function () { return 'foo'; });
  48. }
  49. /**
  50. * @dataProvider invalidKeyProvider
  51. */
  52. public function testInvalidateComplainsAboutInvalidKeys($key) {
  53. $pool = StashPool::createEphemeral();
  54. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'assert');
  55. $pool->invalidate($key);
  56. }
  57. public function invalidKeyProvider() {
  58. return array(
  59. array(123.1),
  60. array(true),
  61. array(array()),
  62. array(new \stdClass()),
  63. );
  64. }
  65. /**
  66. * Stash recommends always calling $item->lock() on miss to make sure that
  67. * the caching is as performant as possible by avoiding multiple
  68. * simultaneous regenerations of the same value.
  69. *
  70. * http://www.stashphp.com/Invalidation.html#stampede-protection
  71. *
  72. * 1. Create a new cache
  73. * 2. Get any entry
  74. * 3. Check that Stash\Item::lock() was called
  75. * 4. Get the same entry
  76. * 5. Check that Stash\Item::lock() was *not* called
  77. */
  78. public function testEnablesStashStampedeProtection() {
  79. $pool = StashPool::createEphemeral();
  80. $this->markTestIncomplete();
  81. }
  82. }