|
|
@@ -15,6 +15,7 @@
|
|
|
namespace Cake\Test\TestCase\Console;
|
|
|
|
|
|
use Cake\Console\ConsoleIo;
|
|
|
+use Cake\Filesystem\Folder;
|
|
|
use Cake\Log\Log;
|
|
|
use Cake\TestSuite\TestCase;
|
|
|
|
|
|
@@ -47,6 +48,20 @@ class ConsoleIoTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * teardown method
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function tearDown()
|
|
|
+ {
|
|
|
+ parent::tearDown();
|
|
|
+ if (is_dir(TMP . 'shell_test')) {
|
|
|
+ $folder = new Folder(TMP . 'shell_test');
|
|
|
+ $folder->delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Provider for testing choice types.
|
|
|
*
|
|
|
* @return array
|
|
|
@@ -557,4 +572,148 @@ class ConsoleIoTest extends TestCase
|
|
|
$this->io->{$method}('Just a test');
|
|
|
$this->io->{$method}(['Just', 'a test']);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that createFile
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFileSuccess()
|
|
|
+ {
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ mkdir($path);
|
|
|
+
|
|
|
+ $file = $path . DS . 'file1.php';
|
|
|
+ $contents = 'some content';
|
|
|
+ $result = $this->io->createFile($file, $contents);
|
|
|
+
|
|
|
+ $this->assertTrue($result);
|
|
|
+ $this->assertFileExists($file);
|
|
|
+ $this->assertEquals($contents, file_get_contents($file));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that createFile with permissions error.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFilePermissionsError()
|
|
|
+ {
|
|
|
+ $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
|
|
|
+
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ $file = $path . DS . 'no_perms';
|
|
|
+
|
|
|
+ if (!is_dir($path)) {
|
|
|
+ mkdir($path);
|
|
|
+ }
|
|
|
+ chmod($path, 0444);
|
|
|
+
|
|
|
+ $this->io->createFile($file, 'testing');
|
|
|
+ $this->assertFileNotExists($file);
|
|
|
+
|
|
|
+ chmod($path, 0744);
|
|
|
+ rmdir($path);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that `q` raises an error.
|
|
|
+ *
|
|
|
+ * @expectedException \Cake\Console\Exception\StopException
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFileOverwriteQuit()
|
|
|
+ {
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ mkdir($path);
|
|
|
+
|
|
|
+ $file = $path . DS . 'file1.php';
|
|
|
+ touch($file);
|
|
|
+
|
|
|
+ $this->in->expects($this->once())
|
|
|
+ ->method('read')
|
|
|
+ ->will($this->returnValue('q'));
|
|
|
+
|
|
|
+ $this->io->createFile($file, 'some content');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that `n` raises an error.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFileOverwriteNo()
|
|
|
+ {
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ mkdir($path);
|
|
|
+
|
|
|
+ $file = $path . DS . 'file1.php';
|
|
|
+ file_put_contents($file, 'original');
|
|
|
+ touch($file);
|
|
|
+
|
|
|
+ $this->in->expects($this->once())
|
|
|
+ ->method('read')
|
|
|
+ ->will($this->returnValue('n'));
|
|
|
+
|
|
|
+ $contents = 'new content';
|
|
|
+ $result = $this->io->createFile($file, $contents);
|
|
|
+
|
|
|
+ $this->assertFalse($result);
|
|
|
+ $this->assertFileExists($file);
|
|
|
+ $this->assertEquals('original', file_get_contents($file));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test the forceOverwrite parameter
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFileOverwriteParam()
|
|
|
+ {
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ mkdir($path);
|
|
|
+
|
|
|
+ $file = $path . DS . 'file1.php';
|
|
|
+ file_put_contents($file, 'original');
|
|
|
+ touch($file);
|
|
|
+
|
|
|
+ $contents = 'new content';
|
|
|
+ $result = $this->io->createFile($file, $contents, true);
|
|
|
+
|
|
|
+ $this->assertTrue($result);
|
|
|
+ $this->assertFileExists($file);
|
|
|
+ $this->assertEquals($contents, file_get_contents($file));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test the `a` response
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testCreateFileOverwriteAll()
|
|
|
+ {
|
|
|
+ $path = TMP . 'shell_test';
|
|
|
+ mkdir($path);
|
|
|
+
|
|
|
+ $file = $path . DS . 'file1.php';
|
|
|
+ file_put_contents($file, 'original');
|
|
|
+ touch($file);
|
|
|
+
|
|
|
+ $this->in->expects($this->once())
|
|
|
+ ->method('read')
|
|
|
+ ->will($this->returnValue('a'));
|
|
|
+
|
|
|
+ $this->io->createFile($file, 'new content');
|
|
|
+ $this->assertEquals('new content', file_get_contents($file));
|
|
|
+
|
|
|
+ $this->io->createFile($file, 'newer content');
|
|
|
+ $this->assertEquals('newer content', file_get_contents($file));
|
|
|
+
|
|
|
+ $this->io->createFile($file, 'newest content', false);
|
|
|
+ $this->assertEquals(
|
|
|
+ 'newest content',
|
|
|
+ file_get_contents($file),
|
|
|
+ 'overwrite state replaces parameter'
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|