2015-10-24 01:16:37 +02:00
|
|
|
<?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>
|
|
|
|
*/
|
2016-05-07 21:43:25 +02:00
|
|
|
class FileUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
|
|
|
|
{
|
2016-07-16 21:39:04 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $backupGlobals = false;
|
|
|
|
|
2016-05-07 21:43:25 +02:00
|
|
|
/**
|
|
|
|
* @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()
|
|
|
|
{
|
2016-05-07 21:56:27 +02:00
|
|
|
$this->subject->concatenate('', ['foo.txt']);
|
2016-05-07 21:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function concatenateWithNoSourceFilesCreatesEmptyTargetFile()
|
|
|
|
{
|
2016-05-07 21:56:27 +02:00
|
|
|
$this->subject->concatenate($this->targetFilePath, []);
|
2016-05-07 21:43:25 +02:00
|
|
|
|
2016-05-08 14:57:07 +02:00
|
|
|
self::assertSame('', file_get_contents($this->targetFilePath));
|
2016-05-07 21:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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, '');
|
|
|
|
|
2016-05-07 21:56:27 +02:00
|
|
|
$this->subject->concatenate($this->targetFilePath, [$sourceFileName]);
|
2016-05-07 21:43:25 +02:00
|
|
|
|
2016-05-08 14:57:07 +02:00
|
|
|
self::assertSame('', file_get_contents($this->targetFilePath));
|
2016-05-07 21:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
2016-05-07 21:56:27 +02:00
|
|
|
$this->subject->concatenate($this->targetFilePath, [$sourceFileName]);
|
2016-05-07 21:43:25 +02:00
|
|
|
|
2016-05-08 14:57:07 +02:00
|
|
|
self::assertSame($contents, file_get_contents($this->targetFilePath));
|
2016-05-07 21:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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,
|
2016-05-07 21:56:27 +02:00
|
|
|
[$sourceFileName1, $sourceFileName2]
|
2016-05-07 21:43:25 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
self::assertSame(
|
|
|
|
$contents1 . $contents2,
|
|
|
|
file_get_contents($this->targetFilePath)
|
|
|
|
);
|
|
|
|
}
|
2015-10-24 01:16:37 +02:00
|
|
|
}
|