Skip to content
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
51 changes: 51 additions & 0 deletions tests/Feature/Actions/Extract/ExtractActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract;

use App\Actions\Extract\ExtractAction;
use App\Actions\Extract\HandlerInterface;
use App\Actions\Extract\Japan\Handler;
use App\Enums\SiteName;
use Psr\Log\NullLogger;
use Tests\Feature\TestCase;

final class ExtractActionTest extends TestCase
{
public function test_invokes_handlers_for_all_sites_when_null_provided(): void
{
$mock = \Mockery::mock(HandlerInterface::class);
$mock->shouldReceive('__invoke')->once();
$this->app->instance(Handler::class, $mock);

$portalHandler = \Mockery::mock(HandlerInterface::class);
$portalHandler->shouldReceive('__invoke')->once();
$this->app->instance(\App\Actions\Extract\Portal\Handler::class, $portalHandler);

$twitransHandler = \Mockery::mock(HandlerInterface::class);
$twitransHandler->shouldReceive('__invoke')->once();
$this->app->instance(\App\Actions\Extract\Twitrans\Handler::class, $twitransHandler);

$extractAction = app(ExtractAction::class);
$extractAction(null, new NullLogger);
}
Comment on lines +16 to +32
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

ハンドラーが呼び出されたことを検証するために、冗長な匿名クラスや手動の $state オブジェクトを使用する代わりに、Mockery を使用してハンドラークラスをモック化し、期待値(once())を設定することをお勧めします。これにより、テストがより簡潔で読みやすく、かつ慣用的(idiomatic)になります。

    public function test_invokes_handlers_for_all_sites_when_null_provided(): void
    {
        $japanHandler = \Mockery::mock(Handler::class);
        $japanHandler->shouldReceive('__invoke')->once();
        $this->app->instance(Handler::class, $japanHandler);

        $portalHandler = \Mockery::mock(\App\Actions\Extract\Portal\Handler::class);
        $portalHandler->shouldReceive('__invoke')->once();
        $this->app->instance(\App\Actions\Extract\Portal\Handler::class, $portalHandler);

        $twitransHandler = \Mockery::mock(\App\Actions\Extract\Twitrans\Handler::class);
        $twitransHandler->shouldReceive('__invoke')->once();
        $this->app->instance(\App\Actions\Extract\Twitrans\Handler::class, $twitransHandler);

        $action = app(ExtractAction::class);
        $action(null, new NullLogger);
    }


public function test_invokes_specific_handler_when_site_provided(): void
{
$mock = \Mockery::mock(HandlerInterface::class);
$mock->shouldReceive('__invoke')->once();
$this->app->instance(Handler::class, $mock);

$portalHandler = \Mockery::mock(HandlerInterface::class);
$portalHandler->shouldReceive('__invoke')->never();
$this->app->instance(\App\Actions\Extract\Portal\Handler::class, $portalHandler);

$twitransHandler = \Mockery::mock(HandlerInterface::class);
$twitransHandler->shouldReceive('__invoke')->never();
$this->app->instance(\App\Actions\Extract\Twitrans\Handler::class, $twitransHandler);

$extractAction = app(ExtractAction::class);
$extractAction(SiteName::Japan, new NullLogger);
}
Comment on lines +34 to +50
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

同様に、Mockery を使用して特定のハンドラーのみが呼び出され(once())、他のハンドラーは呼び出されないこと(never())を検証できます。これにより、テストコードが大幅にシンプルになります。

    public function test_invokes_specific_handler_when_site_provided(): void
    {
        $japanHandler = \Mockery::mock(Handler::class);
        $japanHandler->shouldReceive('__invoke')->once();
        $this->app->instance(Handler::class, $japanHandler);

        $portalHandler = \Mockery::mock(\App\Actions\Extract\Portal\Handler::class);
        $portalHandler->shouldReceive('__invoke')->never();
        $this->app->instance(\App\Actions\Extract\Portal\Handler::class, $portalHandler);

        $twitransHandler = \Mockery::mock(\App\Actions\Extract\Twitrans\Handler::class);
        $twitransHandler->shouldReceive('__invoke')->never();
        $this->app->instance(\App\Actions\Extract\Twitrans\Handler::class, $twitransHandler);

        $action = app(ExtractAction::class);
        $action(SiteName::Japan, new NullLogger);
    }

}
28 changes: 28 additions & 0 deletions tests/Feature/Actions/Extract/Japan/ExtractContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract\Japan;

use App\Actions\Extract\Japan\ExtractContents;
use App\Enums\PakSlug;
use App\Models\RawPage;
use Tests\Feature\TestCase;

final class ExtractContentsTest extends TestCase
{
public function test_extracts_title_text_and_pak(): void
{
$rawPage = RawPage::factory()->make([
'url' => 'https://japanese.simutrans.com/index.php?addon128%2fTest',
'html' => '<html><head><title>Awesome Addon - Simutrans日本語化・解説</title></head><body><div id="body">This is an awesome addon description.</div></body></html>',
]);

$extractContents = new ExtractContents;
$result = $extractContents($rawPage);

$this->assertSame('Awesome Addon', $result['title']);
$this->assertSame('This is an awesome addon description.', $result['text']);
$this->assertEquals([PakSlug::Pak128], $result['paks']);
}
}
26 changes: 26 additions & 0 deletions tests/Feature/Actions/Extract/Japan/ExtractLastModifiedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract\Japan;

use App\Actions\Extract\Japan\ExtractLastModified;
use App\Models\RawPage;
use Carbon\CarbonImmutable;
use Tests\Feature\TestCase;

final class ExtractLastModifiedTest extends TestCase
{
public function test_extracts_last_modified_date(): void
{
$rawPage = RawPage::factory()->make([
'html' => '<html><body><div id="lastmodified">Last-modified: 2023-05-15 14:30:00 (月)</div></body></html>',
]);

$extractLastModified = new ExtractLastModified;
$date = $extractLastModified($rawPage);

$this->assertInstanceOf(CarbonImmutable::class, $date);
$this->assertSame('2023-05-15 14:30:00', $date->format('Y-m-d H:i:s'));
}
}
78 changes: 78 additions & 0 deletions tests/Feature/Actions/Extract/Portal/ExtractContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract\Portal;

use App\Actions\Extract\Portal\ExtractContents;
use App\Actions\Extract\Portal\FindFileInfo;
use App\Enums\PakSlug;
use App\Enums\Portal\ArticlePostType;
use App\Models\Portal\Article;
use App\Models\Portal\Category;
use App\Models\Portal\FileInfo;
use App\Models\Portal\Tag;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\Feature\TestCase;

final class ExtractContentsTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

config(['database.connections.portal' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]]);
Schema::connection('portal')->create('file_infos', function (Blueprint $blueprint): void {
$blueprint->id();
$blueprint->integer('attachment_id');
$blueprint->text('data')->nullable();
$blueprint->timestamps();
});
}

public function test_extracts_title_text_and_pak(): void
{
$fileInfo = new FileInfo;
$fileInfo->attachment_id = 123;
$fileInfo->data = 'file content info';
$fileInfo->save();

$article = new Article([
'title' => 'Portal Addon Title',
'post_type' => ArticlePostType::AddonPost,
'contents' => [
'description' => 'A nice addon.',
'thanks' => 'Thanks to the author.',
'license' => 'MIT',
'file' => 123,
],
]);

$tag = new Tag(['name' => 'Train', 'description' => 'A train addon']);
$article->setRelation('tags', collect([$tag]));

$category = new Category(['slug' => '128-japan']);
$article->setRelation('categories', collect([$category]));

$extractContents = new ExtractContents(new FindFileInfo);
$result = $extractContents($article);

$this->assertSame('Portal Addon Title', $result['title']);
$this->assertStringContainsString('A nice addon.', $result['text']);
$this->assertStringContainsString('Thanks to the author.', $result['text']);
$this->assertStringContainsString('MIT', $result['text']);
$this->assertStringContainsString('Train', $result['text']);
$this->assertStringContainsString('A train addon', $result['text']);
$this->assertStringContainsString('file content info', $result['text']);

$this->assertEquals([PakSlug::Pak128Jp], $result['paks']);
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Actions/Extract/Twitrans/ExtractContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract\Twitrans;

use App\Actions\Extract\Twitrans\ExtractContents;
use App\Enums\PakSlug;
use App\Models\RawPage;
use Tests\Feature\TestCase;

final class ExtractContentsTest extends TestCase
{
public function test_extracts_title_text_and_pak(): void
{
$rawPage = RawPage::factory()->make([
'url' => 'https://wikiwiki.jp/twitrans/addon/pak128.japan/test',
'html' => '<html><head><title>Twitrans Addon - Simutrans的な実験室 Wiki*</title></head><body><div id="content">This is a twitrans addon description.</div></body></html>',
]);

$extractContents = new ExtractContents;
$result = $extractContents($rawPage);

$this->assertSame('Twitrans Addon', $result['title']);
$this->assertSame('This is a twitrans addon description.', $result['text']);
$this->assertEquals([PakSlug::Pak128Jp], $result['paks']);
}
}
26 changes: 26 additions & 0 deletions tests/Feature/Actions/Extract/Twitrans/ExtractLastModifiedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Extract\Twitrans;

use App\Actions\Extract\Twitrans\ExtractLastModified;
use App\Models\RawPage;
use Carbon\CarbonImmutable;
use Tests\Feature\TestCase;

final class ExtractLastModifiedTest extends TestCase
{
public function test_extracts_last_modified_date(): void
{
$rawPage = RawPage::factory()->make([
'html' => '<html><body><div id="lastmodified">Last-modified: 2023-11-20 10:00:00 (月)</div></body></html>',
]);

$extractLastModified = new ExtractLastModified;
$date = $extractLastModified($rawPage);

$this->assertInstanceOf(CarbonImmutable::class, $date);
$this->assertSame('2023-11-20 10:00:00', $date->format('Y-m-d H:i:s'));
}
}
Loading