From 1f89a43499aa6209399468407f023ad4350a5fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Mon, 20 Mar 2023 17:11:41 +0100 Subject: [PATCH] feat(AppTest): next test to usr phpunit --- src/TestBaseUrl.php | 35 +++++++++++ src/TestBaseUrlException.php | 109 +++++++++++++++++++++++++++++++++++ tests/AppTest.php | 78 ++++++++++++++++++++----- 3 files changed, 209 insertions(+), 13 deletions(-) create mode 100644 src/TestBaseUrl.php create mode 100644 src/TestBaseUrlException.php diff --git a/src/TestBaseUrl.php b/src/TestBaseUrl.php new file mode 100644 index 0000000..331f967 --- /dev/null +++ b/src/TestBaseUrl.php @@ -0,0 +1,35 @@ +getPico(); + throw new TestBaseUrlException( + $pico->getBaseUrl(), + $pico->getRootDir(), + $pico->getPluginsDir(), + $pico->getThemesDir(), + $pico->getCurrentPage(), + "Testing base url" + ); + + } +} diff --git a/src/TestBaseUrlException.php b/src/TestBaseUrlException.php new file mode 100644 index 0000000..e58c1aa --- /dev/null +++ b/src/TestBaseUrlException.php @@ -0,0 +1,109 @@ +baseUrl = $baseUrl; + $this->rootDir = $rootDir; + $this->pluginDir = $pluginDir; + $this->themeDir = $themeDir; + $this->currentPage = $currentPage; + } + + /** + * get baseUrl + * @return string + */ + public function getBaseUrl(): string + { + return $this->baseUrl; + } + + /** + * get pluginDir + * @return string + */ + public function getPluginDir(): string + { + return $this->pluginDir; + } + + /** + * get rootDir + * @return string + */ + public function getRootDir(): string + { + return $this->rootDir; + } + + /** + * get themeDir + * @return string + */ + public function getThemeDir(): string + { + return $this->themeDir; + } + + /** + * get currentPage + * @return null|array + */ + public function getcurrentPage(): ?array + { + return $this->currentPage; + } + +} diff --git a/tests/AppTest.php b/tests/AppTest.php index 016a5e3..61d7091 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -7,29 +7,81 @@ namespace SeaCMS\App\Test; -use SeaCMS\App; use PHPUnit\Framework\TestCase; +use SeaCMS\App; +use SeaCMS\App\TestBaseUrl; +use SeaCMS\App\TestBaseUrlException; +use Throwable; final class AppTest extends TestCase { - public function testSuccess(): void + /** + * @dataProvider apiRewriteProvider + * @covers App::update_SERVERIfNeeded + * @param string $rootFolder, + * @param string $filePath, + * @param string $shortScriptName, + * @param string $queryString + */ + public function testApiRewrite( + string $rootFolder, + string $filePath, + string $shortScriptName, + string $queryString, + string $waitedBaseUrl, + ): void { + $this->saveSERVER(); $this->defineServer( true, - 'index.php', - '/', - '' + $filePath, + $shortScriptName, + $queryString ); - $app = new App(''); - $output = $app->runPico(); - $this->assertTrue($app instanceof App); - } - public function testFailure(): void - { - $this->assertTrue(false); + $app = new App($rootFolder,new TestBaseUrl()); + $thrown = false; + $foundTh = null; + try { + $output = $app->runPico(); + } catch (TestBaseUrlException $th) { + $thrown = true; + $foundTh = $th; + } catch (Throwable $th){ + + } + $this->revertSERVER(); + $this->assertTrue($thrown,"TestBaseUrlException not found"); + $this->assertEquals($waitedBaseUrl,$foundTh->getBaseUrl(),"Not same baseUrl"); } - /** + public function apiRewriteProvider() + { + $data = []; + $this->prepareData($data,'content/*','content','index.php','/','','http://localhost/'); + $this->prepareData($data,'content/index.php*','content','index.php','/index.php','','http://localhost/'); + return $data; + } + + public function prepareData( + array &$data, + string $name, + string $rootFolder, + string $filePath, + string $shortScriptName, + string $queryString, + string $waitedBaseUrl, + ) + { + $data[$name] = [ + 'rootFolder' => $rootFolder, + 'filePath' => $filePath, + 'shortScriptName' => $shortScriptName, + 'queryString' => $queryString, + 'waitedBaseUrl' => $waitedBaseUrl, + ]; + } + + /** * define $_SERVER because fastcgi not run in CLI * @param bool $reset reset previous $_SERVER * @param string $filePath realpath of current script file