ConsoleIoTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\Console\Exception\StopException;
  19. use Cake\Filesystem\Filesystem;
  20. use Cake\Log\Log;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * ConsoleIo test.
  24. */
  25. class ConsoleIoTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Console\ConsoleIo
  29. */
  30. protected $io;
  31. /**
  32. * @var \Cake\Console\ConsoleOutput|\PHPUnit\Framework\MockObject\MockObject
  33. */
  34. protected $out;
  35. /**
  36. * @var \Cake\Console\ConsoleOutput|\PHPUnit\Framework\MockObject\MockObject
  37. */
  38. protected $err;
  39. /**
  40. * @var \Cake\Console\ConsoleInput|\PHPUnit\Framework\MockObject\MockObject
  41. */
  42. protected $in;
  43. /**
  44. * setUp method
  45. *
  46. * @return void
  47. */
  48. public function setUp(): void
  49. {
  50. parent::setUp();
  51. static::setAppNamespace();
  52. $this->out = $this->getMockBuilder('Cake\Console\ConsoleOutput')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->err = $this->getMockBuilder('Cake\Console\ConsoleOutput')
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->in = $this->getMockBuilder('Cake\Console\ConsoleInput')
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->io = new ConsoleIo($this->out, $this->err, $this->in);
  62. }
  63. /**
  64. * teardown method
  65. *
  66. * @return void
  67. */
  68. public function tearDown(): void
  69. {
  70. parent::tearDown();
  71. if (is_dir(TMP . 'shell_test')) {
  72. $fs = new Filesystem();
  73. $fs->deleteDir(TMP . 'shell_test');
  74. }
  75. }
  76. /**
  77. * Provider for testing choice types.
  78. *
  79. * @return array
  80. */
  81. public function choiceProvider()
  82. {
  83. return [
  84. [['y', 'n']],
  85. ['y,n'],
  86. ['y/n'],
  87. ['y'],
  88. ];
  89. }
  90. /**
  91. * test ask choices method
  92. *
  93. * @dataProvider choiceProvider
  94. * @return void
  95. */
  96. public function testAskChoices($choices)
  97. {
  98. $this->in->expects($this->at(0))
  99. ->method('read')
  100. ->will($this->returnValue('y'));
  101. $result = $this->io->askChoice('Just a test?', $choices);
  102. $this->assertSame('y', $result);
  103. }
  104. /**
  105. * test ask choices method
  106. *
  107. * @dataProvider choiceProvider
  108. * @return void
  109. */
  110. public function testAskChoicesInsensitive($choices)
  111. {
  112. $this->in->expects($this->at(0))
  113. ->method('read')
  114. ->will($this->returnValue('Y'));
  115. $result = $this->io->askChoice('Just a test?', $choices);
  116. $this->assertSame('Y', $result);
  117. }
  118. /**
  119. * Test ask method
  120. *
  121. * @return void
  122. */
  123. public function testAsk()
  124. {
  125. $this->out->expects($this->at(0))
  126. ->method('write')
  127. ->with("<question>Just a test?</question>\n> ");
  128. $this->in->expects($this->at(0))
  129. ->method('read')
  130. ->will($this->returnValue('y'));
  131. $result = $this->io->ask('Just a test?');
  132. $this->assertSame('y', $result);
  133. }
  134. /**
  135. * Test ask method
  136. *
  137. * @return void
  138. */
  139. public function testAskDefaultValue()
  140. {
  141. $this->out->expects($this->at(0))
  142. ->method('write')
  143. ->with("<question>Just a test?</question>\n[n] > ");
  144. $this->in->expects($this->at(0))
  145. ->method('read')
  146. ->will($this->returnValue(''));
  147. $result = $this->io->ask('Just a test?', 'n');
  148. $this->assertSame('n', $result);
  149. }
  150. /**
  151. * testOut method
  152. *
  153. * @return void
  154. */
  155. public function testOut()
  156. {
  157. $this->out->expects($this->at(0))
  158. ->method('write')
  159. ->with('Just a test', 1);
  160. $this->out->expects($this->at(1))
  161. ->method('write')
  162. ->with(['Just', 'a', 'test'], 1);
  163. $this->out->expects($this->at(2))
  164. ->method('write')
  165. ->with(['Just', 'a', 'test'], 2);
  166. $this->out->expects($this->at(3))
  167. ->method('write')
  168. ->with('', 1);
  169. $this->io->out('Just a test');
  170. $this->io->out(['Just', 'a', 'test']);
  171. $this->io->out(['Just', 'a', 'test'], 2);
  172. $this->io->out();
  173. }
  174. /**
  175. * test that verbose and quiet output levels work
  176. *
  177. * @return void
  178. */
  179. public function testVerboseOut()
  180. {
  181. $this->out->expects($this->at(0))
  182. ->method('write')
  183. ->with('Verbose', 1);
  184. $this->out->expects($this->at(1))
  185. ->method('write')
  186. ->with('Normal', 1);
  187. $this->out->expects($this->at(2))
  188. ->method('write')
  189. ->with('Quiet', 1);
  190. $this->io->level(ConsoleIo::VERBOSE);
  191. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  192. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  193. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  194. }
  195. /**
  196. * test that verbose and quiet output levels work
  197. *
  198. * @return void
  199. */
  200. public function testVerboseOutput()
  201. {
  202. $this->out->expects($this->at(0))
  203. ->method('write')
  204. ->with('Verbose', 1);
  205. $this->out->expects($this->at(1))
  206. ->method('write')
  207. ->with('Normal', 1);
  208. $this->out->expects($this->at(2))
  209. ->method('write')
  210. ->with('Quiet', 1);
  211. $this->io->level(ConsoleIo::VERBOSE);
  212. $this->io->verbose('Verbose');
  213. $this->io->out('Normal');
  214. $this->io->quiet('Quiet');
  215. }
  216. /**
  217. * test that verbose and quiet output levels work
  218. *
  219. * @return void
  220. */
  221. public function testQuietOutput()
  222. {
  223. $this->out->expects($this->exactly(2))
  224. ->method('write')
  225. ->with('Quiet', 1);
  226. $this->io->level(ConsoleIo::QUIET);
  227. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  228. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  229. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  230. $this->io->verbose('Verbose');
  231. $this->io->quiet('Quiet');
  232. }
  233. /**
  234. * testErr method
  235. *
  236. * @return void
  237. */
  238. public function testErr()
  239. {
  240. $this->err->expects($this->at(0))
  241. ->method('write')
  242. ->with('Just a test', 1);
  243. $this->err->expects($this->at(1))
  244. ->method('write')
  245. ->with(['Just', 'a', 'test'], 1);
  246. $this->err->expects($this->at(2))
  247. ->method('write')
  248. ->with(['Just', 'a', 'test'], 2);
  249. $this->err->expects($this->at(3))
  250. ->method('write')
  251. ->with('', 1);
  252. $this->io->err('Just a test');
  253. $this->io->err(['Just', 'a', 'test']);
  254. $this->io->err(['Just', 'a', 'test'], 2);
  255. $this->io->err();
  256. }
  257. /**
  258. * Tests abort() wrapper.
  259. *
  260. * @return void
  261. */
  262. public function testAbort()
  263. {
  264. $this->expectException(StopException::class);
  265. $this->expectExceptionMessage('Some error');
  266. $this->expectExceptionCode(1);
  267. $this->err->expects($this->at(0))
  268. ->method('write')
  269. ->with('<error>Some error</error>', 1);
  270. $this->expectException(StopException::class);
  271. $this->expectExceptionCode(1);
  272. $this->expectExceptionMessage('Some error');
  273. $this->io->abort('Some error');
  274. }
  275. /**
  276. * Tests abort() wrapper.
  277. *
  278. * @return void
  279. */
  280. public function testAbortCustomCode()
  281. {
  282. $this->expectException(StopException::class);
  283. $this->expectExceptionMessage('Some error');
  284. $this->expectExceptionCode(99);
  285. $this->err->expects($this->at(0))
  286. ->method('write')
  287. ->with('<error>Some error</error>', 1);
  288. $this->expectException(StopException::class);
  289. $this->expectExceptionCode(99);
  290. $this->expectExceptionMessage('Some error');
  291. $this->io->abort('Some error', 99);
  292. }
  293. /**
  294. * testNl
  295. *
  296. * @return void
  297. */
  298. public function testNl()
  299. {
  300. $newLine = "\n";
  301. if (DS === '\\') {
  302. $newLine = "\r\n";
  303. }
  304. $this->assertEquals($this->io->nl(), $newLine);
  305. $this->assertEquals($this->io->nl(2), $newLine . $newLine);
  306. $this->assertEquals($this->io->nl(1), $newLine);
  307. }
  308. /**
  309. * testHr
  310. *
  311. * @return void
  312. */
  313. public function testHr()
  314. {
  315. $bar = str_repeat('-', 79);
  316. $this->out->expects($this->at(0))->method('write')->with('', 0);
  317. $this->out->expects($this->at(1))->method('write')->with($bar, 1);
  318. $this->out->expects($this->at(2))->method('write')->with('', 0);
  319. $this->out->expects($this->at(3))->method('write')->with('', true);
  320. $this->out->expects($this->at(4))->method('write')->with($bar, 1);
  321. $this->out->expects($this->at(5))->method('write')->with('', true);
  322. $this->io->hr();
  323. $this->io->hr(2);
  324. }
  325. /**
  326. * Test overwriting.
  327. *
  328. * @return void
  329. */
  330. public function testOverwrite()
  331. {
  332. $number = strlen('Some text I want to overwrite');
  333. $this->out->expects($this->at(0))
  334. ->method('write')
  335. ->with('Some <info>text</info> I want to overwrite', 0)
  336. ->will($this->returnValue($number));
  337. $this->out->expects($this->at(1))
  338. ->method('write')
  339. ->with(str_repeat("\x08", $number), 0);
  340. $this->out->expects($this->at(2))
  341. ->method('write')
  342. ->with('Less text', 0)
  343. ->will($this->returnValue(9));
  344. $this->out->expects($this->at(3))
  345. ->method('write')
  346. ->with(str_repeat(' ', $number - 9), 0);
  347. $this->io->out('Some <info>text</info> I want to overwrite', 0);
  348. $this->io->overwrite('Less text');
  349. }
  350. /**
  351. * Test overwriting content with shorter content
  352. *
  353. * @return void
  354. */
  355. public function testOverwriteWithShorterContent()
  356. {
  357. $length = strlen('12345');
  358. $this->out->expects($this->at(0))
  359. ->method('write')
  360. ->with('12345')
  361. ->will($this->returnValue($length));
  362. // Backspaces
  363. $this->out->expects($this->at(1))
  364. ->method('write')
  365. ->with(str_repeat("\x08", $length), 0)
  366. ->will($this->returnValue($length));
  367. $this->out->expects($this->at(2))
  368. ->method('write')
  369. ->with('123', 0)
  370. ->will($this->returnValue(3));
  371. // 2 spaces output to pad up to 5 bytes
  372. $this->out->expects($this->at(3))
  373. ->method('write')
  374. ->with(str_repeat(' ', $length - 3), 0)
  375. ->will($this->returnValue($length - 3));
  376. // Backspaces
  377. $this->out->expects($this->at(4))
  378. ->method('write')
  379. ->with(str_repeat("\x08", $length), 0)
  380. ->will($this->returnValue($length));
  381. $this->out->expects($this->at(5))
  382. ->method('write')
  383. ->with('12', 0)
  384. ->will($this->returnValue(2));
  385. $this->out->expects($this->at(6))
  386. ->method('write')
  387. ->with(str_repeat(' ', $length - 2), 0);
  388. $this->io->out('12345');
  389. $this->io->overwrite('123', 0);
  390. $this->io->overwrite('12', 0);
  391. }
  392. /**
  393. * Test overwriting content with longer content
  394. *
  395. * @return void
  396. */
  397. public function testOverwriteWithLongerContent()
  398. {
  399. $this->out->expects($this->at(0))
  400. ->method('write')
  401. ->with('1')
  402. ->will($this->returnValue(1));
  403. // Backspaces
  404. $this->out->expects($this->at(1))
  405. ->method('write')
  406. ->with(str_repeat("\x08", 1), 0)
  407. ->will($this->returnValue(1));
  408. $this->out->expects($this->at(2))
  409. ->method('write')
  410. ->with('123', 0)
  411. ->will($this->returnValue(3));
  412. // Backspaces
  413. $this->out->expects($this->at(3))
  414. ->method('write')
  415. ->with(str_repeat("\x08", 3), 0)
  416. ->will($this->returnValue(3));
  417. $this->out->expects($this->at(4))
  418. ->method('write')
  419. ->with('12345', 0)
  420. ->will($this->returnValue(5));
  421. $this->io->out('1');
  422. $this->io->overwrite('123', 0);
  423. $this->io->overwrite('12345', 0);
  424. }
  425. /**
  426. * Tests that setLoggers works properly
  427. *
  428. * @return void
  429. */
  430. public function testSetLoggers()
  431. {
  432. Log::drop('stdout');
  433. Log::drop('stderr');
  434. $this->io->setLoggers(true);
  435. $this->assertNotEmpty(Log::engine('stdout'));
  436. $this->assertNotEmpty(Log::engine('stderr'));
  437. $this->io->setLoggers(false);
  438. $this->assertNull(Log::engine('stdout'));
  439. $this->assertNull(Log::engine('stderr'));
  440. }
  441. /**
  442. * Tests that setLoggers works properly with quiet
  443. *
  444. * @return void
  445. */
  446. public function testSetLoggersQuiet()
  447. {
  448. Log::drop('stdout');
  449. Log::drop('stderr');
  450. $this->io->setLoggers(ConsoleIo::QUIET);
  451. $this->assertEmpty(Log::engine('stdout'));
  452. $this->assertNotEmpty(Log::engine('stderr'));
  453. }
  454. /**
  455. * Tests that setLoggers works properly with verbose
  456. *
  457. * @return void
  458. */
  459. public function testSetLoggersVerbose()
  460. {
  461. Log::drop('stdout');
  462. Log::drop('stderr');
  463. $this->io->setLoggers(ConsoleIo::VERBOSE);
  464. $this->assertNotEmpty(Log::engine('stderr'));
  465. $engine = Log::engine('stdout');
  466. $this->assertEquals(['notice', 'info', 'debug'], $engine->getConfig('levels'));
  467. }
  468. /**
  469. * Ensure that setStyle() just proxies to stdout.
  470. *
  471. * @return void
  472. */
  473. public function testSetStyle()
  474. {
  475. $this->out->expects($this->once())
  476. ->method('setStyle')
  477. ->with('name', ['props']);
  478. $this->io->setStyle('name', ['props']);
  479. }
  480. /**
  481. * Ensure that getStyle() just proxies to stdout.
  482. *
  483. * @return void
  484. */
  485. public function testGetStyle()
  486. {
  487. $this->out->expects($this->once())
  488. ->method('getStyle')
  489. ->with('name');
  490. $this->io->getStyle('name');
  491. }
  492. /**
  493. * Ensure that styles() just proxies to stdout.
  494. *
  495. * @return void
  496. */
  497. public function testStyles()
  498. {
  499. $this->out->expects($this->once())
  500. ->method('styles');
  501. $this->io->styles();
  502. }
  503. /**
  504. * Test the helper method.
  505. *
  506. * @return void
  507. */
  508. public function testHelper()
  509. {
  510. $this->out->expects($this->once())
  511. ->method('write')
  512. ->with('It works!well ish');
  513. $helper = $this->io->helper('simple');
  514. $this->assertInstanceOf('Cake\Console\Helper', $helper);
  515. $helper->output(['well', 'ish']);
  516. }
  517. /**
  518. * Provider for output helpers
  519. *
  520. * @return array
  521. */
  522. public function outHelperProvider()
  523. {
  524. return [['info'], ['success']];
  525. }
  526. /**
  527. * Provider for err helpers
  528. *
  529. * @return array
  530. */
  531. public function errHelperProvider()
  532. {
  533. return [['warning'], ['error']];
  534. }
  535. /**
  536. * test out helper methods
  537. *
  538. * @dataProvider outHelperProvider
  539. * @return void
  540. */
  541. public function testOutHelpers($method)
  542. {
  543. $this->out->expects($this->at(0))
  544. ->method('write')
  545. ->with("<{$method}>Just a test</{$method}>", 1);
  546. $this->out->expects($this->at(1))
  547. ->method('write')
  548. ->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);
  549. $this->io->{$method}('Just a test');
  550. $this->io->{$method}(['Just', 'a test']);
  551. }
  552. /**
  553. * test err helper methods
  554. *
  555. * @dataProvider errHelperProvider
  556. * @return void
  557. */
  558. public function testErrHelpers($method)
  559. {
  560. $this->err->expects($this->at(0))
  561. ->method('write')
  562. ->with("<{$method}>Just a test</{$method}>", 1);
  563. $this->err->expects($this->at(1))
  564. ->method('write')
  565. ->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);
  566. $this->io->{$method}('Just a test');
  567. $this->io->{$method}(['Just', 'a test']);
  568. }
  569. /**
  570. * Test that createFile
  571. *
  572. * @return void
  573. */
  574. public function testCreateFileSuccess()
  575. {
  576. $this->err->expects($this->never())
  577. ->method('write');
  578. $path = TMP . 'shell_test';
  579. mkdir($path);
  580. $file = $path . DS . 'file1.php';
  581. $contents = 'some content';
  582. $result = $this->io->createFile($file, $contents);
  583. $this->assertTrue($result);
  584. $this->assertFileExists($file);
  585. $this->assertStringEqualsFile($file, $contents);
  586. }
  587. public function testCreateFileEmptySuccess()
  588. {
  589. $this->err->expects($this->never())
  590. ->method('write');
  591. $path = TMP . 'shell_test';
  592. mkdir($path);
  593. $file = $path . DS . 'file_empty.php';
  594. $contents = '';
  595. $result = $this->io->createFile($file, $contents);
  596. $this->assertTrue($result);
  597. $this->assertFileExists($file);
  598. $this->assertStringEqualsFile($file, $contents);
  599. }
  600. public function testCreateFileDirectoryCreation()
  601. {
  602. $this->err->expects($this->never())
  603. ->method('write');
  604. $directory = TMP . 'shell_test';
  605. $this->assertFileNotExists($directory, 'Directory should not exist before createFile');
  606. $path = $directory . DS . 'create.txt';
  607. $contents = 'some content';
  608. $result = $this->io->createFile($path, $contents);
  609. $this->assertTrue($result, 'File should create');
  610. $this->assertFileExists($path);
  611. $this->assertStringEqualsFile($path, $contents);
  612. }
  613. /**
  614. * Test that createFile with permissions error.
  615. *
  616. * @return void
  617. */
  618. public function testCreateFilePermissionsError()
  619. {
  620. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  621. $path = TMP . 'shell_test';
  622. $file = $path . DS . 'no_perms';
  623. if (!is_dir($path)) {
  624. mkdir($path);
  625. }
  626. chmod($path, 0444);
  627. $this->io->createFile($file, 'testing');
  628. $this->assertFileNotExists($file);
  629. chmod($path, 0744);
  630. rmdir($path);
  631. }
  632. /**
  633. * Test that `q` raises an error.
  634. *
  635. * @return void
  636. */
  637. public function testCreateFileOverwriteQuit()
  638. {
  639. $path = TMP . 'shell_test';
  640. mkdir($path);
  641. $file = $path . DS . 'file1.php';
  642. touch($file);
  643. $this->expectException(StopException::class);
  644. $this->in->expects($this->once())
  645. ->method('read')
  646. ->will($this->returnValue('q'));
  647. $this->io->createFile($file, 'some content');
  648. }
  649. /**
  650. * Test that `n` raises an error.
  651. *
  652. * @return void
  653. */
  654. public function testCreateFileOverwriteNo()
  655. {
  656. $path = TMP . 'shell_test';
  657. mkdir($path);
  658. $file = $path . DS . 'file1.php';
  659. file_put_contents($file, 'original');
  660. touch($file);
  661. $this->in->expects($this->once())
  662. ->method('read')
  663. ->will($this->returnValue('n'));
  664. $contents = 'new content';
  665. $result = $this->io->createFile($file, $contents);
  666. $this->assertFalse($result);
  667. $this->assertFileExists($file);
  668. $this->assertStringEqualsFile($file, 'original');
  669. }
  670. /**
  671. * Test the forceOverwrite parameter
  672. *
  673. * @return void
  674. */
  675. public function testCreateFileOverwriteParam()
  676. {
  677. $path = TMP . 'shell_test';
  678. mkdir($path);
  679. $file = $path . DS . 'file1.php';
  680. file_put_contents($file, 'original');
  681. touch($file);
  682. $contents = 'new content';
  683. $result = $this->io->createFile($file, $contents, true);
  684. $this->assertTrue($result);
  685. $this->assertFileExists($file);
  686. $this->assertStringEqualsFile($file, $contents);
  687. }
  688. /**
  689. * Test the `a` response
  690. *
  691. * @return void
  692. */
  693. public function testCreateFileOverwriteAll()
  694. {
  695. $path = TMP . 'shell_test';
  696. mkdir($path);
  697. $file = $path . DS . 'file1.php';
  698. file_put_contents($file, 'original');
  699. touch($file);
  700. $this->in->expects($this->once())
  701. ->method('read')
  702. ->will($this->returnValue('a'));
  703. $this->io->createFile($file, 'new content');
  704. $this->assertStringEqualsFile($file, 'new content');
  705. $this->io->createFile($file, 'newer content');
  706. $this->assertStringEqualsFile($file, 'newer content');
  707. $this->io->createFile($file, 'newest content', false);
  708. $this->assertStringEqualsFile(
  709. $file,
  710. 'newest content',
  711. 'overwrite state replaces parameter'
  712. );
  713. }
  714. }