Skip to content

Commit 1e881ae

Browse files
committed
add behat tests for Group::listNames()
1 parent 2bade9c commit 1e881ae

File tree

4 files changed

+118
-51
lines changed

4 files changed

+118
-51
lines changed

tests/Behat/Bootstrap/FeatureContext.php

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,36 @@ public function theReturnedDataIsAnInstanceOf(string $className)
205205
$this->assertInstanceOf($className, $this->lastReturn);
206206
}
207207

208+
/**
209+
* @Then the returned data is an array
210+
*/
211+
public function theReturnedDataIsAnArray()
212+
{
213+
$this->assertIsArray($this->lastReturn);
214+
}
215+
216+
/**
217+
* @Then the returned data contains :count items
218+
*/
219+
public function theReturnedDataContainsItems(int $count)
220+
{
221+
$this->assertCount($count, $this->lastReturn);
222+
}
223+
224+
/**
225+
* @Then the returned data contains the following data
226+
*/
227+
public function theReturnedDataContainsTheFollowingData(TableNode $table)
228+
{
229+
$returnData = $this->lastReturn;
230+
231+
if (! is_array($returnData)) {
232+
throw new RuntimeException('The returned data is not an array.');
233+
}
234+
235+
$this->assertTableNodeIsSameAsArray($table, $returnData);
236+
}
237+
208238
/**
209239
* @Then the returned data has only the following properties
210240
*/
@@ -257,54 +287,7 @@ public function theReturnedDataPropertyContainsTheFollowingData($property, Table
257287
throw new RuntimeException('The returned data on property "' . $property . '" is not an array.');
258288
}
259289

260-
foreach ($table as $row) {
261-
$this->assertArrayHasKey($row['property'], $returnData);
262-
263-
$value = $returnData[$row['property']];
264-
265-
if ($value instanceof SimpleXMLElement) {
266-
$value = strval($value);
267-
}
268-
269-
$expected = $row['value'];
270-
271-
// Handle expected empty array
272-
if ($value === [] && $expected === '[]') {
273-
$expected = [];
274-
}
275-
276-
// Handle expected int values
277-
if (is_int($value) && ctype_digit($expected)) {
278-
$expected = intval($expected);
279-
}
280-
281-
// Handle expected float values
282-
if (is_float($value) && is_numeric($expected)) {
283-
$expected = floatval($expected);
284-
}
285-
286-
// Handle expected null value
287-
if ($value === null && $expected === 'null') {
288-
$expected = null;
289-
}
290-
291-
// Handle expected true value
292-
if ($value === true && $expected === 'true') {
293-
$expected = true;
294-
}
295-
296-
// Handle expected false value
297-
if ($value === false && $expected === 'false') {
298-
$expected = false;
299-
}
300-
301-
// Handle placeholder %redmine_id%
302-
if (is_string($expected)) {
303-
$expected = str_replace('%redmine_id%', strval($this->redmine->getVersionId()), $expected);
304-
}
305-
306-
$this->assertSame($expected, $value, 'Error with property "' . $row['property'] . '"');
307-
}
290+
$this->assertTableNodeIsSameAsArray($table, $returnData);
308291
}
309292

310293
/**
@@ -384,4 +367,56 @@ private function getItemFromArray(array $array, $key): mixed
384367

385368
return $array;
386369
}
370+
371+
private function assertTableNodeIsSameAsArray(TableNode $table, array $data)
372+
{
373+
foreach ($table as $row) {
374+
$this->assertArrayHasKey($row['property'], $data, 'Possible keys are: ' . implode(', ', array_keys($data)));
375+
376+
$value = $data[$row['property']];
377+
378+
if ($value instanceof SimpleXMLElement) {
379+
$value = strval($value);
380+
}
381+
382+
$expected = $row['value'];
383+
384+
// Handle expected empty array
385+
if ($value === [] && $expected === '[]') {
386+
$expected = [];
387+
}
388+
389+
// Handle expected int values
390+
if (is_int($value) && ctype_digit($expected)) {
391+
$expected = intval($expected);
392+
}
393+
394+
// Handle expected float values
395+
if (is_float($value) && is_numeric($expected)) {
396+
$expected = floatval($expected);
397+
}
398+
399+
// Handle expected null value
400+
if ($value === null && $expected === 'null') {
401+
$expected = null;
402+
}
403+
404+
// Handle expected true value
405+
if ($value === true && $expected === 'true') {
406+
$expected = true;
407+
}
408+
409+
// Handle expected false value
410+
if ($value === false && $expected === 'false') {
411+
$expected = false;
412+
}
413+
414+
// Handle placeholder %redmine_id%
415+
if (is_string($expected)) {
416+
$expected = str_replace('%redmine_id%', strval($this->redmine->getVersionId()), $expected);
417+
}
418+
419+
$this->assertSame($expected, $value, 'Error with property "' . $row['property'] . '"');
420+
}
421+
}
387422
}

tests/Behat/Bootstrap/GroupContextTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public function iListAllGroups()
5757
);
5858
}
5959

60+
/**
61+
* @When I list the names of all groups
62+
*/
63+
public function iListTheNamesOfAllGroups()
64+
{
65+
/** @var Group */
66+
$api = $this->getNativeCurlClient()->getApi('group');
67+
68+
$this->registerClientResponse(
69+
$api->listNames(),
70+
$api->getLastResponse()
71+
);
72+
}
73+
6074
/**
6175
* @When I show the group with id :groupId
6276
*/

tests/Behat/features/groups.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ Feature: Interacting with the REST API for groups
5757
| id | 4 |
5858
| name | Test Group |
5959

60+
@group
61+
Scenario: Listing names of all groups
62+
Given I have a "NativeCurlClient" client
63+
And I create a group with name "Test Group 1"
64+
And I create a group with name "Test Group 2"
65+
And I create a group with name "Test Group 3"
66+
And I create a group with name "Test Group 4"
67+
And I create a group with name "Test Group 5"
68+
When I list the names of all groups
69+
Then the response has the status code "200"
70+
And the response has the content type "application/json"
71+
And the returned data is an array
72+
And the returned data contains "5" items
73+
And the returned data contains the following data
74+
| property | value |
75+
| 4 | Test Group 1 |
76+
| 5 | Test Group 2 |
77+
| 6 | Test Group 3 |
78+
| 7 | Test Group 4 |
79+
| 8 | Test Group 5 |
80+
6081
@group
6182
Scenario: Showing a specific group
6283
Given I have a "NativeCurlClient" client

tests/Unit/Api/Group/ListNamesTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
1010
use Redmine\Api\Group;
11-
use Redmine\Exception\MissingParameterException;
12-
use Redmine\Http\HttpClient;
1311
use Redmine\Tests\Fixtures\AssertingHttpClient;
14-
use SimpleXMLElement;
1512

1613
#[CoversClass(Group::class)]
1714
class ListNamesTest extends TestCase

0 commit comments

Comments
 (0)