From 93a3f72186c8629f3b8af710af851379b45fe427 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 11 Jan 2024 13:31:14 +0100 Subject: [PATCH] Extract redmine tasks into own testcase class, add composer script for e2e tests --- composer.json | 1 + tests/End2End/ClientTestCase.php | 68 +++++++++++++++++++++++++++++++ tests/End2End/Group/GroupTest.php | 68 +++++-------------------------- 3 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 tests/End2End/ClientTestCase.php diff --git a/composer.json b/composer.json index f9a45932..6ee3128d 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,7 @@ "scripts": { "codestyle": "php-cs-fixer fix", "coverage": "phpunit --coverage-html=\".phpunit.cache/code-coverage\"", + "end2end": "phpunit -- tests/End2End", "phpstan": "phpstan analyze --memory-limit 512M --configuration .phpstan.neon", "phpunit": "phpunit", "test": [ diff --git a/tests/End2End/ClientTestCase.php b/tests/End2End/ClientTestCase.php new file mode 100644 index 00000000..82a9a7c9 --- /dev/null +++ b/tests/End2End/ClientTestCase.php @@ -0,0 +1,68 @@ +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); + + $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) VALUES(:name, :value);'); + $stmt->execute([':name' => 'rest_api_enabled', ':value' => 1]); + + $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' => $adminUser['last_login_on'], + ':updated_on' => $adminUser['last_login_on'], + ]); + } + + 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 + ); + } +} diff --git a/tests/End2End/Group/GroupTest.php b/tests/End2End/Group/GroupTest.php index ef28d522..f38d2bc1 100644 --- a/tests/End2End/Group/GroupTest.php +++ b/tests/End2End/Group/GroupTest.php @@ -4,70 +4,22 @@ namespace Redmine\Tests\End2End\Group; -use PDO; -use PHPUnit\Framework\TestCase; -use Redmine\Client\NativeCurlClient; +use DateTimeImmutable; +use Redmine\Api\Group; +use Redmine\Tests\End2End\ClientTestCase; -class GroupTest extends TestCase +class GroupTest extends ClientTestCase { - private $client; - - private $sqliteFile; - - private $sqliteBackup; - - public function setUp(): void + public function testInteractionWithGroup(): void { - $this->sqliteFile = dirname(__FILE__, 4) . '/.docker/redmine_data/sqlite/redmine.db'; - $this->sqliteBackup = dirname(__FILE__, 4) . '/.docker/redmine_data/sqlite/redmine.db.bak'; - - // Create backup of database - copy($this->sqliteFile, $this->sqliteBackup); - - $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) VALUES(:name, :value);'); - $stmt->execute([':name' => 'rest_api_enabled', ':value' => 1]); + $client = $this->getNativeCurlClient(); - $apiKey = sha1((string) time()); + /** @var Group */ + $groupApi = $client->getApi('group'); - // 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' => $apiKey, - ':created_on' => $adminUser['last_login_on'], - ':updated_on' => $adminUser['last_login_on'], - ]); - - $this->client = new NativeCurlClient( - 'http://redmine:3000', - $apiKey - ); - } - - public function tearDown(): void - { - // Restore database from backup - copy($this->sqliteBackup, $this->sqliteFile); - } - - public function testInteractionWithGroup(): void - { // Create group - $xmlData = $this->client->getApi('group')->create([ - 'name' => 'test group ' . (new \DateTimeImmutable())->format('Y-m-d H:i:s'), + $xmlData = $groupApi->create([ + 'name' => 'test group ' . (new DateTimeImmutable())->format('Y-m-d H:i:s'), ]); $data = json_decode(json_encode($xmlData), true);