Skip to content

Commit

Permalink
Improve tests for Attachment::download()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Feb 15, 2024
1 parent 6c25ce3 commit 323aa2f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 28 deletions.
14 changes: 14 additions & 0 deletions tests/Behat/Bootstrap/AttachmentContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
16 changes: 16 additions & 0 deletions tests/Behat/Bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/Behat/features/attachments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
47 changes: 47 additions & 0 deletions tests/Unit/Api/Attachment/DownloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Redmine\Tests\Unit\Api\Attachment;

use PHPUnit\Framework\TestCase;
use Redmine\Api\Attachment;
use Redmine\Tests\Fixtures\AssertingHttpClient;

/**
* @covers \Redmine\Api\Attachment::download
*/
class DownloadTest extends TestCase
{
/**
* @dataProvider getDownloadData
*/
public function testDownloadReturnsCorrectResponse($id, $expectedPath, $response, $expectedReturn)
{
$client = AssertingHttpClient::create(
$this,
[
'GET',
$expectedPath,
'',
'',
200,
'application/json',
$response
]
);

// Create the object under test
$api = new Attachment($client);

// Perform the tests
$this->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],
];
}
}
28 changes: 0 additions & 28 deletions tests/Unit/Api/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down

0 comments on commit 323aa2f

Please sign in to comment.