MemcacheTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Test\Stubs\PoolGetDriverStub;
  12. use Stash\Driver\Memcache;
  13. use Stash\Item;
  14. /**
  15. * @package Stash
  16. * @author Robert Hafner <tedivm@tedivm.com>
  17. */
  18. class MemcacheTest extends AbstractDriverTest
  19. {
  20. protected $driverClass = 'Stash\Driver\Memcache';
  21. protected $extension = 'memcache';
  22. protected $servers = array('127.0.0.1', '11211');
  23. protected $persistence = true;
  24. protected function getOptions()
  25. {
  26. return array('extension' => $this->extension, 'servers' => $this->servers);
  27. }
  28. protected function setUp()
  29. {
  30. if (!$this->setup) {
  31. $this->startTime = time();
  32. $this->expiration = $this->startTime + 3600;
  33. $driverClass = $this->driverClass;
  34. if (!$driverClass::isAvailable()) {
  35. $this->markTestSkipped('Driver class unsuited for current environment');
  36. return;
  37. }
  38. if (!class_exists(ucfirst($this->extension))) {
  39. $this->markTestSkipped('Test requires ' . $this->extension . ' extension');
  40. return;
  41. }
  42. if (!($sock = @fsockopen($this->servers[0], $this->servers[1], $errno, $errstr, 1))) {
  43. $this->markTestSkipped('Memcache tests require memcache server');
  44. return;
  45. }
  46. fclose($sock);
  47. $this->data['object'] = new \stdClass();
  48. }
  49. }
  50. public function testConstructionOptions()
  51. {
  52. $key = array('apple', 'sauce');
  53. $options = array();
  54. $options['servers'][] = array('127.0.0.1', '11211', '50');
  55. $options['servers'][] = array('127.0.0.1', '11211');
  56. $options['extension'] = $this->extension;
  57. $driver = new Memcache($options);
  58. $item = new Item();
  59. $poolStub = new PoolGetDriverStub();
  60. $poolStub->setDriver($driver);
  61. $item->setPool($poolStub);
  62. $item->setKey($key);
  63. $this->assertTrue($item->set($key)->save(), 'Able to load and store memcache driver using multiple servers');
  64. $options = array();
  65. $options['extension'] = $this->extension;
  66. $driver = new Memcache($options);
  67. $item = new Item();
  68. $poolStub = new PoolGetDriverStub();
  69. $poolStub->setDriver($driver);
  70. $item->setPool($poolStub);
  71. $item->setKey($key);
  72. $this->assertTrue($item->set($key)->save(), 'Able to load and store memcache driver using default server');
  73. }
  74. }