diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-09 20:27:27 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-12 11:58:50 +0400 |
commit | 24e75d7cb99964a3886d3b33356806c210783b6f (patch) | |
tree | 1eaa7026e34d2e43a8672fb9d298487d6998252c /tests | |
parent | 6320f5e35a766b8cf1bb618c87192943fbcef62b (diff) |
refs #5936 added many tests for the new filesystem methods to make sure they only delete the files they are supposed to do
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Core/FilesystemTest.php | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/tests/PHPUnit/Core/FilesystemTest.php b/tests/PHPUnit/Core/FilesystemTest.php new file mode 100644 index 0000000000..26518c1a08 --- /dev/null +++ b/tests/PHPUnit/Core/FilesystemTest.php @@ -0,0 +1,245 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +use Piwik\Filesystem; + +/** + * @group Core + */ +class FilesystemTest extends PHPUnit_Framework_TestCase +{ + private $testPath; + + public function setUp() + { + parent::setUp(); + $this->testPath = PIWIK_INCLUDE_PATH . '/tmp/filesystemtest'; + Filesystem::mkdir($this->testPath); + } + + public function tearDown() + { + Filesystem::unlinkRecursive($this->testPath, true); + + parent::tearDown(); + } + + public function test_sortFilesDescByPathLength_shouldNotFail_IfEmptyArrayGiven() + { + $result = Filesystem::sortFilesDescByPathLength(array()); + $this->assertEquals(array(), $result); + } + + public function test_sortFilesDescByPathLength_shouldNotChangeOrder_IfAllHaveSameLength() + { + $input = array('xyz/1.gif', 'x/xyz.gif', 'xxyyzzgg'); + $result = Filesystem::sortFilesDescByPathLength($input); + $this->assertEquals($input, $result); + } + + public function test_sortFilesDescByPathLength_shouldOrderDesc_IfDifferentLengthsGiven() + { + $input = array('xyz/1.gif', '1.gif', 'x', 'x/xyz.gif', 'xyz', 'xxyyzzgg', 'xyz/long.gif'); + $result = Filesystem::sortFilesDescByPathLength($input); + + $expected = array( + 'xyz/long.gif', + 'x/xyz.gif', + 'xyz/1.gif', + 'xxyyzzgg', + '1.gif', + 'xyz', + 'x', + ); + $this->assertEquals($expected, $result); + } + + public function test_directoryDiff_shouldNotReturnDifference_IfBothDirectoriesAreSame() + { + $dir = PIWIK_INCLUDE_PATH . '/core'; + $result = Filesystem::directoryDiff($dir, $dir); + + $this->assertEquals(array(), $result); + } + + public function test_directoryDiff_shouldNotReturnAnything_IfTargetEmpty() + { + $result = Filesystem::directoryDiff($this->createSourceFiles(), $this->createEmptyTarget()); + + $this->assertEquals(array(), $result); + } + + public function test_directoryDiff_shouldReturnAllTargetFiles_IfSourceIsEmpty() + { + $result = Filesystem::directoryDiff($this->createEmptySource(), $this->createTargetFiles()); + + $this->assertEquals(array( + '/DataTable', + '/DataTable/BaseFilter.php', + '/DataTable/Bridges.php', + '/DataTable/DataTableInterface.php', + '/DataTable/Filter', + '/DataTable/Manager.php', + '/DataTable/Map.php', + '/DataTable/Renderer', + '/DataTable/Renderer.php', + '/DataTable/Row', + '/DataTable/Row.php', + '/DataTable/Simple.php', + '/DataTable/Renderer/Console.php', + '/DataTable/Renderer/Csv.php', + '/DataTable/Renderer/Html.php', + '/DataTable/Renderer/Json.php', + '/DataTable/Renderer/Php.php', + '/DataTable/Renderer/Rss.php', + '/DataTable/Renderer/Tsv.php', + '/DataTable/Renderer/Xml', + '/DataTable/Renderer/Xml.php', + '/DataTable/Renderer/Xml/Other.php', + '/DataTable/Row/DataTableSummaryRow.php' + ), $result); + } + + public function test_directoryDiff_shouldReturnFilesPresentInTargetButNotSource_IfSourceAndTargetGiven() + { + $result = Filesystem::directoryDiff($this->createSourceFiles(), $this->createTargetFiles()); + + $this->assertEquals(array( + '/DataTable/Filter', + '/DataTable/Row', + '/DataTable/Renderer/Json.php', + '/DataTable/Renderer/Php.php', + '/DataTable/Renderer/Rss.php', + '/DataTable/Renderer/Xml', + '/DataTable/Renderer/Xml/Other.php', + '/DataTable/Row/DataTableSummaryRow.php', + ), $result); + } + + public function test_unlinkTargetFilesNotPresentInSource_shouldUnlinkFilesPresentInTargetButNotSource_IfSourceAndTargetGiven() + { + $source = $this->createSourceFiles(); + $target = $this->createTargetFiles(); + + // make sure there is a difference between those folders + $result = Filesystem::directoryDiff($source, $target); + $this->assertCount(8, $result); + + Filesystem::unlinkTargetFilesNotPresentInSource($source, $target); + + // make sure there is no longer a difference + $result = Filesystem::directoryDiff($source, $target); + $this->assertEquals(array(), $result); + + $result = Filesystem::directoryDiff($target, $source); + $this->assertEquals(array( + '/DataTable/NotInTarget.php', + '/DataTable/Renderer/NotInTarget.php' + ), $result); + } + + public function test_unlinkTargetFilesNotPresentInSource_shouldNotFail_IfBothEmpty() + { + $source = $this->createEmptySource(); + $target = $this->createEmptyTarget(); + + Filesystem::unlinkTargetFilesNotPresentInSource($source, $target); + } + + public function test_unlinkTargetFilesNotPresentInSource_shouldUnlinkAllTargetFiles_IfSourceIsEmpty() + { + $source = $this->createEmptySource(); + $target = $this->createTargetFiles(); + + // make sure there is a difference between those folders + $result = Filesystem::directoryDiff($source, $target); + $this->assertNotEmpty($result); + + Filesystem::unlinkTargetFilesNotPresentInSource($source, $target); + + // make sure there is no longer a difference + $result = Filesystem::directoryDiff($source, $target); + $this->assertEquals(array(), $result); + + $result = Filesystem::directoryDiff($target, $source); + $this->assertEquals(array(), $result); + } + + private function createSourceFiles() + { + $source = $this->createEmptySource(); + Filesystem::mkdir($source . '/DataTable'); + Filesystem::mkdir($source . '/DataTable/Renderer'); + + file_put_contents($source . '/DataTable/Renderer/Console.php', ''); + file_put_contents($source . '/DataTable/Renderer/Csv.php', ''); + file_put_contents($source . '/DataTable/Renderer/Html.php', ''); + file_put_contents($source . '/DataTable/Renderer/Tsv.php', ''); + file_put_contents($source . '/DataTable/Renderer/Xml.php', ''); + file_put_contents($source . '/DataTable/Renderer/NotInTarget.php', ''); + + file_put_contents($source . '/DataTable/BaseFilter.php', ''); + file_put_contents($source . '/DataTable/Bridges.php', ''); + file_put_contents($source . '/DataTable/DataTableInterface.php', ''); + file_put_contents($source . '/DataTable/NotInTarget.php', ''); + file_put_contents($source . '/DataTable/Manager.php', ''); + file_put_contents($source . '/DataTable/Map.php', ''); + file_put_contents($source . '/DataTable/Renderer.php', ''); + file_put_contents($source . '/DataTable/Row.php', ''); + file_put_contents($source . '/DataTable/Simple.php', ''); + + return $source; + } + + private function createTargetFiles() + { + $target = $this->createEmptyTarget(); + Filesystem::mkdir($target . '/DataTable'); + Filesystem::mkdir($target . '/DataTable/Filter'); + Filesystem::mkdir($target . '/DataTable/Renderer'); + Filesystem::mkdir($target . '/DataTable/Renderer/Xml'); + Filesystem::mkdir($target . '/DataTable/Row'); + + file_put_contents($target . '/DataTable/Renderer/Console.php', ''); + file_put_contents($target . '/DataTable/Renderer/Csv.php', ''); + file_put_contents($target . '/DataTable/Renderer/Html.php', ''); + file_put_contents($target . '/DataTable/Renderer/Json.php', ''); + file_put_contents($target . '/DataTable/Renderer/Php.php', ''); + file_put_contents($target . '/DataTable/Renderer/Rss.php', ''); + file_put_contents($target . '/DataTable/Renderer/Tsv.php', ''); + file_put_contents($target . '/DataTable/Renderer/Xml.php', ''); + file_put_contents($target . '/DataTable/Renderer/Xml/Other.php', ''); + + file_put_contents($target . '/DataTable/Row/DataTableSummaryRow.php', ''); + + file_put_contents($target . '/DataTable/BaseFilter.php', ''); + file_put_contents($target . '/DataTable/Bridges.php', ''); + file_put_contents($target . '/DataTable/DataTableInterface.php', ''); + file_put_contents($target . '/DataTable/Manager.php', ''); + file_put_contents($target . '/DataTable/Map.php', ''); + file_put_contents($target . '/DataTable/Renderer.php', ''); + file_put_contents($target . '/DataTable/Row.php', ''); + file_put_contents($target . '/DataTable/Simple.php', ''); + + return $target; + } + + private function createEmptySource() + { + Filesystem::mkdir($this->testPath . '/source'); + + return $this->testPath . '/source'; + } + + private function createEmptyTarget() + { + Filesystem::mkdir($this->testPath . '/target'); + + return $this->testPath . '/target'; + } + +}
\ No newline at end of file |