CacheTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json_Server
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Json_Server_CacheTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_CacheTest::main");
  25. }
  26. require_once 'Zend/Json/Server/Cache.php';
  27. require_once 'Zend/Json/Server.php';
  28. /**
  29. * Test class for Zend_Json_Server_Cache
  30. *
  31. * @category Zend
  32. * @package Zend_Json_Server
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Json
  37. * @group Zend_Json_Server
  38. */
  39. class Zend_Json_Server_CacheTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_CacheTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->server = new Zend_Json_Server();
  60. $this->server->setClass('Zend_Json_Server_CacheTest_Foo', 'foo');
  61. $this->cacheFile = tempnam(sys_get_temp_dir(), 'zjs');
  62. // if (!is_writeable(dirname(__FILE__))) {
  63. if (!is_writeable($this->cacheFile)) {
  64. $this->markTestSkipped('Cannot write test caches due to permissions');
  65. }
  66. if (file_exists($this->cacheFile)) {
  67. unlink($this->cacheFile);
  68. }
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. if (file_exists($this->cacheFile)) {
  79. unlink($this->cacheFile);
  80. }
  81. }
  82. public function testRetrievingSmdCacheShouldReturnFalseIfCacheDoesNotExist()
  83. {
  84. $this->assertFalse(Zend_Json_Server_Cache::getSmd($this->cacheFile));
  85. }
  86. public function testSavingSmdCacheShouldReturnTrueOnSuccess()
  87. {
  88. $this->assertTrue(Zend_Json_Server_Cache::saveSmd($this->cacheFile, $this->server));
  89. }
  90. public function testSavedCacheShouldMatchGeneratedCache()
  91. {
  92. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  93. $json = $this->server->getServiceMap()->toJson();
  94. $test = Zend_Json_Server_Cache::getSmd($this->cacheFile);
  95. $this->assertSame($json, $test);
  96. }
  97. public function testDeletingSmdShouldReturnFalseOnFailure()
  98. {
  99. $this->assertFalse(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  100. }
  101. public function testDeletingSmdShouldReturnTrueOnSuccess()
  102. {
  103. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  104. $this->assertTrue(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  105. }
  106. }
  107. /**
  108. * Class for testing JSON-RPC server caching
  109. */
  110. class Zend_Json_Server_CacheTest_Foo
  111. {
  112. /**
  113. * Bar
  114. *
  115. * @param bool $one
  116. * @param string $two
  117. * @param mixed $three
  118. * @return array
  119. */
  120. public function bar($one, $two = 'two', $three = null)
  121. {
  122. return array($one, $two, $three);
  123. }
  124. /**
  125. * Baz
  126. *
  127. * @return void
  128. */
  129. public function baz()
  130. {
  131. throw new Exception('application error');
  132. }
  133. }
  134. // Call Zend_Json_Server_CacheTest::main() if this source file is executed directly.
  135. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_CacheTest::main") {
  136. Zend_Json_Server_CacheTest::main();
  137. }