diff --git a/tests/Behat/Bootstrap/AttachmentContextTrait.php b/tests/Behat/Bootstrap/AttachmentContextTrait.php index 7b234891..63a86b75 100644 --- a/tests/Behat/Bootstrap/AttachmentContextTrait.php +++ b/tests/Behat/Bootstrap/AttachmentContextTrait.php @@ -45,4 +45,18 @@ public function iShowTheAttachmentWithTheId(int $attachmentId) $api->getLastResponse() ); } + + /** + * @When I download the attachment with the id :attachmentId + */ + public function iDownloadTheAttachmentWithTheId(int $attachmentId) + { + /** @var Attachment */ + $api = $this->getNativeCurlClient()->getApi('attachment'); + + $this->registerClientResponse( + $api->download($attachmentId), + $api->getLastResponse() + ); + } } diff --git a/tests/Behat/Bootstrap/FeatureContext.php b/tests/Behat/Bootstrap/FeatureContext.php index 3a4ac40f..d51a9fa2 100644 --- a/tests/Behat/Bootstrap/FeatureContext.php +++ b/tests/Behat/Bootstrap/FeatureContext.php @@ -150,6 +150,14 @@ public function theResponseHasTheContent(string $content) $this->assertSame($content, $this->lastResponse->getContent()); } + /** + * @Then the response has the content + */ + public function theResponseHasTheContentWithMultipleLines(PyStringNode $string) + { + $this->assertSame($string->getRaw(), $this->lastResponse->getContent()); + } + /** * @Then the returned data is true */ @@ -174,6 +182,14 @@ public function theReturnedDataIsExactly(string $content) $this->assertSame($content, $this->lastReturn); } + /** + * @Then the returned data is exactly + */ + public function theReturnedDataIsExactlyWithMultipleLines(PyStringNode $string) + { + $this->assertSame($string->getRaw(), $this->lastReturn); + } + /** * @Then the returned data is an instance of :className */ diff --git a/tests/Behat/features/attachments.feature b/tests/Behat/features/attachments.feature index e68c12da..13c42362 100644 --- a/tests/Behat/features/attachments.feature +++ b/tests/Behat/features/attachments.feature @@ -64,3 +64,34 @@ Feature: Interacting with the REST API for attachments | property | value | | id | 1 | | name | Redmine Admin | + + @attachment @error + Scenario: Try to show details of a non-existing attachment + Given I have a "NativeCurlClient" client + When I show the attachment with the id "1" + Then the response has the status code "404" + And the response has the content type "application/json" + And the response has the content "" + And the returned data is false + + @attachment + Scenario: Downloading an attachment + Given I have a "NativeCurlClient" client + And I upload the content of the file "%tests_dir%/Fixtures/testfile_01.txt" with the following data + | property | value | + | filename | testfile.txt | + When I download the attachment with the id "1" + Then the response has the status code "200" + And the response has the content type "text/plain" + And the response has the content + """ + This is a test file. + It will be needed for testing file uploads. + + """ + And the returned data is exactly + """ + This is a test file. + It will be needed for testing file uploads. + + """ diff --git a/tests/Unit/Api/Attachment/DownloadTest.php b/tests/Unit/Api/Attachment/DownloadTest.php new file mode 100644 index 00000000..dc82f636 --- /dev/null +++ b/tests/Unit/Api/Attachment/DownloadTest.php @@ -0,0 +1,47 @@ +assertSame($expectedReturn, $api->download($id)); + } + + public static function getDownloadData(): array + { + return [ + 'string response with integer id' => [5, '/attachments/download/5', 'attachment-content', 'attachment-content'], + 'string response with string id' => ['5', '/attachments/download/5', 'attachment-content', 'attachment-content'], + 'false response' => [5, '/attachments/download/5', '', false], + ]; + } +} diff --git a/tests/Unit/Api/AttachmentTest.php b/tests/Unit/Api/AttachmentTest.php index efaa5564..d37f2c9e 100644 --- a/tests/Unit/Api/AttachmentTest.php +++ b/tests/Unit/Api/AttachmentTest.php @@ -58,34 +58,6 @@ public static function responseCodeProvider(): array ]; } - /** - * Test download(). - * - * @covers ::download - * @test - */ - public function testDownloadReturnsUndecodedClientGetResponse() - { - // Test values - $response = 'API Response'; - - // Create the used mock objects - $client = $this->createMock(Client::class); - $client->expects($this->once()) - ->method('requestGet') - ->with($this->equalTo('/attachments/download/5')) - ->willReturn(true); - $client->expects($this->exactly(1)) - ->method('getLastResponseBody') - ->willReturn($response); - - // Create the object under test - $api = new Attachment($client); - - // Perform the tests - $this->assertSame($response, $api->download(5)); - } - /** * Test upload(). *