Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boherm committed Apr 9, 2024
1 parent e7c5535 commit ae76bee
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace App\Tests\PullRequest\Application\CommandHandler;

use App\PullRequest\Application\Command\CheckTranslationsCommand;
use App\PullRequest\Application\CommandHandler\CheckTranslationsCommandHandler;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequest;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestDiff;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use App\PullRequest\Infrastructure\Adapter\InMemoryPullRequestRepository;
use App\Shared\Infrastructure\Provider\TranslationsCatalogProvider;
use PHPUnit\Framework\TestCase;

class CheckTranslationsCommandHandlerTest extends TestCase
{
private PullRequestId $prId;
private PullRequest $pr;
private InMemoryPullRequestRepository $prRepository;
private TranslationsCatalogProvider $catalogProvider;
private CheckTranslationsCommandHandler $checkTranslationsCommandHandler;

protected function setUp(): void
{
$this->prId = new PullRequestId('PrestaShop', 'PrestaShop', '30510');
$this->pr = PullRequest::create(id: $this->prId, labels: [], approvals: []);
$prDiffContent = file_get_contents(__DIR__.'/../../../fixtures/30510.diff');
$this->prRepository = $this->createMock(InMemoryPullRequestRepository::class);
$this->catalogProvider = $this->createMock(TranslationsCatalogProvider::class);
$this->checkTranslationsCommandHandler = new CheckTranslationsCommandHandler($this->prRepository, $this->catalogProvider);

$this->prRepository->method('find')->willReturn($this->pr);
$this->prRepository->method('getDiff')->willReturn(PullRequestDiff::parseDiff($this->prId, (string) $prDiffContent));
}

public function testHandleWithNewStrings(): void
{
// @phpstan-ignore-next-line
$this->catalogProvider->method('getTranslationsCatalog')->willReturn([]);

$this->checkTranslationsCommandHandler->__invoke(new CheckTranslationsCommand(
repositoryOwner: $this->prId->repositoryOwner,
repositoryName: $this->prId->repositoryName,
pullRequestNumber: $this->prId->pullRequestNumber,
));

/** @var PullRequest $pr */
$pr = $this->prRepository->find($this->prId);

$this->assertCount(
1,
array_filter(
$pr->getLabels(),
static fn (string $label) => 'Waiting for wording' === $label
)
);
}

public function testHandleWithoutNewStrings(): void
{
// @phpstan-ignore-next-line
$this->catalogProvider->method('getTranslationsCatalog')->willReturn([
'By deleting this image format, the theme will not be able to use it. This will result in a degraded experience on your front office.',
'Delete the images linked to this image setting',
'Are you sure you want to delete this image setting?',
'Cancel',
'Delete',
]);

$this->checkTranslationsCommandHandler->__invoke(new CheckTranslationsCommand(
repositoryOwner: $this->prId->repositoryOwner,
repositoryName: $this->prId->repositoryName,
pullRequestNumber: $this->prId->pullRequestNumber,
));

/** @var PullRequest $pr */
$pr = $this->prRepository->find($this->prId);

$this->assertCount(
0,
array_filter(
$pr->getLabels(),
static fn (string $label) => 'Waiting for wording' === $label
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace App\Tests\PullRequest\Domain\Aggregate\PullRequest;

use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestDiff;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class PullRequestDiffTest extends KernelTestCase
{
public function testParseDiff(): void
{
$diffContent = file_get_contents(__DIR__.'/../../../../fixtures/35380.diff');
$pr = new PullRequestId('prestashop', 'prestashop', '35380');
$prDiff = PullRequestDiff::parseDiff($pr, $diffContent); // @phpstan-ignore-line

$this->assertNotNull($prDiff);
$this->assertEquals(3, count($prDiff->getFiles()));

$files = [
'src/Adapter/Hosting/HostingInformation.php',
'src/Adapter/System/SystemInformation.php',
'src/PrestaShopBundle/Resources/views/Admin/Configure/AdvancedParameters/system_information.html.twig',
];

foreach ($prDiff->getFiles() as $key => $file) {
$this->assertEquals($files[$key], $file->getFileName());
}

$this->assertStringContainsString('\'hostname\' => $this->hostingInformation->getHostname(),',
$prDiff->getFiles()[1]->getHunks()[0]->getNew()
);
$this->assertStringNotContainsString('\'hostname\' => $this->hostingInformation->getHostname(),',
$prDiff->getFiles()[1]->getHunks()[0]->getOld()
);
}

public function testParseDiffWithEmptyDiff(): void
{
$pr = new PullRequestId('prestashop', 'prestashop', 'ko');
$prDiff = PullRequestDiff::parseDiff($pr, '');

$this->assertNotNull($prDiff);
$this->assertEquals(0, count($prDiff->getFiles()));
}

public function testParseDiffWithTranslations(): void
{
$diffContent = file_get_contents(__DIR__.'/../../../../fixtures/30510.diff');
$pr = new PullRequestId('prestashop', 'prestashop', '30510');
$prDiff = PullRequestDiff::parseDiff($pr, $diffContent); // @phpstan-ignore-line

$this->assertNotNull($prDiff);

$translations = $prDiff->getTranslations();

$needles = [
'Admin.Design.Notification' => [
'By deleting this image format, the theme will not be able to use it. This will result in a degraded experience on your front office.',
'Delete the images linked to this image setting',
],
'Admin.Design.Feature' => [
'Are you sure you want to delete this image setting?',
],
'Admin.Actions' => [
'Cancel',
'Delete',
],
];
$this->assertEquals($needles, $translations);
}
}
96 changes: 96 additions & 0 deletions tests/Shared/Infrastructure/Webhook/GithubWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Tests\Shared\Infrastructure\Webhook;

use App\PullRequest\Application\Command\AddLabelByApprovalCountCommand;
use App\PullRequest\Application\Command\CheckTranslationsCommand;
use App\PullRequest\Application\Command\RequestChangesCommand;
use App\PullRequestDashboard\Application\Command\MovePullRequestCardToColumnByApprovalCountCommand;
use App\PullRequestDashboard\Application\Command\MovePullRequestCardToColumnByLabelCommand;
Expand Down Expand Up @@ -214,6 +215,38 @@ public static function successfulExecutionProvider(): array
),
],
],
[
'pull_request',
'{
"action": "opened",
"pull_request": {
"base": {
"repo": {
"name": "PrestaShop",
"owner": {
"login": "PrestaShop"
}
}
},
"draft": false,
"number": 123
}
}',
[
new MovePullRequestCardToColumnByLabelCommand(
projectNumber: '17',
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123',
label: 'Ready for review',
),
new CheckTranslationsCommand(
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123'
),
],
],
[
'pull_request',
'{
Expand Down Expand Up @@ -260,6 +293,38 @@ public static function successfulExecutionProvider(): array
),
],
],
[
'pull_request',
'{
"action": "ready_for_review",
"pull_request": {
"base": {
"repo": {
"name": "PrestaShop",
"owner": {
"login": "PrestaShop"
}
}
},
"draft": false,
"number": 123
}
}',
[
new MovePullRequestCardToColumnByLabelCommand(
projectNumber: '17',
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123',
label: 'Ready for review',
),
new CheckTranslationsCommand(
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123'
),
],
],
[
'pull_request',
'{
Expand Down Expand Up @@ -329,6 +394,37 @@ public static function successfulExecutionProvider(): array
),
],
],
[
'pull_request',
'{
"action": "reopened",
"pull_request": {
"base": {
"repo": {
"name": "PrestaShop",
"owner": {
"login": "PrestaShop"
}
}
},
"number": 123
}
}',
[
new MovePullRequestCardToColumnByLabelCommand(
projectNumber: '17',
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123',
label: 'Reopened',
),
new CheckTranslationsCommand(
repositoryOwner: 'PrestaShop',
repositoryName: 'PrestaShop',
pullRequestNumber: '123'
),
],
],
[
'pull_request',
'{
Expand Down
Loading

0 comments on commit ae76bee

Please sign in to comment.