AbstractDriverTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Stash\Test\Driver;
  11. use Stash\Utilities;
  12. /**
  13. * @package Stash
  14. * @author Robert Hafner <tedivm@tedivm.com>
  15. */
  16. abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $data = array('string' => 'Hello world!',
  19. 'complexString' => "\t\tHello\r\n\r\'\'World!\"\'\\",
  20. 'int' => 4234,
  21. 'negint' => -6534,
  22. 'bigint' => 58635272821786587286382824657568871098287278276543219876543,
  23. 'float' => 1.8358023545,
  24. 'negfloat' => -5.7003249023,
  25. 'false' => false,
  26. 'true' => true,
  27. 'null' => null,
  28. 'array' => array(3, 5, 7),
  29. 'hashmap' => array('one' => 1, 'two' => 2),
  30. 'multidemensional array' => array(array(5345),
  31. array(3, 'hello', false, array('one' => 1, 'two' => 2
  32. )
  33. )
  34. ),
  35. '@node' => 'stuff',
  36. 'test/of/really/long/key/with/lots/of/children/keys' => true
  37. );
  38. protected $expiration;
  39. protected $driverClass;
  40. protected $startTime;
  41. protected $setup = false;
  42. protected $persistence = false;
  43. public static function tearDownAfterClass()
  44. {
  45. Utilities::deleteRecursive(Utilities::getBaseDirectory());
  46. }
  47. protected function setUp()
  48. {
  49. if (!$this->setup) {
  50. $this->startTime = time();
  51. $this->expiration = $this->startTime + 3600;
  52. if (!$this->getFreshDriver()) {
  53. $this->markTestSkipped('Driver class unsuited for current environment');
  54. }
  55. $this->data['object'] = new \stdClass();
  56. $this->data['large_string'] = str_repeat('apples', ceil(200000 / 6));
  57. }
  58. }
  59. protected function getFreshDriver(array $options = null)
  60. {
  61. $driverClass = $this->driverClass;
  62. if ($options === null) {
  63. $options = $this->getOptions();
  64. }
  65. if (!$driverClass::isAvailable()) {
  66. return false;
  67. }
  68. $driver = new $driverClass($options);
  69. return $driver;
  70. }
  71. public function testSetOptions()
  72. {
  73. $driverType = $this->driverClass;
  74. $options = $this->getOptions();
  75. $driver = new $driverType($options);
  76. $this->assertTrue(is_a($driver, $driverType), 'Driver is an instance of ' . $driverType);
  77. $this->assertTrue(is_a($driver, '\Stash\Interfaces\DriverInterface'), 'Driver implments the Stash\Driver\DriverInterface interface');
  78. return $driver;
  79. }
  80. protected function getOptions()
  81. {
  82. return array();
  83. }
  84. /**
  85. * @depends testSetOptions
  86. */
  87. public function testStoreData($driver)
  88. {
  89. foreach ($this->data as $type => $value) {
  90. $key = array('base', $type);
  91. $this->assertTrue($driver->storeData($key, $value, $this->expiration), 'Driver class able to store data type ' . $type);
  92. }
  93. return $driver;
  94. }
  95. /**
  96. * @depends testStoreData
  97. */
  98. public function testGetData($driver)
  99. {
  100. foreach ($this->data as $type => $value) {
  101. $key = array('base', $type);
  102. $return = $driver->getData($key);
  103. $this->assertTrue(is_array($return), 'getData ' . $type . ' returns array');
  104. $this->assertArrayHasKey('expiration', $return, 'getData ' . $type . ' has expiration');
  105. $this->assertLessThanOrEqual($this->expiration, $return['expiration'], 'getData ' . $type . ' returns same expiration that is equal to or sooner than the one passed.');
  106. if (!is_null($return['expiration'])) {
  107. $this->assertGreaterThan($this->startTime, $return['expiration'], 'getData ' . $type . ' returns expiration that after it\'s storage time');
  108. }
  109. $this->assertArrayHasKey('data', $return, 'getData ' . $type . ' has data');
  110. $this->assertEquals($value, $return['data'], 'getData ' . $type . ' returns same item as stored');
  111. }
  112. return $driver;
  113. }
  114. /**
  115. * @depends testGetData
  116. */
  117. public function testClear($driver)
  118. {
  119. foreach ($this->data as $type => $value) {
  120. $key = array('base', $type);
  121. $keyString = implode('::', $key);
  122. $return = $driver->getData($key);
  123. $this->assertArrayHasKey('data', $return, 'Repopulating ' . $type . ' stores data');
  124. $this->assertEquals($value, $return['data'], 'Repopulating ' . $type . ' returns same item as stored');
  125. $this->assertTrue($driver->clear($key), 'clear of ' . $keyString . ' returned true');
  126. $this->assertFalse($driver->getData($key), 'clear of ' . $keyString . ' removed data');
  127. }
  128. // repopulate
  129. foreach ($this->data as $type => $value) {
  130. $key = array('base', $type);
  131. $keyString = implode('::', $key);
  132. $driver->storeData($key, $value, $this->expiration);
  133. $return = $driver->getData($key);
  134. $this->assertArrayHasKey('data', $return, 'Repopulating ' . $type . ' stores data');
  135. $this->assertEquals($value, $return['data'], 'Repopulating ' . $type . ' returns same item as stored');
  136. }
  137. $this->assertTrue($driver->clear(array('base')), 'clear of base node returned true');
  138. foreach ($this->data as $type => $value) {
  139. $this->assertFalse($driver->getData(array('base', $type)), 'clear of base node removed data');
  140. }
  141. // repopulate
  142. foreach ($this->data as $type => $value) {
  143. $key = array('base', $type);
  144. $keyString = implode('::', $key);
  145. $driver->storeData($key, $value, $this->expiration);
  146. $return = $driver->getData($key);
  147. $this->assertArrayHasKey('data', $return, 'Repopulating ' . $type . ' stores data');
  148. $this->assertEquals($value, $return['data'], 'Repopulating ' . $type . ' returns same item as stored');
  149. }
  150. $this->assertTrue($driver->clear(), 'clear of root node returned true');
  151. foreach ($this->data as $type => $value) {
  152. $this->assertFalse($driver->getData(array('base', $type)), 'clear of root node removed data');
  153. }
  154. return $driver;
  155. }
  156. /**
  157. * @depends testClear
  158. */
  159. public function testPurge($driver)
  160. {
  161. // We're going to populate this with both stale and fresh data, but we're only checking that the stale data
  162. // is removed. This is to give drivers the flexibility to introduce their own removal algorithms- our only
  163. // restriction is that they can't keep things for longer than the developers tell them to, but it's okay to
  164. // remove things early.
  165. foreach ($this->data as $type => $value) {
  166. $driver->storeData(array('base', 'fresh', $type), $value, $this->expiration);
  167. }
  168. foreach ($this->data as $type => $value) {
  169. $driver->storeData(array('base', 'stale', $type), $value, $this->startTime - 600);
  170. }
  171. $this->assertTrue($driver->purge());
  172. foreach ($this->data as $type => $value) {
  173. $this->assertFalse($driver->getData(array('base', 'stale', $type)), 'purge removed stale data');
  174. }
  175. return $driver;
  176. }
  177. /**
  178. * @depends testPurge
  179. */
  180. public function testDestructor($driver)
  181. {
  182. $driver=null;
  183. }
  184. /**
  185. * @depends testDestructor
  186. */
  187. public function testIsPersistant()
  188. {
  189. if (!$driver = $this->getFreshDriver()) {
  190. $this->markTestSkipped('Driver class unsuited for current environment');
  191. }
  192. $this->assertEquals($this->persistence, $driver->isPersistent());
  193. }
  194. }