From 804ddb76d4a0349fd2168894286ea575246279df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Sun, 19 Mar 2023 13:21:37 +0100 Subject: [PATCH] feat(tests): add them first step --- App.php | 5 +++-- SeacmsAppPlugin.php | 37 +++++++++++++++++++++++++++++++++++++ src/TestException.php | 14 ++++++++++++++ src/TestInterface.php | 22 ++++++++++++++++++++++ tests/AppTest.php | 25 +++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/TestException.php create mode 100644 src/TestInterface.php create mode 100644 tests/AppTest.php diff --git a/App.php b/App.php index 824e541..f6dedbe 100644 --- a/App.php +++ b/App.php @@ -11,6 +11,7 @@ use Exception; use Pico; use SeacmsAppPlugin; use SeaCMS\Api\SpecialOutputException; +use SeaCMS\App\TestInterface; use Throwable; set_error_handler(function ( @@ -51,7 +52,7 @@ class App */ protected $pico; - public function __construct(string $contentFolderFromRoot) + public function __construct(string $contentFolderFromRoot, ?TestInterface $testRunner = null) { // sanitize content folder $cwd = getcwd(); @@ -76,7 +77,7 @@ class App self::PLUGINS_PATH, // plugins dir self::THEMES_PATH // themes dir ); - $this->pico->loadPlugin(new SeacmsAppPlugin($this->pico)); + $this->pico->loadPlugin(new SeacmsAppPlugin($this->pico, $testRunner)); $this->update_SERVERIfNeeded($this->pico, $contentFolderFromRoot); } diff --git a/SeacmsAppPlugin.php b/SeacmsAppPlugin.php index 73db11f..fb8b36d 100644 --- a/SeacmsAppPlugin.php +++ b/SeacmsAppPlugin.php @@ -5,6 +5,7 @@ use SeaCMS\Api\LateApiAware; use SeaCMS\Api\JsonResponse; use SeaCMS\App\MdResponse; +use SeaCMS\App\TestInterface; /** * A plugin for SeaCMS-app. @@ -17,6 +18,30 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware */ const API_VERSION = 4; + /** + * define if test output should be defined + * @var bool + */ + protected $triggerTest; + + /** + * define test + * @var TestInterface + */ + protected $testRunner; + + /** + * construct + * @param Pico $pico current instance of Pico + * @param ?TestInterface $testRunner optional + */ + public function __construct(Pico $pico, $testRunner = null) + { + parent::__construct($pico); + $this->triggerTest = !empty($testRunner) && ($testRunner instanceof TestInterface); + $this->testRunner = ($this->triggerTest) ? $testRunner : null; + } + /** * return api routes * @return array @@ -136,4 +161,16 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware return $this->apiPageAsMd("$folder1/$folder2/$folder3/$pageName"); } + /** + * Triggered after Pico has rendered the page + * + * @param string &$output contents which will be sent to the user + */ + public function onPageRendered(&$output) + { + if ($this->triggerTest){ + $this->testRunner->run($this,$output); + } + } + } \ No newline at end of file diff --git a/src/TestException.php b/src/TestException.php new file mode 100644 index 0000000..1345f48 --- /dev/null +++ b/src/TestException.php @@ -0,0 +1,14 @@ +runPico(); + $this->assertTrue($app instanceof App); + } + public function testFailure(): void + { + $this->assertTrue(false); + } +} \ No newline at end of file