mirror of https://github.com/FriendsOfTYPO3/tea.git synced 2024-11-23 11:56:12 +01:00
tea/Tests/Functional/Utility/FileUtilityTest.php

134 lines
3.6 KiB
PHP
Raw Normal View History

2015-10-24 01:16:37 +02:00
<?php
declare(strict_types=1);
2015-10-24 01:16:37 +02:00
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 \Nimut\TestingFramework\TestCase\FunctionalTestCase
2016-05-07 21:43:25 +02:00
{
/**
* @var string[]
*/
protected $testExtensionsToLoad = ['typo3conf/ext/tea'];
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()
{
parent::setUp();
2016-05-07 21:43:25 +02:00
$this->root = vfsStream::setup('home');
$this->targetFilePath = vfsStream::url('home/target.txt');
$this->subject = new \OliverKlee\Tea\Utility\FileUtility();
}
/**
* @test
*/
public function concatenateWithEmptyTargetFileNameThrowsException()
{
2017-09-07 12:30:59 +02:00
$this->expectException(\InvalidArgumentException::class);
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
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
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
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
);
2017-09-08 18:06:02 +02:00
self::assertStringEqualsFile(
$this->targetFilePath, $contents1 . $contents2
2016-05-07 21:43:25 +02:00
);
}
2015-10-24 01:16:37 +02:00
}