-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Add Tests for Extract Action #178
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0ee1f4f
Feature: Add Tests for Extract Action
128na 2c0b491
Fix Portal ExtractContentsTest to use SQLite for CI as FindFileInfo m…
128na 887b79f
[rector] Rector fixes
actions-user 8473306
Fix NOT NULL constraint violation in ExtractContentsTest by assigning…
128na c53acfe
[rector] Rector fixes
actions-user 2d40ab7
[rector] Rector fixes
actions-user fc78c07
Refactor ExtractActionTest to use Mockery
128na 82bd969
[rector] Rector fixes
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同様に、Mockery を使用して特定のハンドラーのみが呼び出され( 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
28
tests/Feature/Actions/Extract/Japan/ExtractContentsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
26
tests/Feature/Actions/Extract/Japan/ExtractLastModifiedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
78
tests/Feature/Actions/Extract/Portal/ExtractContentsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
28
tests/Feature/Actions/Extract/Twitrans/ExtractContentsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
26
tests/Feature/Actions/Extract/Twitrans/ExtractLastModifiedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ハンドラーが呼び出されたことを検証するために、冗長な匿名クラスや手動の
$stateオブジェクトを使用する代わりに、Mockery を使用してハンドラークラスをモック化し、期待値(once())を設定することをお勧めします。これにより、テストがより簡潔で読みやすく、かつ慣用的(idiomatic)になります。