From ee8a8f903c23896d445e660ffa65a89831776a1f Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 24 Oct 2015 01:16:37 +0200 Subject: [PATCH] [FEATURE] Add an example for vfsStream --- Classes/Utility/FileUtility.php | 45 +++++++ README.md | 3 + Tests/Functional/Utility/FileUtilityTest.php | 126 +++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 Classes/Utility/FileUtility.php create mode 100644 Tests/Functional/Utility/FileUtilityTest.php diff --git a/Classes/Utility/FileUtility.php b/Classes/Utility/FileUtility.php new file mode 100644 index 0000000..6442541 --- /dev/null +++ b/Classes/Utility/FileUtility.php @@ -0,0 +1,45 @@ + + */ +class FileUtility { + /** + * Concatenates the files given as $sourceFilePaths and writes their contents into $targetFilePath. + * + * @param string $targetFilePath + * @param string[] $sourceFilePaths + * + * @return void + * + * @throws \InvalidArgumentException + */ + public function concatenate($targetFilePath, array $sourceFilePaths) { + if ($targetFilePath === '') { + throw new \InvalidArgumentException('$targetFileName must not be empty.', 1445631384); + } + + $concatenatedContents = ''; + foreach ($sourceFilePaths as $sourceFilePath) { + $concatenatedContents .= file_get_contents($sourceFilePath); + } + + file_put_contents($targetFilePath, $concatenatedContents); + } +} diff --git a/README.md b/README.md index d8b58fd..f90f3d5 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ This TYPO3 extension is an example for writing unit tests for extbase extensions for TYPO3 CMS using PHPUnit. +The functional test for the Utility/FileUtility class provides examples +for working with [vfsStream](https://github.com/mikey179/vfsStream/). + ## About me (Oliver Klee) diff --git a/Tests/Functional/Utility/FileUtilityTest.php b/Tests/Functional/Utility/FileUtilityTest.php new file mode 100644 index 0000000..16a187a --- /dev/null +++ b/Tests/Functional/Utility/FileUtilityTest.php @@ -0,0 +1,126 @@ + + */ +class FileUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase { + /** + * @var \OliverKlee\Tea\Utility\FileUtility + */ + protected $subject = null; + + /** + * @var vfsStreamDirectory + */ + protected $root = null; + + /** @var string */ + protected $rootDirectoryName = 'home'; + + /** + * @var string + */ + protected $targetFilePath = ''; + + protected function setUp() { + $this->root = vfsStream::setup('home'); + $this->targetFilePath = vfsStream::url('home/target.txt'); + + $this->subject = new \OliverKlee\Tea\Utility\FileUtility(); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function concatenateWithEmptyTargetFileNameThrowsException() { + $this->subject->concatenate('', array('foo.txt')); + } + + /** + * @test + */ + public function concatenateWithNoSourceFilesCreatesEmptyTargetFile() { + $this->subject->concatenate($this->targetFilePath, array()); + + self::assertSame( + '', + file_get_contents($this->targetFilePath) + ); + } + + /** + * @test + */ + public function concatenateWithOneEmptySourceFileCreatesEmptyTargetFile() { + // This is one way to create a file with contents, using PHP's file functions. + $sourceFileName = vfsStream::url('home/source.txt'); + // Just calling vfsStream::url does not create the file yet. We need to write into it to create it. + file_put_contents($sourceFileName, ''); + + $this->subject->concatenate($this->targetFilePath, array($sourceFileName)); + + self::assertSame( + '', + file_get_contents($this->targetFilePath) + ); + } + + /** + * @test + */ + public function concatenateWithOneFileCopiesContentsFromSourceFileToTargetFile() { + // This is vfsStream's way of creating a file with contents. + $contents = 'Hello world!'; + $sourceFileName = vfsStream::url('home/source.txt'); + vfsStream::newFile('source.txt')->at($this->root)->setContent($contents); + + $this->subject->concatenate($this->targetFilePath, array($sourceFileName)); + + self::assertSame( + $contents, + file_get_contents($this->targetFilePath) + ); + } + + /** + * @test + */ + public function concatenateWithTWoFileCopiesContentsFromBothFilesInOrderToTargetFile() { + $contents1 = 'Hello '; + $sourceFileName1 = vfsStream::url('home/source1.txt'); + file_put_contents($sourceFileName1, $contents1); + $contents2 = 'world!'; + $sourceFileName2 = vfsStream::url('home/source2.txt'); + file_put_contents($sourceFileName2, $contents2); + + $this->subject->concatenate( + $this->targetFilePath, + array($sourceFileName1, $sourceFileName2) + ); + + self::assertSame( + $contents1 . $contents2, + file_get_contents($this->targetFilePath) + ); + } +}