CacheTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2009 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 dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Json/Server/Cache.php';
  28. require_once 'Zend/Json/Server.php';
  29. /**
  30. * Test class for Zend_Json_Server_Cache
  31. *
  32. * @category Zend
  33. * @package Zend_Json_Server
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Json
  38. * @group Zend_Json_Server
  39. */
  40. class Zend_Json_Server_CacheTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. require_once "PHPUnit/TextUI/TestRunner.php";
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_CacheTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Sets up the fixture, for example, open a network connection.
  55. * This method is called before a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. $this->server = new Zend_Json_Server();
  62. $this->server->setClass('Zend_Json_Server_CacheTest_Foo', 'foo');
  63. $this->cacheFile = tempnam(sys_get_temp_dir(), 'zjs');
  64. // if (!is_writeable(dirname(__FILE__))) {
  65. if (!is_writeable($this->cacheFile)) {
  66. $this->markTestSkipped('Cannot write test caches due to permissions');
  67. }
  68. if (file_exists($this->cacheFile)) {
  69. unlink($this->cacheFile);
  70. }
  71. }
  72. /**
  73. * Tears down the fixture, for example, close a network connection.
  74. * This method is called after a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. if (file_exists($this->cacheFile)) {
  81. unlink($this->cacheFile);
  82. }
  83. }
  84. public function testRetrievingSmdCacheShouldReturnFalseIfCacheDoesNotExist()
  85. {
  86. $this->assertFalse(Zend_Json_Server_Cache::getSmd($this->cacheFile));
  87. }
  88. public function testSavingSmdCacheShouldReturnTrueOnSuccess()
  89. {
  90. $this->assertTrue(Zend_Json_Server_Cache::saveSmd($this->cacheFile, $this->server));
  91. }
  92. public function testSavedCacheShouldMatchGeneratedCache()
  93. {
  94. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  95. $json = $this->server->getServiceMap()->toJson();
  96. $test = Zend_Json_Server_Cache::getSmd($this->cacheFile);
  97. $this->assertSame($json, $test);
  98. }
  99. public function testDeletingSmdShouldReturnFalseOnFailure()
  100. {
  101. $this->assertFalse(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  102. }
  103. public function testDeletingSmdShouldReturnTrueOnSuccess()
  104. {
  105. $this->testSavingSmdCacheShouldReturnTrueOnSuccess();
  106. $this->assertTrue(Zend_Json_Server_Cache::deleteSmd($this->cacheFile));
  107. }
  108. }
  109. /**
  110. * Class for testing JSON-RPC server caching
  111. */
  112. class Zend_Json_Server_CacheTest_Foo
  113. {
  114. /**
  115. * Bar
  116. *
  117. * @param bool $one
  118. * @param string $two
  119. * @param mixed $three
  120. * @return array
  121. */
  122. public function bar($one, $two = 'two', $three = null)
  123. {
  124. return array($one, $two, $three);
  125. }
  126. /**
  127. * Baz
  128. *
  129. * @return void
  130. */
  131. public function baz()
  132. {
  133. throw new Exception('application error');
  134. }
  135. }
  136. // Call Zend_Json_Server_CacheTest::main() if this source file is executed directly.
  137. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_CacheTest::main") {
  138. Zend_Json_Server_CacheTest::main();
  139. }