From e40f9bee35a6a09c1942b489ff00a7e38a1d4090 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Thu, 26 May 2022 13:18:47 +0100 Subject: [PATCH] Issue #12 Upgrade to PHP 8.1 Still Flysystem 1.x but with a minimum PHP version 8.1 --- .gitignore | 4 +- README.md | 9 ++- composer.json | 9 +-- phpunit.xml.dist | 6 -- tests/AzureFileStorageAdapterLiveTest.php | 83 +++++++++++++++++------ tests/AzureFileStorageAdapterTest.php | 80 +++++++++++----------- 6 files changed, 118 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 1eaadf6..389dbf2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ -vendor/ +/vendor bin coverage coverage.xml composer.lock /.idea +/.phpunit.result.cache +/.env diff --git a/README.md b/README.md index ac478f0..f4b4e35 100644 --- a/README.md +++ b/README.md @@ -83,5 +83,10 @@ $url = $filesystem->getAdapter()->getUrl('path/to/my/foo.bar'); ## Testing -There are no tests yet. -These will be added when time permits. +Set up `.env` and run live tests: + + composer install + vendor/bin/phpunit --testsuite flysystem-azure-live-tests + +These will create/delete a few test files and directories in the root +of the Azure file share. diff --git a/composer.json b/composer.json index f0fd62d..1734b32 100644 --- a/composer.json +++ b/composer.json @@ -21,13 +21,14 @@ } ], "require": { - "php": ">=7.2.0", + "php": ">=8.1.0", "league/flysystem": "^1.0.50", - "microsoft/azure-storage-file": "^1.0" + "microsoft/azure-storage-file": "^1.2.5" }, "require-dev": { - "phpunit/phpunit": "^5.7.25", - "mockery/mockery": "0.9.*" + "phpunit/phpunit": "^9.0", + "mockery/mockery": "^1.5", + "vlucas/phpdotenv": "^5.4" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 61e6595..d56ca94 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,7 +7,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true" - syntaxCheck="true" defaultTestSuite="flysystem-azure-tests" > @@ -18,11 +17,6 @@ tests/AzureFileStorageAdapterLiveTest.php - - - ./src/ - - diff --git a/tests/AzureFileStorageAdapterLiveTest.php b/tests/AzureFileStorageAdapterLiveTest.php index 66a8ff5..a8486ae 100644 --- a/tests/AzureFileStorageAdapterLiveTest.php +++ b/tests/AzureFileStorageAdapterLiveTest.php @@ -1,14 +1,17 @@ load(); + // } + /** * Provides a live adapter based on config. * There are three adapters - one with no prefex, one with a single level @@ -30,6 +44,13 @@ class AzureFileStorageAdapterLiveTest extends TestCase */ public function adapterProvider() { + // @fixme why does this work here, but not in the setup? + // It's like the environment is cleared after every iteration of a test + // but only set up prior to the first iteration. + + $dotenv = Dotenv::createUnsafeImmutable(__DIR__ . '/..'); + $dotenv->load(); + // Assert that required environment variables are set. // Will be bool (false) if not set at all. @@ -194,7 +215,7 @@ public function testHasFail($filesystem) /** * @dataProvider adapterProvider - * @expectedException League\Flysystem\FileExistsException + * @expectedException \League\Flysystem\FileExistsException */ public function testWrite($filesystem) { @@ -202,18 +223,22 @@ public function testWrite($filesystem) // Try writing again and we get an exception. // It is unclear what condition triggers a `false` return. + $this->expectException(FileExistsException::class); + foreach ([1, 2] as $attempt) { $this->assertTrue($filesystem->write( $this->filename(5), 'content', - ['visibility' => 'public']) - ); + ['visibility' => 'public'], + )); } + + $filesystem->delete($this->filename(5)); } /** * @dataProvider adapterProvider - * @expectedException League\Flysystem\FileExistsException + * @expectedException \League\Flysystem\FileExistsException */ public function testWriteDir($filesystem) { @@ -221,6 +246,8 @@ public function testWriteDir($filesystem) // Try writing again and we get an exception. // It is unclear what condition triggers a `false` return. + $this->expectException(FileExistsException::class); + foreach ([1, 2] as $attempt) { $this->assertTrue( $filesystem->write( @@ -242,6 +269,8 @@ public function testWriteDirTwo($filesystem) // Try writing again and we get an exception. // It is unclear what condition triggers a `false` return. + $this->expectException(FileExistsException::class); + foreach ([1, 2] as $attempt) { $this->assertTrue( $filesystem->write( @@ -259,6 +288,8 @@ public function testWriteDirTwo($filesystem) */ public function testWriteStream($filesystem) { + $this->expectException(FileExistsException::class); + foreach ([1, 2] as $attempt) { $this->assertTrue( $filesystem->writeStream( @@ -281,6 +312,8 @@ public function testWriteStream($filesystem) */ public function testWriteStreamDirOne($filesystem) { + $this->expectException(FileExistsException::class); + foreach ([1, 2] as $attempt) { $this->assertTrue( $filesystem->writeStream( @@ -310,13 +343,15 @@ public function testDeleteFile($filesystem) /** * @dataProvider adapterProvider - * @expectedException League\Flysystem\FileNotFoundException + * @expectedException \League\Flysystem\FileNotFoundException */ public function testDeleteFileFail($filesystem) { // Deleting again will thow an exception. // It is flysystem core that does that. + $this->expectException(FileNotFoundException::class); + $this->assertTrue($filesystem->delete($this->filename(8))); } @@ -329,6 +364,8 @@ public function testDeleteFileFailDirOne($filesystem) // Deleting again will thow an exception. // It is flysystem core that does that. + $this->expectException(FileNotFoundException::class); + $this->assertTrue($filesystem->delete($this->filename(8, self::SUBDIR_ONE))); } @@ -359,6 +396,8 @@ public function testUpdateFail($filesystem) // Updating a file that does not exist will throw an exception. // It is flysystem core that does that. + $this->expectException(FileNotFoundException::class); + $filesystem->update($this->filename(15), 'foobar15'); } @@ -391,6 +430,8 @@ public function testSetVisibility($filesystem) { // Visibility is not supported by this driver. + $this->expectException(LogicException::class); + $filesystem->setVisibility($this->filename(5), 'public'); } @@ -405,6 +446,8 @@ public function testGetVisibility($filesystem) { // Visibility is not supported by this driver. + $this->expectException(LogicException::class); + $filesystem->getVisibility($this->filename(5)); } @@ -470,10 +513,10 @@ public function testRename($filesystem) /** * @dataProvider adapterProvider */ - public function testDeleteDir($filesystem) - { - // TBC - } + // public function testDeleteDir($filesystem) + // { + // // TBC + // } /** * @dataProvider adapterProvider @@ -482,7 +525,7 @@ public function testListContents($filesystem) { $allContents = $filesystem->listContents('', true); - $this->assertInternalType('array', $allContents); + $this->assertIsArray($allContents); // Look for the directories and files we have created. @@ -521,8 +564,8 @@ public function testListContents($filesystem) $directory = dirname($this->filename(3, self::SUBDIR_ONE)); $this->assertContains([ - 'type' => 'dir', 'path' => $directory, + 'type' => 'dir', 'dirname' => (dirname($directory) === '.' ? '' : dirname($directory)), 'basename' => basename($directory), 'filename' => basename($directory), @@ -531,8 +574,8 @@ public function testListContents($filesystem) $directory = dirname($this->filename(3, self::SUBDIR_TWO)); $this->assertContains([ - 'type' => 'dir', 'path' => $directory, + 'type' => 'dir', 'dirname' => dirname($directory), 'basename' => basename($directory), 'filename' => basename($directory), @@ -542,7 +585,7 @@ public function testListContents($filesystem) $subdirContents = $filesystem->listContents(self::SUBDIR_TWO, true); - $this->assertInternalType('array', $subdirContents); + $this->assertIsArray($subdirContents); // The full paths are as for the recursive list, but only comntain the // two files in the selected direectory. @@ -579,7 +622,7 @@ public function testGetMeta($filesystem) // Note that minetype is the mimetype of the API endpoint, and not // of the actual file. Some further research may be needed there. - $this->assertInternalType('int', $filesystem->getTimestamp($this->filename(1))); // Unix timetamp + $this->assertIsInt($filesystem->getTimestamp($this->filename(1))); // Unix timetamp $this->assertSame(7, $filesystem->getSize($this->filename(1))); // "content" } @@ -606,7 +649,7 @@ public function testReadStream($filesystem) { $stream = $filesystem->readStream($this->filename(1)); - $this->assertInternalType('resource', $stream); + $this->assertIsResource($stream); $this->assertSame( 'content', diff --git a/tests/AzureFileStorageAdapterTest.php b/tests/AzureFileStorageAdapterTest.php index 512cf6f..3d61103 100644 --- a/tests/AzureFileStorageAdapterTest.php +++ b/tests/AzureFileStorageAdapterTest.php @@ -13,60 +13,60 @@ class AzureFileStorageAdapterTest extends TestCase { - public function adapterProvider() - { - // Mock needs to be an instance of interface MicrosoftAzure\Storage\File\Internal\IFile - // implementation normally MicrosoftAzure\Storage\File\FileRestProxy + // public function adapterProvider() + // { + // // Mock needs to be an instance of interface MicrosoftAzure\Storage\File\Internal\IFile + // // implementation normally MicrosoftAzure\Storage\File\FileRestProxy - $mockAzureClient = Mockery::mock(FileRestProxy::class)->makePartial(); + // $mockAzureClient = Mockery::mock(FileRestProxy::class)->makePartial(); - $adapter = new AzureFileAdapter($mockAzureClient, [ - 'container' => 'foo-container', - ]); + // $adapter = new AzureFileAdapter($mockAzureClient, [ + // 'container' => 'foo-container', + // ]); - $filesystem = new Filesystem($adapter); + // $filesystem = new Filesystem($adapter); - return [ - [$filesystem, $adapter, $mockAzureClient], - ]; - } + // return [ + // [$filesystem, $adapter, $mockAzureClient], + // ]; + // } /** * @dataProvider adapterProvider */ - public function testHas($filesystem, $adapter, $mockAzureClient) - { - $mockAzureClient->shouldReceive('getFileProperties')->andReturn([ - 'type' => 'xxx', - 'mtime' => time(), - 'size' => 20, - 'permissions' => 0777, - ]); + // public function testHas($filesystem, $adapter, $mockAzureClient) + // { + // $mockAzureClient->shouldReceive('getFileProperties')->andReturn([ + // 'type' => 'xxx', + // 'mtime' => time(), + // 'size' => 20, + // 'permissions' => 0777, + // ]); - $responseInterface = Mockery::mock(ResponseInterface::class)->makePartial(); - $responseInterface->shouldReceive('getStatusCode')->andReturn(404); - $responseInterface->shouldReceive('getReasonPhrase')->andReturn('File not found'); - $responseInterface->shouldReceive('getBody')->andReturn(''); + // $responseInterface = Mockery::mock(ResponseInterface::class)->makePartial(); + // $responseInterface->shouldReceive('getStatusCode')->andReturn(404); + // $responseInterface->shouldReceive('getReasonPhrase')->andReturn('File not found'); + // $responseInterface->shouldReceive('getBody')->andReturn(''); - /*$mockAzureClient->shouldReceive('getFileProperties') - ->willThrowException(new ServiceException($responseInterface));*/ + // $mockAzureClient->shouldReceive('getFileProperties') + // ->willThrowException(new ServiceException($responseInterface)); - /*$mockAzureClient->shouldReceive('getFileProperties')->will( - // First parameter must be ResponseInterface + // $mockAzureClient->shouldReceive('getFileProperties')->will( + // // First parameter must be ResponseInterface - (new ServiceException($responseInterface)) - );*/ + // (new ServiceException($responseInterface)) + // ); - /*$mock = $this->createMock(FileRestProxy::class); - $mock->expects($this->once()) - ->method('getFileProperties') - ->willThrowException(new ServiceException($responseInterface));*/ + // $mock = $this->createMock(FileRestProxy::class); + // $mock->expects($this->once()) + // ->method('getFileProperties') + // ->willThrowException(new ServiceException($responseInterface)); - //$this->assertTrue($filesystem->has('something')); + // $this->assertTrue($filesystem->has('something')); - // Giving up. I just don't have the time to mock up Azure filesystem API - // in all its intricacies and details, including th exceptions that is throws. + // // Giving up. I just don't have the time to mock up Azure filesystem API + // // in all its intricacies and details, including th exceptions that is throws. - $this->assertTrue(true); - } + // $this->assertTrue(true); + // } }