CacheTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // Call Zend_Json_Server_CacheTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_CacheTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Json/Server/Cache.php';
  8. require_once 'Zend/Json/Server.php';
  9. /**
  10. * Test class for Zend_Json_Server_Cache
  11. */
  12. class Zend_Json_Server_CacheTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. require_once "PHPUnit/TextUI/TestRunner.php";
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_CacheTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->server = new Zend_Json_Server();
  34. $this->server->setClass('Zend_Json_Server_CacheTest_Foo', 'foo');
  35. $this->cacheFile = tempnam(sys_get_temp_dir(), 'zjs');
  36. // if (!is_writeable(dirname(__FILE__))) {
  37. if (!is_writeable($this->cacheFile)) {
  38. $this->markTestSkipped('Cannot write test caches due to permissions');
  39. }
  40. if (file_exists($this->cacheFile)) {
  41. unlink($this->cacheFile);
  42. }
  43. }
  44. /**
  45. * Tears down the fixture, for example, close a network connection.
  46. * This method is called after a test is executed.
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. if (file_exists($this->cacheFile)) {
  53. unlink($this->cacheFile);
  54. }
  55. }
  56. public function testRetrievingSmdCacheShouldReturnFalseIfCacheDoesNotExist()
  57. {
  58. $this->assertFalse(Zend_Json_Server_Cache::getSmd($this->cacheFile));
  59. }
  60. public function testSavingSmdCacheShouldReturnTrueOnSuccess()
  61. {
  62. $this->assertTrue(Zend_Json_Server_Cache::saveSmd($this->cacheFile, $this->server));
  63. }
  64. public function testSavedCacheShouldMatchGeneratedCache()
  65. {
  66. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  67. $json = $this->server->getServiceMap()->toJson();
  68. $test = Zend_Json_Server_Cache::getSmd($this->cacheFile);
  69. $this->assertSame($json, $test);
  70. }
  71. public function testDeletingSmdShouldReturnFalseOnFailure()
  72. {
  73. $this->assertFalse(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  74. }
  75. public function testDeletingSmdShouldReturnTrueOnSuccess()
  76. {
  77. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  78. $this->assertTrue(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  79. }
  80. }
  81. /**
  82. * Class for testing JSON-RPC server caching
  83. */
  84. class Zend_Json_Server_CacheTest_Foo
  85. {
  86. /**
  87. * Bar
  88. *
  89. * @param bool $one
  90. * @param string $two
  91. * @param mixed $three
  92. * @return array
  93. */
  94. public function bar($one, $two = 'two', $three = null)
  95. {
  96. return array($one, $two, $three);
  97. }
  98. /**
  99. * Baz
  100. *
  101. * @return void
  102. */
  103. public function baz()
  104. {
  105. throw new Exception('application error');
  106. }
  107. }
  108. // Call Zend_Json_Server_CacheTest::main() if this source file is executed directly.
  109. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_CacheTest::main") {
  110. Zend_Json_Server_CacheTest::main();
  111. }