Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce end to end tests #360

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .docker/PHP83-Dockerfile
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 \
Expand Down
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
21 changes: 2 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,8 @@ services:
ports:
- "3000:3000"
environment:
REDMINE_DB_MYSQL: "mariadb"
REDMINE_DB_PORT: ""
REDMINE_DB_USERNAME: "redmine"
REDMINE_DB_PASSWORD: "redmine"
REDMINE_DB_DATABASE: "redmine"
REDMINE_SECRET_KEY_BASE: supersecretkey
REDMINE_PLUGINS_MIGRATE: true
depends_on:
- mariadb
volumes:
- ./.docker/redmine_data:/usr/src/redmine/files

mariadb:
image: mariadb:11.2
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "redmine"
MYSQL_USER: "redmine"
MYSQL_PASSWORD: "redmine"
volumes:
- ./.docker/mariadb_data:/var/lib/mysql

- ./.docker/redmine_data/files:/usr/src/redmine/files
- ./.docker/redmine_data/sqlite:/usr/src/redmine/sqlite
74 changes: 74 additions & 0 deletions tests/End2End/ClientTestCase.php
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
);
}
}
74 changes: 74 additions & 0 deletions tests/End2End/Group/GroupTest.php
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)
);
}
}