Skip to content

Commit 2409bb7

Browse files
committed
Improve tests for User::remove()
1 parent 1baf964 commit 2409bb7

File tree

4 files changed

+80
-25
lines changed

4 files changed

+80
-25
lines changed

tests/Behat/Bootstrap/UserContextTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ public function iShowTheUserWithId(int $userId)
6262
$api->getLastResponse()
6363
);
6464
}
65+
66+
/**
67+
* @When I remove the user with id :userId
68+
*/
69+
public function iRemoveTheUserWithId($userId)
70+
{
71+
/** @var User */
72+
$api = $this->getNativeCurlClient()->getApi('user');
73+
74+
$this->registerClientResponse(
75+
$api->remove($userId),
76+
$api->getLastResponse()
77+
);
78+
}
6579
}

tests/Behat/features/user.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,18 @@ Feature: Interacting with the REST API for users
113113
And the response has the content type "application/json"
114114
And the response has the content ""
115115
And the returned data is false
116+
117+
@user
118+
Scenario: Removing an user
119+
Given I have a "NativeCurlClient" client
120+
And I create a user with the following data
121+
| property | value |
122+
| login | username |
123+
| firstname | first |
124+
| lastname | last |
125+
| mail | mail@example.com |
126+
When I remove the user with id "4"
127+
Then the response has the status code "204"
128+
And the response has an empty content type
129+
And the response has the content ""
130+
And the returned data is exactly ""

tests/Unit/Api/User/RemoveTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Api\User;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
use Redmine\Api\User;
9+
use Redmine\Tests\Fixtures\AssertingHttpClient;
10+
11+
#[CoversClass(User::class)]
12+
class RemoveTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider getRemoveData
16+
*/
17+
#[DataProvider('getRemoveData')]
18+
public function testRemoveReturnsCorrectResponse($id, $expectedPath, $responseCode, $response)
19+
{
20+
$client = AssertingHttpClient::create(
21+
$this,
22+
[
23+
'DELETE',
24+
$expectedPath,
25+
'application/xml',
26+
'',
27+
$responseCode,
28+
'',
29+
$response
30+
]
31+
);
32+
33+
// Create the object under test
34+
$api = new User($client);
35+
36+
// Perform the tests
37+
$this->assertSame($response, $api->remove($id));
38+
}
39+
40+
public static function getRemoveData(): array
41+
{
42+
return [
43+
'test with integer' => [
44+
5,
45+
'/users/5.xml',
46+
204,
47+
'',
48+
],
49+
];
50+
}
51+
}

tests/Unit/Api/UserTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,6 @@ public function testAllReturnsClientGetResponseWithParameters()
183183
$this->assertSame($expectedReturn, $api->all($parameters));
184184
}
185185

186-
/**
187-
* Test remove().
188-
*/
189-
public function testRemoveCallsDelete()
190-
{
191-
// Test values
192-
$response = 'API Response';
193-
194-
// Create the used mock objects
195-
$client = $this->createMock(Client::class);
196-
$client->expects($this->once())
197-
->method('requestDelete')
198-
->with('/users/5.xml')
199-
->willReturn(true);
200-
$client->expects($this->exactly(1))
201-
->method('getLastResponseBody')
202-
->willReturn($response);
203-
204-
// Create the object under test
205-
$api = new User($client);
206-
207-
// Perform the tests
208-
$this->assertSame($response, $api->remove(5));
209-
}
210-
211186
/**
212187
* Test listing().
213188
*/

0 commit comments

Comments
 (0)