Skip to content

Commit

Permalink
Extract redmine tasks into own testcase class, add composer script fo…
Browse files Browse the repository at this point in the history
…r e2e tests
  • Loading branch information
Art4 committed Jan 11, 2024
1 parent ad141f2 commit 93a3f72
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
68 changes: 68 additions & 0 deletions tests/End2End/ClientTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Redmine\Tests\End2End;

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);

$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
);
}
}
68 changes: 10 additions & 58 deletions tests/End2End/Group/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 93a3f72

Please sign in to comment.