From 898fb4f80039bf1ec920f931d4e8c00162bf4ef8 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 23 Jul 2024 18:51:22 +0200 Subject: Refactor HelpTest.php. Move default stubs to a function to make them reusable. --- tests/unit/Module/HelpTest.php | 52 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'tests') diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index 353a40def..64e4a2663 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -31,30 +31,7 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { * ["html"] */ public function test_get_request_when_help_file_exists(string $ext): void { - // Stub file exists, to only retur true for the file with the current - // extension - $fe_stub = $this->getFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); - $fe_stub - ->expects($this->any()) - ->willReturnCallback( - fn (string $path) => $path === "doc/en/about/help_topic.{$ext}" - ); - - // Use a value map to make the `file_get_contents` stub return the - // correct content for the file types. - $file_content_map = [ - [ 'doc/en/about/help_topic.md', "### Help heading\n\$Projectname help content" ], - [ 'doc/en/about/help_topic.bb', "[h3]Help heading[/h3]\n\n\$Projectname help content" ], - [ 'doc/en/about/help_topic.html', "

Help heading

\$Projectname help content

" ], - ]; - - // Stub `file_get_contents` to plant our own content. - $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); - $fgc_stub - ->expects($this->once()) - ->willReturnMap($file_content_map); - - + $stubs = $this->prepare_stubs($ext); $this->get("help/about/help_topic"); // Check that markdown content was correctly rendered @@ -181,4 +158,31 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->assertPageContains('

This is the included file.

'); } + + private function prepare_stubs(string $ext): array { + // Stub file exists, to only retur true for the file with the current + // extension + $fe_stub = $this->getFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); + $fe_stub + ->expects($this->any()) + ->willReturnCallback( + fn (string $path) => $path === "doc/en/about/help_topic.{$ext}" + ); + + // Use a value map to make the `file_get_contents` stub return the + // correct content for the file types. + $file_content_map = [ + [ 'doc/en/about/help_topic.md', "### Help heading\n\$Projectname help content" ], + [ 'doc/en/about/help_topic.bb', "[h3]Help heading[/h3]\n\n\$Projectname help content" ], + [ 'doc/en/about/help_topic.html', "

Help heading

\$Projectname help content

" ], + ]; + + // Stub `file_get_contents` to plant our own content. + $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); + $fgc_stub + ->expects($this->once()) + ->willReturnMap($file_content_map); + + return [ 'file_exists' => $fe_stub, 'file_get_contents' => $fgc_stub ]; + } } -- cgit v1.2.3