PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/share/test/application/output/output-test.php

https://github.com/php-tox/tox
PHP | 360 lines | 234 code | 28 blank | 98 comment | 0 complexity | bf096c647f88ed35dd7e04342aa6066a MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Application\Output.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Application\Output;
  24. use PHPUnit_Framework_TestCase;
  25. require_once __DIR__ . '/../../../../src/core/assembly.php';
  26. require_once __DIR__ . '/../../../../src/application/ioutput.php';
  27. require_once __DIR__ . '/../../../../src/application/output/output.php';
  28. require_once __DIR__ . '/../../../../src/core/exception.php';
  29. require_once __DIR__ . '/../../../../src/application/output/closedoutputexception.php';
  30. require_once __DIR__ . '/../../../../src/application/output/bufferreadonlyexception.php';
  31. require_once __DIR__ . '/../../../../src/application/output/streamingviewexpectedexception.php';
  32. require_once __DIR__ . '/../../../../src/application/iview.php';
  33. require_once __DIR__ . '/../../../../src/application/istreamingview.php';
  34. require_once __DIR__ . '/../../../../src/application/ifallback.php';
  35. require_once __DIR__ . '/../../../../src/application/view/view.php';
  36. require_once __DIR__ . '/../../../../src/application/view/streamingview.php';
  37. require_once __DIR__ . '/../../../../src/application/ioutputtask.php';
  38. use Exception as PHPException;
  39. use Tox\Application;
  40. /**
  41. * Tests Tox\Application\Output.
  42. *
  43. * @internal
  44. *
  45. * @package tox.application.output
  46. * @author Snakevil Zen <zsnakevil@gmail.com>
  47. */
  48. class OutputTest extends PHPUnit_Framework_TestCase
  49. {
  50. public function testMagicMethods()
  51. {
  52. $o_out = $this->getMockBuilder('Tox\\Application\\Output\\Output')
  53. ->setMethods(array('setBuffer', 'getBuffer', 'setView', 'getView'))
  54. ->getMockForAbstractClass();
  55. $o_view = $this->getMock('Tox\\Application\\IView');
  56. $s_buff = microtime();
  57. $o_out->expects($this->once())->method('getBuffer');
  58. $o_out->buffer;
  59. $o_out->expects($this->once())->method('setBuffer')->with($this->equalTo($s_buff));
  60. $o_out->buffer = $s_buff;
  61. $o_out->expects($this->once())->method('getView');
  62. $o_out->view;
  63. $o_out->expects($this->once())->method('setView')->with($this->equalTo($o_view));
  64. $o_out->view = $o_view;
  65. }
  66. public function testOutputtingOnceWithAnyTimesWritings()
  67. {
  68. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  69. ob_start();
  70. $o1 = $o_out->write('foo');
  71. $o2 = $o_out->close();
  72. ob_end_clean();
  73. $this->assertSame($o_out, $o1);
  74. $this->assertSame($o_out, $o2);
  75. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  76. ob_start();
  77. $o1 = $o_out->writeClose('bar');
  78. ob_end_clean();
  79. $this->assertSame($o_out, $o1);
  80. $a_lobs = range(1, 9);
  81. shuffle($a_lobs);
  82. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  83. ob_start();
  84. foreach ($a_lobs as $ii) {
  85. $o_out->write($ii);
  86. }
  87. $s_out = ob_get_clean();
  88. $this->assertEquals('', $s_out);
  89. ob_start();
  90. $o_out->close();
  91. $s_out = ob_get_clean();
  92. $this->assertEquals(implode('', $a_lobs), rtrim($s_out));
  93. }
  94. /**
  95. * @depends testOutputtingOnceWithAnyTimesWritings
  96. * @expectedException Tox\Application\Output\ClosedOutputException
  97. */
  98. public function testOutputFrozenAfterClose()
  99. {
  100. ob_start();
  101. $this->getMockForAbstractClass('Tox\\Application\\Output\\Output')->close()->write('foo');
  102. ob_end_clean();
  103. }
  104. /**
  105. * @extends testOutputFrozenAfterClose
  106. */
  107. public function testFallbackOnClose()
  108. {
  109. $s_lob = microtime();
  110. $o_view = $this->getMock('Tox\\Application\\IFallback');
  111. $o_view->expects($this->once())->method('render')
  112. ->will($this->returnValue($s_lob));
  113. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  114. ob_start();
  115. $o_out->close();
  116. ob_end_clean();
  117. ob_start();
  118. $o_out->setView($o_view)->close();
  119. $this->assertEquals($s_lob, ob_get_clean());
  120. }
  121. public function testOutputtingBufferReadableForTasks()
  122. {
  123. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  124. $s_lob = $s_buff = microtime();
  125. $this->assertEquals($s_lob, $o_out->write($s_lob)->getBuffer());
  126. $s_lob = microtime(true);
  127. $this->assertEquals($s_buff . $s_lob, $o_out->write($s_lob)->getBuffer());
  128. }
  129. /**
  130. * @expectedException Tox\Application\Output\BufferReadonlyException
  131. */
  132. public function testBufferAccessableOnOutputtingOnly()
  133. {
  134. $this->getMockForAbstractClass('Tox\\Application\\Output\\Output')->setBuffer('foo');
  135. }
  136. /**
  137. * @depends testOutputtingOnceWithAnyTimesWritings
  138. */
  139. public function testPreAndPostOutputting()
  140. {
  141. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  142. $o_task1 = new TaskMock($o_out);
  143. $o_task1->name = microtime();
  144. $o_task2 = new TaskMock($o_out);
  145. $o_task2->name = microtime();
  146. $this->assertSame($o_out, $o_out->addTask($o_task1));
  147. TaskMock::$log = array();
  148. ob_start();
  149. $o_out->addTask($o_task2)->close();
  150. ob_end_clean();
  151. $this->assertEquals(
  152. array(
  153. $o_task1->name . '::preOutput()',
  154. $o_task2->name . '::preOutput()',
  155. $o_task2->name . '::postOutput()',
  156. $o_task1->name . '::postOutput()'
  157. ),
  158. TaskMock::$log
  159. );
  160. }
  161. public function testWritingAgainstAnyOtherViewExceptStreamingView()
  162. {
  163. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  164. $o_out->setView($this->getMock('Tox\\Application\\IView'));
  165. try {
  166. $o_out->write('foo');
  167. } catch (StreamingViewExpectedException $ex) {
  168. } catch (PHPException $ex) {
  169. $this->fail();
  170. }
  171. $o_out->setView($this->getMock('Tox\\Application\\IStreamingView'));
  172. try {
  173. $o_out->write('foo');
  174. } catch (PHPException $ex) {
  175. $this->fail();
  176. }
  177. $this->assertTrue(true);
  178. }
  179. /**
  180. * @depends testWritingAgainstAnyOtherViewExceptStreamingView
  181. */
  182. public function testOutputtingWithAnyOtherView()
  183. {
  184. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  185. $o_view = $this->getMock('Tox\\Application\\IView');
  186. $o_view->expects($this->once())->method('render');
  187. $o_out->setView($o_view);
  188. ob_start();
  189. $o_out->close();
  190. ob_end_clean();
  191. }
  192. public function testStreamingDisabledAtFirst()
  193. {
  194. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  195. $this->assertFalse($o_out->isStreaming());
  196. }
  197. /**
  198. * @depends testStreamingDisabledAtFirst
  199. */
  200. public function testEnablingAndDisablingStreaming()
  201. {
  202. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  203. $this->assertSame($o_out, $o_out->enableStreaming());
  204. $this->assertTrue($o_out->isStreaming());
  205. $this->assertSame($o_out, $o_out->disableStreaming());
  206. $this->assertFalse($o_out->isStreaming());
  207. }
  208. /**
  209. * @depends testEnablingAndDisablingStreaming
  210. */
  211. public function testStreamingModeAgainstAnyOtherViewExceptStreamingView()
  212. {
  213. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  214. $o_out->setView($this->getMock('Tox\\Application\\IView'));
  215. try {
  216. $o_out->enableStreaming();
  217. } catch (StreamingViewExpectedException $e) {
  218. } catch (PHPException $ex) {
  219. $this->fail();
  220. }
  221. try {
  222. $o_out->disableStreaming();
  223. } catch (StreamingViewExpectedException $e) {
  224. } catch (PHPException $ex) {
  225. $this->fail();
  226. }
  227. $this->assertTrue(true);
  228. }
  229. /**
  230. * @depends testEnablingAndDisablingStreaming
  231. */
  232. public function testOutputtingImmediatelyOnStreaming()
  233. {
  234. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output')->enableStreaming();
  235. $a_lobs = range(1, 9);
  236. shuffle($a_lobs);
  237. foreach ($a_lobs as $ii) {
  238. ob_start();
  239. $o_out->write($ii);
  240. $this->assertEquals(ob_get_clean(), $ii);
  241. }
  242. }
  243. /**
  244. * @depends testOutputtingImmediatelyOnStreaming
  245. */
  246. public function testPreAndPostOutputtingOnStreaming()
  247. {
  248. $o_out = $this->getMockForAbstractClass('Tox\\Application\\Output\\Output');
  249. $o_task1 = new TaskMock($o_out);
  250. $o_task1->name = md5('foo' . microtime());
  251. $o_task2 = new TaskMock($o_out);
  252. $o_task2->name = md5('bar' . microtime());
  253. $o_out->addTask($o_task1)->addTask($o_task2)->enableStreaming();
  254. TaskMock::$log = array();
  255. ob_start();
  256. $o_out->write('foo')->write('bar');
  257. ob_end_clean();
  258. $this->assertEquals(
  259. array(
  260. $o_task1->name . '::preOutput()',
  261. $o_task2->name . '::preOutput()',
  262. $o_task2->name . '::postOutput()',
  263. $o_task1->name . '::postOutput()',
  264. $o_task1->name . '::preOutput()',
  265. $o_task2->name . '::preOutput()',
  266. $o_task2->name . '::postOutput()',
  267. $o_task1->name . '::postOutput()'
  268. ),
  269. TaskMock::$log
  270. );
  271. }
  272. }
  273. /**
  274. * Represents as a task for mocking test.
  275. *
  276. * @internal
  277. *
  278. * @package tox.application.output
  279. * @author Snakevil Zen <zsnakevil@gmail.com>
  280. *
  281. * @property mixed $ok
  282. */
  283. class TaskMock implements Application\IOutputTask
  284. {
  285. /**
  286. * Retrieves or sets the tracing logs.
  287. *
  288. * @var string[]
  289. */
  290. public static $log = array();
  291. /**
  292. * Retrieves and sets the name of the instance.
  293. *
  294. * @var string
  295. */
  296. public $name;
  297. /**
  298. * Stores the related output.
  299. *
  300. * @var Application\IOutput
  301. */
  302. protected $output;
  303. /**
  304. * {@inheritdoc}
  305. *
  306. * @param Application\IOutput $output The output which to be used for.
  307. */
  308. public function __construct(Application\IOutput $output)
  309. {
  310. $this->output = $output;
  311. }
  312. /**
  313. * {@inheritdoc}
  314. *
  315. * @return void
  316. */
  317. public function preOutput()
  318. {
  319. self::$log[] = $this->name . '::preOutput()';
  320. $this->output->setBuffer(microtime());
  321. }
  322. /**
  323. * {@inheritdoc}
  324. *
  325. * @return void
  326. */
  327. public function postOutput()
  328. {
  329. self::$log[] = $this->name . '::postOutput()';
  330. }
  331. }
  332. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120