format('Y-m-d'), File::created($testFile)->format('Y-m-d')); \unlink($testFile); } /** * @testdox The directories last change date can be returned * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticChangedAt() : void { $testFile = __DIR__ . '/test.txt'; self::assertTrue(File::create($testFile)); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), File::changed($testFile)->format('Y-m-d')); \unlink($testFile); } /** * @testdox A file can be deleted * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticDelete() : void { $testFile = __DIR__ . '/test.txt'; self::assertTrue(File::create($testFile)); self::assertTrue(File::delete($testFile)); self::assertFalse(File::exists($testFile)); } /** * @testdox A none-existing file cannot be deleted * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidStaticDelete() : void { $testFile = __DIR__ . '/test.txt'; self::assertFalse(File::delete($testFile)); } /** * @testdox The size of a file can be returned * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticSize() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } File::put($testFile, 'test', ContentPutMode::CREATE); self::assertGreaterThan(0, File::size($testFile)); \unlink($testFile); } /** * @testdox The permission of a file can be returned * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticPermission() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } File::put($testFile, 'test', ContentPutMode::CREATE); self::assertGreaterThan(0, File::permission($testFile)); \unlink($testFile); } /** * @testdox The permission of a none-existing file is negative * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidStaticPermission() : void { $testFile = __DIR__ . '/test.txt'; self::assertEquals(-1, File::permission($testFile)); } public function testPathInfo() : void { $testFile = __DIR__ . '/test.txt'; self::assertEquals([ 'dirname' => __DIR__, 'basename' => 'test.txt', 'filename' => 'test', 'extension' => 'txt', ], File::pathInfo($testFile) ); } /** * @testdox A file can be copied to a different location * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticCopy() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/sub/path/testing.txt'; File::put($testFile, 'test', ContentPutMode::CREATE); self::assertTrue(File::copy($testFile, $newPath)); self::assertTrue(File::exists($newPath)); self::assertEquals('test', File::get($newPath)); \unlink($newPath); \rmdir(__DIR__ . '/sub/path/'); \rmdir(__DIR__ . '/sub/'); \unlink($testFile); } /** * @testdox A file cannot be copied to a different location if the destination already exists * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidStaticCopy() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/test2.txt'; if (\file_exists($newPath)) { \unlink($newPath); } File::put($testFile, 'test', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE); self::assertFalse(File::copy($testFile, $newPath)); self::assertEquals('test2', File::get($newPath)); \unlink($newPath); \unlink($testFile); } /** * @testdox A file can be forced to be copied to a different location even if the destination already exists * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticCopyOverwrite() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/test2.txt'; if (\file_exists($newPath)) { \unlink($newPath); } File::put($testFile, 'test', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE); self::assertTrue(File::copy($testFile, $newPath, true)); self::assertEquals('test', File::get($newPath)); \unlink($newPath); \unlink($testFile); } /** * @testdox A file can be moved to a different location * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticMove() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/sub/path/testing.txt'; File::put($testFile, 'test', ContentPutMode::CREATE); self::assertTrue(File::move($testFile, $newPath)); self::assertFalse(File::exists($testFile)); self::assertTrue(File::exists($newPath)); self::assertEquals('test', File::get($newPath)); \unlink($newPath); \rmdir(__DIR__ . '/sub/path/'); \rmdir(__DIR__ . '/sub/'); } /** * @testdox A file cannot be moved to a different location if the destination already exists * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidStaticMove() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/test2.txt'; File::put($testFile, 'test', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE); self::assertFalse(File::move($testFile, $newPath)); self::assertTrue(File::exists($testFile)); self::assertEquals('test2', File::get($newPath)); \unlink($newPath); \unlink($testFile); } /** * @testdox A file can be forced to be moved to a different location even if the destination already exists * @covers phpOMS\System\File\Local\File * @group framework */ public function testStaticMoveOverwrite() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $newPath = __DIR__ . '/test2.txt'; File::put($testFile, 'test', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE); self::assertTrue(File::move($testFile, $newPath, true)); self::assertFalse(File::exists($testFile)); self::assertEquals('test', File::get($newPath)); \unlink($newPath); } public function testStaticOwner() : void { $dirTestPath = __DIR__ . '/dirtest/test.txt'; self::assertNotEmpty(File::owner($dirTestPath)); } public function testFileNameSanitizing() : void { self::assertEquals('/some/test/[path.txt', File::sanitize(':#&^$/some%/test/[path!.txt')); } /** * @testdox The size of a none-existing file is negative * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidSizePath() : void { self::assertEquals(-1, File::size(__DIR__ . '/invalid.txt')); } /** * @testdox A none-existing file cannot be copied * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidCopyPath() : void { self::assertFalse(File::copy(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); } /** * @testdox A none-existing file cannot be moved * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidMovePath() : void { self::assertFalse(File::move(__DIR__ . '/invalid.txt', __DIR__ . '/invalid2.txt')); } /** * @testdox Reading the content of a none-existing file throws a PathException * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidGetPath() : void { $this->expectException(\phpOMS\System\File\PathException::class); File::get(__DIR__ . '/invalid.txt'); } /** * @testdox Reading the created date of a none-existing file throws a PathException * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidCreatedPath() : void { $this->expectException(\phpOMS\System\File\PathException::class); File::created(__DIR__ . '/invalid.txt'); } /** * @testdox Reading the last change date of a none-existing file throws a PathException * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidChangedPath() : void { $this->expectException(\phpOMS\System\File\PathException::class); File::changed(__DIR__ . '/invalid.txt'); } /** * @testdox Reading the owner of a none-existing file throws a PathException * @covers phpOMS\System\File\Local\File * @group framework */ public function testInvalidOwnerPath() : void { $this->expectException(\phpOMS\System\File\PathException::class); File::owner(__DIR__ . '/invalid.txt'); } public function testNodeInputOutput() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); self::assertTrue($file->setContent('test')); self::assertEquals('test', $file->getContent()); \unlink($testFile); } public function testNodeReplace() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); self::assertTrue($file->setContent('test')); self::assertTrue($file->setContent('test2')); self::assertEquals('test2', $file->getContent()); \unlink($testFile); } public function testNodeAppend() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); self::assertTrue($file->setContent('test')); self::assertTrue($file->appendContent('2')); self::assertEquals('test2', $file->getContent()); \unlink($testFile); } public function testNodePrepend() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); self::assertTrue($file->setContent('test')); self::assertTrue($file->prependContent('2')); self::assertEquals('2test', $file->getContent()); \unlink($testFile); } public function testNodeExtension() : void { $testFile = __DIR__ . '/test.txt'; $file = new File($testFile); self::assertEquals('txt', $file->getExtension()); } public function testNodeCreatedAt() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), $file->getCreatedAt()->format('Y-m-d')); \unlink($testFile); } public function testNodeChangedAt() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); $now = new \DateTime('now'); self::assertEquals($now->format('Y-m-d'), $file->getChangedAt()->format('Y-m-d')); \unlink($testFile); } public function testNodeOwner() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertNotEmpty($file->getOwner()); } public function testNodePermission() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertGreaterThan(0, $file->getPermission()); } public function testDirname() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertEquals('dirtest', $file->getDirname()); } public function testName() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertEquals('test', $file->getName()); } public function testBaseame() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertEquals('test.txt', $file->getBasename()); } public function testDirpath() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath()); } public function testParentOutput() : void { $testFile = __DIR__ . '/dirtest/test.txt'; $file = new File($testFile); self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath()); } public function testNodeCreate() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); self::assertTrue(\file_exists($testFile)); \unlink($testFile); } public function testNodeDelete() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); self::assertTrue(\file_exists($testFile)); self::assertTrue($file->deleteNode()); self::assertFalse(\file_exists($testFile)); } public function testNodeCopy() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); self::assertTrue($file->copyNode(__DIR__ . '/test2.txt')); self::assertTrue(\file_exists($testFile)); self::assertTrue(\file_exists(__DIR__ . '/test2.txt')); \unlink($testFile); \unlink(__DIR__ . '/test2.txt'); } public function testNodeMove() : void { $testFile = __DIR__ . '/test.txt'; if (\file_exists($testFile)) { \unlink($testFile); } $file = new File($testFile); $file->createNode(); self::assertTrue($file->moveNode(__DIR__ . '/test2.txt')); self::assertFalse(\file_exists($testFile)); self::assertTrue(\file_exists(__DIR__ . '/test2.txt')); \unlink(__DIR__ . '/test2.txt'); } public function testNodeExists() : void { $file = new File(__DIR__ . '/dirtest/test.txt'); $file2 = new File(__DIR__ . '/invalid.txt'); self::assertTrue($file->isExisting()); self::assertFalse($file2->isExisting()); } public function testNodeParent() : void { $file = new File(__DIR__ . '/dirtest/test.txt'); self::assertEquals('Local', $file->getParent()->getName()); } public function testNodeDirectory() : void { $file = new File(__DIR__ . '/dirtest/test.txt'); self::assertEquals('dirtest', $file->getDirectory()->getName()); } }