CompositeTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Driver\FileSystem;
  12. use Stash\Driver\Composite;
  13. use Stash\Driver\Ephemeral;
  14. /**
  15. * @package Stash
  16. * @author Robert Hafner <tedivm@tedivm.com>
  17. */
  18. class CompositeTest extends AbstractDriverTest
  19. {
  20. protected $driverClass = 'Stash\Driver\Composite';
  21. protected $subDrivers;
  22. protected function getOptions()
  23. {
  24. $options = array();
  25. $options['drivers'][] = new Ephemeral();
  26. $options['drivers'][] = new Ephemeral();
  27. $options['drivers'][] = new Ephemeral();
  28. $this->subDrivers = $options['drivers'];
  29. return $options;
  30. }
  31. public function testStaggeredStore()
  32. {
  33. $driver = $this->getFreshDriver();
  34. $a = $this->subDrivers[0];
  35. $b = $this->subDrivers[1];
  36. $c = $this->subDrivers[2];
  37. foreach ($this->data as $type => $value) {
  38. $key = array('base', $type);
  39. $this->assertTrue($driver->storeData($key, $value, $this->expiration), 'Driver class able to store data type ' . $type);
  40. }
  41. foreach ($this->data as $type => $value) {
  42. $key = array('base', $type);
  43. $return = $c->getData($key);
  44. $this->assertTrue(is_array($return), 'getData ' . $type . ' returns array');
  45. $this->assertArrayHasKey('expiration', $return, 'getData ' . $type . ' has expiration');
  46. $this->assertLessThanOrEqual($this->expiration, $return['expiration'], 'getData ' . $type . ' returns same expiration that is equal to or sooner than the one passed.');
  47. $this->assertGreaterThan($this->startTime, $return['expiration'], 'getData ' . $type . ' returns expiration that after it\'s storage time');
  48. $this->assertArrayHasKey('data', $return, 'getData ' . $type . ' has data');
  49. $this->assertEquals($value, $return['data'], 'getData ' . $type . ' returns same item as stored');
  50. }
  51. }
  52. public function testStaggeredGet()
  53. {
  54. $driver = $this->getFreshDriver();
  55. $a = $this->subDrivers[0];
  56. $b = $this->subDrivers[1];
  57. $c = $this->subDrivers[2];
  58. foreach ($this->data as $type => $value) {
  59. $key = array('base', $type);
  60. $this->assertTrue($c->storeData($key, $value, $this->expiration), 'Driver class able to store data type ' . $type);
  61. }
  62. foreach ($this->data as $type => $value) {
  63. $key = array('base', $type);
  64. $return = $driver->getData($key);
  65. $this->assertTrue(is_array($return), 'getData ' . $type . ' returns array');
  66. $this->assertArrayHasKey('expiration', $return, 'getData ' . $type . ' has expiration');
  67. $this->assertLessThanOrEqual($this->expiration, $return['expiration'], 'getData ' . $type . ' returns same expiration that is equal to or sooner than the one passed.');
  68. $this->assertGreaterThan($this->startTime, $return['expiration'], 'getData ' . $type . ' returns expiration that after it\'s storage time');
  69. $this->assertArrayHasKey('data', $return, 'getData ' . $type . ' has data');
  70. $this->assertEquals($value, $return['data'], 'getData ' . $type . ' returns same item as stored');
  71. }
  72. }
  73. public function testIsPersistent()
  74. {
  75. $fileDriver = new FileSystem();
  76. $ephemeralDriver = new Ephemeral();
  77. $drivers = array($fileDriver, $ephemeralDriver);
  78. $driver = new Composite(array('drivers' => $drivers));
  79. $this->assertTrue($driver->isPersistent());
  80. $drivers = array($ephemeralDriver, $fileDriver);
  81. $driver = new Composite(array('drivers' => $drivers));
  82. $this->assertTrue($driver->isPersistent());
  83. $drivers = array($fileDriver, $fileDriver);
  84. $driver = new Composite(array('drivers' => $drivers));
  85. $this->assertTrue($driver->isPersistent());
  86. $drivers = array($ephemeralDriver, $ephemeralDriver);
  87. $driver = new Composite(array('drivers' => $drivers));
  88. $this->assertFalse($driver->isPersistent());
  89. }
  90. /**
  91. * @expectedException Stash\Exception\RuntimeException
  92. */
  93. public function testWithoutDriversException()
  94. {
  95. $driver = new Composite(array('drivers' => null));
  96. }
  97. /**
  98. * @expectedException Stash\Exception\RuntimeException
  99. */
  100. public function testWithFakeDriversException()
  101. {
  102. $driver = new Composite(array('drivers' => array('fakedriver')));
  103. }
  104. /**
  105. * @expectedException Stash\Exception\InvalidArgumentException
  106. */
  107. public function testWithBadDriverArgumentException()
  108. {
  109. $driver = new Composite(array('drivers' => 'fakedriver'));
  110. }
  111. }