MockerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\test;
  9. use lithium\test\Mocker;
  10. /**
  11. * WARNING:
  12. * No unit test should mock the same test as another to avoid conflicting filters.
  13. */
  14. class MockerTest extends \lithium\test\Unit {
  15. public function setUp() {
  16. Mocker::register();
  17. }
  18. public function testAutoloadRegister() {
  19. Mocker::register();
  20. $registered = spl_autoload_functions();
  21. $this->assertTrue(in_array(array(
  22. 'lithium\test\Mocker',
  23. 'create'
  24. ), $registered));
  25. }
  26. public function testBasicCreation() {
  27. $mockee = 'lithium\console\command\Mock';
  28. Mocker::create($mockee);
  29. $this->assertTrue(class_exists($mockee));
  30. }
  31. public function testBasicCreationExtendsCorrectParent() {
  32. $mocker = 'lithium\console\Request';
  33. $mockeeObj = new \lithium\console\request\Mock();
  34. $this->assertTrue(is_a($mockeeObj, $mocker));
  35. }
  36. public function testCannotMockNonLithiumClasses() {
  37. $mockee = 'stdClass\Mock';
  38. Mocker::create($mockee);
  39. $this->assertTrue(!class_exists($mockee));
  40. }
  41. public function testCannotCreateNonStandardMockClass() {
  42. $mockee = 'lithium\console\request\Mocker';
  43. Mocker::create($mockee);
  44. $this->assertTrue(!class_exists($mockee));
  45. }
  46. public function testFilteringNonStaticClass() {
  47. $dispatcher = new \lithium\console\dispatcher\Mock();
  48. $originalResult = $dispatcher->config(array());
  49. $dispatcher->applyFilter('config', function($self, $params, $chain) {
  50. return array();
  51. });
  52. $filteredResult = $dispatcher->config(array());
  53. $this->assertEqual(0, count($filteredResult));
  54. $this->assertNotEqual($filteredResult, $originalResult);
  55. }
  56. public function testFilteringNonStaticClassCanReturnOriginal() {
  57. $response = new \lithium\console\response\Mock();
  58. $originalResult = $response->styles();
  59. $response->applyFilter('styles', function($self, $params, $chain) {
  60. return $chain->next($self, $params, $chain);
  61. });
  62. $filteredResult = $response->styles();
  63. $this->assertEqual($filteredResult, $originalResult);
  64. }
  65. public function testFilteringStaticClass() {
  66. $mockee = 'lithium\analysis\parser\Mock';
  67. $code = 'echo "foobar";';
  68. $originalResult = $mockee::tokenize($code, array('wrap' => true));
  69. $mockee::applyFilter('tokenize', function($self, $params, $chain) {
  70. return array();
  71. });
  72. $filteredResult = $mockee::tokenize($code, array('wrap' => true));
  73. $this->assertEqual(0, count($filteredResult));
  74. $this->assertNotEqual($filteredResult, $originalResult);
  75. }
  76. public function testFilteringStaticClassCanReturnOriginal() {
  77. $mockee = 'lithium\analysis\inspector\Mock';
  78. $originalResult = $mockee::methods('lithium\analysis\Inspector');
  79. $mockee::applyFilter('tokenize', function($self, $params, $chain) {
  80. return $chain->next($self, $params, $chain);
  81. });
  82. $filteredResult = $mockee::methods('lithium\analysis\Inspector');
  83. $this->assertEqual($filteredResult, $originalResult);
  84. }
  85. public function testOriginalMethodNotCalled() {
  86. $http = new \lithium\tests\mocks\security\auth\adapter\mockHttp\Mock;
  87. $this->assertEqual(0, count($http->headers));
  88. $http->_writeHeader('Content-type: text/html');
  89. $this->assertEqual(1, count($http->headers));
  90. $http->applyFilter('_writeHeader', function($self, $params, $chain) {
  91. return false;
  92. });
  93. $http->_writeHeader('Content-type: application/pdf');
  94. $this->assertEqual(1, count($http->headers));
  95. }
  96. public function testFilteringAFilteredMethod() {
  97. $adapt = 'lithium\core\adaptable\Mock';
  98. $adapt::applyFilter('_initAdapter', function($self, $params, $chain) {
  99. return false;
  100. });
  101. $this->assertIdentical(false, $adapt::_initAdapter('foo', array()));
  102. }
  103. public function testStaticResults() {
  104. $docblock = 'lithium\analysis\docblock\Mock';
  105. $docblock::applyFilter(array('comment', 'tags'), function($self, $params, $chain) {
  106. return false;
  107. });
  108. $docblock::comment('foo', 'foobar');
  109. $docblock::comment('bar');
  110. $docblock::tags('baz');
  111. $this->assertIdentical(2, count($docblock::$results['comment']));
  112. $this->assertIdentical(array('foo', 'foobar'), $docblock::$results['comment'][0]['args']);
  113. $this->assertIdentical(false, $docblock::$results['comment'][0]['result']);
  114. $this->assertIdentical(array('bar'), $docblock::$results['comment'][1]['args']);
  115. $this->assertIdentical(false, $docblock::$results['comment'][1]['result']);
  116. $this->assertIdentical(1, count($docblock::$results['tags']));
  117. $this->assertIdentical(array('baz'), $docblock::$results['tags'][0]['args']);
  118. $this->assertIdentical(false, $docblock::$results['tags'][0]['result']);
  119. }
  120. public function testInstanceResults() {
  121. $debugger = new \lithium\data\schema\Mock;
  122. $debugger->applyFilter(array('names', 'meta'), function($self, $params, $chain) {
  123. return false;
  124. });
  125. $debugger->names('foo', 'foobar');
  126. $debugger->names('bar');
  127. $debugger->meta('baz');
  128. $this->assertIdentical(2, count($debugger->results['names']));
  129. $this->assertIdentical(array('foo', 'foobar'), $debugger->results['names'][0]['args']);
  130. $this->assertIdentical(false, $debugger->results['names'][0]['result']);
  131. $this->assertIdentical(array('bar'), $debugger->results['names'][1]['args']);
  132. $this->assertIdentical(false, $debugger->results['names'][1]['result']);
  133. $this->assertIdentical(1, count($debugger->results['meta']));
  134. $this->assertIdentical(array('baz'), $debugger->results['meta'][0]['args']);
  135. $this->assertIdentical(false, $debugger->results['meta'][0]['result']);
  136. }
  137. public function testSkipByReference() {
  138. $stdObj = new \lithium\tests\mocks\test\mockStdClass\Mock();
  139. $stdObj->foo = 'foo';
  140. $originalData = $stdObj->data();
  141. $stdObj->applyFilter('data', function($self, $params, $chain) {
  142. return array();
  143. });
  144. $nonfilteredData = $stdObj->data();
  145. $this->assertIdentical($originalData, $nonfilteredData);
  146. }
  147. public function testGetByReference() {
  148. $stdObj = new \lithium\tests\mocks\test\mockStdClass\Mock();
  149. $stdObj->foo = 'foo';
  150. $foo =& $stdObj->foo;
  151. $foo = 'bar';
  152. $this->assertIdentical('bar', $stdObj->foo);
  153. }
  154. public function testChainReturnsMockerChain() {
  155. $this->assertTrue(Mocker::chain(new \stdClass) instanceof \lithium\test\MockerChain);
  156. }
  157. }
  158. ?>