From 6f18305b95f37f8998ee98e5cd4705f4b553cb8f Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Mon, 28 Dec 2020 16:23:29 +0000 Subject: [PATCH 1/2] switch from travisci to github actions --- .github/workflows/continuous-integration.yml | 23 ++++++++++++++++++++ .travis.yml | 10 --------- 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/continuous-integration.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..3e8e5ba --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,23 @@ +name: "Continuous Integration" + +on: + pull_request: + pull_request_target: + push: + branches: + - master + +jobs: + ci: + name: "Run Build" + runs-on: ubuntu-latest + + steps: + - name: "Checkout" + uses: actions/checkout@v2 + + - name: "Run Lint" + run: make lint + + - name: "Run Tests" + run: make test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 61b853e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -sudo: required - -language: generic - -services: - - docker - -script: - - make lint - - make tests \ No newline at end of file From 01dbffee216a6f325f8d22dd25dc8407a5765592 Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Mon, 28 Dec 2020 12:43:33 +0000 Subject: [PATCH 2/2] remove thecodingmachine/safe --- composer.json | 4 +--- phpstan.neon | 3 +-- src/Client.php | 20 +++++++++++----- src/DocumentFactory.php | 13 +++++++---- src/FilesystemException.php | 23 ++++++++++++++++++ tests/ClientTest.php | 1 - tests/DocumentFactoryTest.php | 1 - tests/FilesystemExceptionTest.php | 39 +++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 src/FilesystemException.php create mode 100644 tests/FilesystemExceptionTest.php diff --git a/composer.json b/composer.json index 12485ab..32f2e38 100644 --- a/composer.json +++ b/composer.json @@ -28,14 +28,12 @@ "php-http/httplug": ">=1.0", "php-http/discovery": "^1.0", "guzzlehttp/psr7": "^1.4.2", - "php-http/message": "^1.0", - "thecodingmachine/safe": "^1.0" + "php-http/message": "^1.0" }, "require-dev" : { "phpunit/phpunit": "^7", "squizlabs/php_codesniffer": "^3.2", "phpstan/phpstan": "^0.12.7", - "thecodingmachine/phpstan-safe-rule": "^1.0", "thecodingmachine/phpstan-strict-rules": "^0.12.0", "php-http/mock-client": "^1.0", "php-http/guzzle6-adapter": "^1.1", diff --git a/phpstan.neon b/phpstan.neon index 54a57ce..04205e9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,2 @@ includes: - - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon - - vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon \ No newline at end of file + - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 69e1472..c7229a9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -11,10 +11,9 @@ use Http\Discovery\MessageFactoryDiscovery; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Safe\Exceptions\FilesystemException; -use function Safe\fclose; -use function Safe\fopen; -use function Safe\fwrite; +use function fclose; +use function fopen; +use function fwrite; final class Client { @@ -57,8 +56,17 @@ public function store(GotenbergRequestInterface $request, string $destination): $response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); $fileStream = $response->getBody(); $fp = fopen($destination, 'w'); - fwrite($fp, $fileStream->getContents()); - fclose($fp); + if ($fp === false) { + throw FilesystemException::createFromPhpError(); + } + + if (fwrite($fp, $fileStream->getContents()) === false) { + throw FilesystemException::createFromPhpError(); + } + + if (fclose($fp) === false) { + throw FilesystemException::createFromPhpError(); + } } private function makeMultipartFormDataRequest(GotenbergRequestInterface $request): RequestInterface diff --git a/src/DocumentFactory.php b/src/DocumentFactory.php index 85f99f0..3e77422 100644 --- a/src/DocumentFactory.php +++ b/src/DocumentFactory.php @@ -6,10 +6,9 @@ use GuzzleHttp\Psr7\LazyOpenStream; use Psr\Http\Message\StreamInterface; -use Safe\Exceptions\FilesystemException; +use function fopen; +use function fwrite; use function GuzzleHttp\Psr7\stream_for; -use function Safe\fopen; -use function Safe\fwrite; final class DocumentFactory { @@ -29,7 +28,13 @@ public static function makeFromStream(string $fileName, StreamInterface $fileStr public static function makeFromString(string $fileName, string $string): Document { $fileStream = fopen('php://memory', 'rb+'); - fwrite($fileStream, $string); + if ($fileStream === false) { + throw FilesystemException::createFromPhpError(); + } + + if (fwrite($fileStream, $string) === false) { + throw FilesystemException::createFromPhpError(); + } return new Document($fileName, stream_for($fileStream)); } diff --git a/src/FilesystemException.php b/src/FilesystemException.php new file mode 100644 index 0000000..034b272 --- /dev/null +++ b/src/FilesystemException.php @@ -0,0 +1,23 @@ +assertInstanceOf(ErrorException::class, $exception); + $this->assertSame('fclose(): supplied resource is not a valid stream resource', $exception->getMessage()); + $this->assertSame(0, $exception->getCode()); + $this->assertSame(2, $exception->getSeverity()); + } + + public function testExceptionThrownWhenNoError(): void + { + error_clear_last(); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('No error information available'); + + FilesystemException::createFromPhpError(); + } +}