fileSystemTmpId = 'test_' . uniqid(); $fileSystemService = ServiceManager::getServiceManager()->get(FileSystemService::SERVICE_ID); $this->fileSystem = $fileSystemService->createLocalFileSystem($this->fileSystemTmpId); $this->idFixture = 123; $this->pathFixture = 'fixture'; $this->accessProvider = $this->getAccessProvider($this->pathFixture); $this->instance = new \tao_models_classes_service_StorageDirectory($this->idFixture, $this->fileSystemTmpId, $this->pathFixture, $this->accessProvider); $this->instance->setServiceLocator(ServiceManager::getServiceManager()); } public function tearDown(): void { $fileSystemService = ServiceManager::getServiceManager()->get(FileSystemService::SERVICE_ID); $fileSystemService->unregisterFileSystem($this->fileSystemTmpId); $reflectionClass = new \ReflectionClass(AbstractAdapter::class); $reflectionProperty = $reflectionClass->getProperty('pathPrefix'); $reflectionProperty->setAccessible(true); $this->rrmdir($reflectionProperty->getValue($this->fileSystem->getAdapter())); $this->instance = false; } protected function rrmdir($dir) { foreach (glob($dir . '/*') as $file) { if (is_dir($file)) { $this->rrmdir($file); } else { unlink($file); } } rmdir($dir); } protected function getAccessProvider($pathFixture) { $providerFixture = $this->getMockBuilder('oat\tao\model\websource\TokenWebSource')->getMock(); $providerFixture->method('getAccessUrl')->with($pathFixture . DIRECTORY_SEPARATOR)->willReturn('polop'); return $providerFixture; } public function testAssertInstanceOfDirectory() { $this->assertInstanceOf(Directory::class, $this->instance); $this->assertEquals($this->idFixture, $this->instance->getId()); $this->assertEquals($this->pathFixture, $this->instance->getPrefix()); $this->assertEquals($this->fileSystem, $this->instance->getFileSystem()); } public function testIsPublic() { $this->assertTrue($this->instance->isPublic()); $this->assertEquals('polop', $this->instance->getPublicAccessUrl()); } public function testIsNotPublic() { $this->instance = new \tao_models_classes_service_StorageDirectory($this->idFixture, $this->fileSystem, $this->pathFixture, null); $this->instance->setServiceLocator(ServiceManager::getServiceManager()); $this->assertFalse($this->instance->isPublic()); $this->expectException(common_Exception::class); $this->instance->getPublicAccessUrl(); } /** * Test read and write from resource */ public function testReadWrite() { $tmpFile = uniqid() . '.php'; $content = file_get_contents(__DIR__ . '/samples/43bytes.php', 'r'); $file = $this->instance->getFile($tmpFile); $file->write($content); $this->assertEquals($content, $file->read()); } /** * Test read and write from stream */ public function testWriteReadPHPStream() { $tmpFile = uniqid() . '.php'; $resource = fopen(__DIR__ . '/samples/43bytes.php', 'r'); $file = $this->instance->getFile($tmpFile); $file->write($resource); fclose($resource); $readFixture = $file->readStream(); $this->assertEquals( file_get_contents(__DIR__ . '/samples/43bytes.php'), stream_get_contents($readFixture) ); fclose($readFixture); } public function testWriteReadPsrStream() { $tmpFile = uniqid() . '.php'; $resource = fopen(__DIR__ . '/samples/43bytes.php', 'r'); $streamFixture = \GuzzleHttp\Psr7\stream_for($resource); $file = $this->instance->getFile($tmpFile); $file->write($streamFixture); fclose($resource); $streamFixture->close(); $readStreamFixture = $file->readPsrStream(); $this->assertInstanceOf(\GuzzleHttp\Psr7\Stream::class, $readStreamFixture); $this->assertEquals( file_get_contents(__DIR__ . '/samples/43bytes.php'), $readStreamFixture->getContents() ); $readStreamFixture->close(); } /** * Test write stream in case of remote resource */ public function testWriteRemoteStream() { $tmpFile = uniqid() . '.php'; $client = new \GuzzleHttp\Client(); $response = $client->get('http://www.google.org'); $this->assertTrue($this->instance->getFile($tmpFile)->write($response->getBody())); $this->assertNotEquals(0, $response->getBody()->getSize()); } /** * Test write stream in case of remote resource */ public function testSeekToEndOfFileForWriteStream() { $tmpFile = uniqid() . '.php'; $resource = fopen(__DIR__ . '/samples/43bytes.php', 'r'); fseek($resource, 43); $streamFixture = \GuzzleHttp\Psr7\stream_for($resource); $file = $this->instance->getFile($tmpFile); $file->write($streamFixture); $streamFixture->rewind(); $this->assertEquals($streamFixture->getContents(), $file->readPsrStream()->getContents()); fclose($resource); $streamFixture->close(); } /** * Test exception for unseekable resource */ public function testUnseekableResource() { $tmpFile = uniqid() . '.php'; // @todo requesting example.com is not a good idea as test should ideally be isolated from external world $resource = fopen('http://example.com', 'r'); $streamFixture = \GuzzleHttp\Psr7\stream_for($resource); $this->expectException(common_Exception::class); $this->instance->getFile($tmpFile)->write($streamFixture); fclose($resource); $streamFixture->close(); } /** * Test has and delete file function with no file */ public function testHasAndDeleteWithNoFile() { $tmpFile = uniqid() . '.php'; $file = $this->instance->getFile($tmpFile); $this->assertFalse($file->exists()); $this->assertFalse($file->delete()); } /** * Test has and delete file function with valid file */ public function testHasAndDeleteWithValidFile() { $tmpFile = uniqid() . '.php'; $resource = fopen(__DIR__ . '/samples/43bytes.php', 'r'); $streamFixture = \GuzzleHttp\Psr7\stream_for($resource); $file = $this->instance->getFile($tmpFile); $file->write($streamFixture); $this->assertTrue($file->exists()); $this->assertTrue($file->delete()); fclose($resource); $streamFixture->close(); } public function testGetPath() { $reflectionClass = new \ReflectionClass(AbstractAdapter::class); $reflectionProperty = $reflectionClass->getProperty('pathPrefix'); $reflectionProperty->setAccessible(true); $path = $reflectionProperty->getValue($this->fileSystem->getAdapter()) . $this->pathFixture; $this->assertEquals($path, $this->instance->getPath()); } }