mirror of
https://github.com/FriendsOfTYPO3/tea.git
synced 2024-11-22 12:56:12 +01:00
[FEATURE] Add an example for vfsStream
This commit is contained in:
parent
bd16a2a531
commit
ee8a8f903c
3 changed files with 174 additions and 0 deletions
45
Classes/Utility/FileUtility.php
Normal file
45
Classes/Utility/FileUtility.php
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
namespace OliverKlee\Tea\Utility;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the TYPO3 CMS project.
|
||||||
|
*
|
||||||
|
* It is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, either version 2
|
||||||
|
* of the License, or any later version.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the
|
||||||
|
* LICENSE.txt file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* The TYPO3 project - inspiring people to share!
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides functions concerning files.
|
||||||
|
*
|
||||||
|
* @author Oliver Klee <typo3-coding@oliverklee.de>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,9 @@
|
||||||
This TYPO3 extension is an example for writing unit tests for extbase
|
This TYPO3 extension is an example for writing unit tests for extbase
|
||||||
extensions for TYPO3 CMS using PHPUnit.
|
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)
|
## About me (Oliver Klee)
|
||||||
|
|
||||||
|
|
126
Tests/Functional/Utility/FileUtilityTest.php
Normal file
126
Tests/Functional/Utility/FileUtilityTest.php
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
<?php
|
||||||
|
namespace OliverKlee\Tea\Tests\Functional\Utility;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the TYPO3 CMS project.
|
||||||
|
*
|
||||||
|
* It is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, either version 2
|
||||||
|
* of the License, or any later version.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the
|
||||||
|
* LICENSE.txt file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* The TYPO3 project - inspiring people to share!
|
||||||
|
*/
|
||||||
|
|
||||||
|
use org\bovigo\vfs\vfsStream;
|
||||||
|
use org\bovigo\vfs\vfsStreamDirectory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case.
|
||||||
|
*
|
||||||
|
* @author Oliver Klee <typo3-coding@oliverklee.de>
|
||||||
|
*/
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue