-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from Art4/introduce-e2e-tests
Introduce end to end tests
- Loading branch information
Showing
5 changed files
with
152 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM php:8.3-fpm | ||
FROM php:8.3.1-fpm | ||
|
||
RUN apt-get update | ||
RUN apt-get --yes --no-install-recommends install \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Redmine\Tests\End2End; | ||
|
||
use DateTimeImmutable; | ||
use PDO; | ||
use PHPUnit\Framework\TestCase; | ||
use Redmine\Client\NativeCurlClient; | ||
|
||
class ClientTestCase extends TestCase | ||
{ | ||
private $apiKey; | ||
|
||
private $sqliteFile; | ||
|
||
private $sqliteBackup; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->sqliteFile = dirname(__FILE__, 3) . '/.docker/redmine_data/sqlite/redmine.db'; | ||
$this->sqliteBackup = dirname(__FILE__, 3) . '/.docker/redmine_data/sqlite/redmine.db.bak'; | ||
|
||
// Create backup of database | ||
copy($this->sqliteFile, $this->sqliteBackup); | ||
|
||
$now = new DateTimeImmutable(); | ||
$pdo = new PDO('sqlite:' . $this->sqliteFile); | ||
|
||
// Get admin user to check sqlite connection | ||
$stmt = $pdo->prepare('SELECT * FROM users WHERE login = :login;'); | ||
$stmt->execute([':login' => 'admin']); | ||
$adminUser = $stmt->fetch(PDO::FETCH_ASSOC); | ||
|
||
// Update admin user | ||
$stmt = $pdo->prepare('UPDATE users SET must_change_passwd = :must_change_passwd WHERE id = :id;'); | ||
$stmt->execute([':id' => $adminUser['id'], ':must_change_passwd' => 0]); | ||
|
||
// Enable rest api | ||
$stmt = $pdo->prepare('INSERT INTO settings(name, value, updated_on) VALUES(:name, :value, :updated_on);'); | ||
$stmt->execute([ | ||
':name' => 'rest_api_enabled', | ||
':value' => 1, | ||
':updated_on' => $now->format('Y-m-d H:i:s.u'), | ||
]); | ||
|
||
$this->apiKey = sha1((string) time()); | ||
|
||
// Create api token for admin user | ||
$stmt = $pdo->prepare('INSERT INTO tokens(user_id, action, value, created_on, updated_on) VALUES(:user_id, :action, :value, :created_on, :updated_on);'); | ||
$stmt->execute([ | ||
':user_id' => $adminUser['id'], | ||
':action' => 'api', | ||
':value' => $this->apiKey, | ||
':created_on' => $now->format('Y-m-d H:i:s.u'), | ||
':updated_on' => $now->format('Y-m-d H:i:s.u'), | ||
]); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
// Restore database from backup | ||
copy($this->sqliteBackup, $this->sqliteFile); | ||
} | ||
|
||
protected function getNativeCurlClient(): NativeCurlClient | ||
{ | ||
return new NativeCurlClient( | ||
'http://redmine:3000', | ||
$this->apiKey | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Redmine\Tests\End2End\Group; | ||
|
||
use DateTimeImmutable; | ||
use Redmine\Api\Group; | ||
use Redmine\Tests\End2End\ClientTestCase; | ||
|
||
class GroupTest extends ClientTestCase | ||
{ | ||
public function testInteractionWithGroup(): void | ||
{ | ||
$client = $this->getNativeCurlClient(); | ||
|
||
/** @var Group */ | ||
$groupApi = $client->getApi('group'); | ||
$now = new DateTimeImmutable(); | ||
|
||
// Create group | ||
$groupName = 'test group ' . $now->format('Y-m-d H:i:s'); | ||
|
||
$xmlData = $groupApi->create([ | ||
'name' => $groupName, | ||
]); | ||
|
||
$groupData = json_decode(json_encode($xmlData), true); | ||
|
||
$this->assertIsArray($groupData, json_encode($groupData)); | ||
$this->assertIsString($groupData['id']); | ||
$this->assertSame($groupName, $groupData['name']); | ||
|
||
$groupId = (int) $groupData['id']; | ||
|
||
// List groups | ||
$this->assertSame( | ||
[ | ||
'groups' => [ | ||
[ | ||
'id' => $groupId, | ||
'name' => $groupName, | ||
], | ||
], | ||
], | ||
$groupApi->list() | ||
); | ||
|
||
// Read group | ||
$this->assertSame( | ||
[ | ||
'group' => [ | ||
'id' => $groupId, | ||
'name' => $groupName, | ||
] | ||
], | ||
$groupApi->show($groupId) | ||
); | ||
|
||
// Update group | ||
$result = $groupApi->update($groupId, ['name' => 'new group name']); | ||
$this->assertSame('', $result); | ||
|
||
$this->assertSame( | ||
[ | ||
'group' => [ | ||
'id' => $groupId, | ||
'name' => 'new group name', | ||
] | ||
], | ||
$groupApi->show($groupId) | ||
); | ||
} | ||
} |