From 126fdee80e36eb8a2df22a0de831bc1aeaeaf4d5 Mon Sep 17 00:00:00 2001 From: Vassyli Date: Wed, 30 Dec 2020 14:28:53 +0100 Subject: [PATCH] Adds additional methods to ModelTestCase for modules to refine their tests. --- tests/CoreModelTestCase.php | 2 +- tests/ModelTestCase.php | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/CoreModelTestCase.php b/tests/CoreModelTestCase.php index b95bd17..447c34f 100644 --- a/tests/CoreModelTestCase.php +++ b/tests/CoreModelTestCase.php @@ -11,7 +11,7 @@ class CoreModelTestCase extends ModelTestCase * Returns a .yml dataset under this name * @return array */ - protected function getDataSet(): array + public function getDataSet(): array { $datasetFile = implode(DIRECTORY_SEPARATOR, [__DIR__, 'datasets', $this->dataset . '.yml']); $dataset = Yaml::parseFile($datasetFile); diff --git a/tests/ModelTestCase.php b/tests/ModelTestCase.php index c620f02..a373e33 100644 --- a/tests/ModelTestCase.php +++ b/tests/ModelTestCase.php @@ -19,6 +19,7 @@ use LotGD\Core\LibraryConfigurationManager; use LotGD\Core\Exceptions\InvalidConfigurationException; use LotGD\Core\ModelExtender; use Monolog\Handler\NullHandler; +use Monolog\Handler\StreamHandler; use Monolog\Logger; use PDO; use PHPUnit\Framework\TestCase; @@ -104,6 +105,21 @@ abstract class ModelTestCase extends TestCase return self::$em; } + public function getCwd(): string + { + return implode(DIRECTORY_SEPARATOR, [__DIR__, '..']); + } + + public function getDataSet(): ?array + { + return null; + } + + public function useSilentHandler(): bool + { + return true; + } + protected function setUp(): void { $this->getConnection(); @@ -111,7 +127,9 @@ abstract class ModelTestCase extends TestCase // Set up database content if (method_exists($this, "getDataSet")) { $dataSet = $this->getDataSet(); - $this->insertData($dataSet); + if (!empty($dataSet)) { + $this->insertData($dataSet); + } } parent::setUp(); @@ -122,14 +140,19 @@ abstract class ModelTestCase extends TestCase // Make an empty logger for these tests. Feel free to change this // to place log messages somewhere you can easily find them. $logger = new Logger('test'); - $logger->pushHandler(new NullHandler()); + + if ($this->useSilentHandler()) { + $logger->pushHandler(new NullHandler()); + } else { + $logger->pushHandler(new StreamHandler("php://stdout.")); + } // Create a Game object for use in these tests. $this->g = (new GameBuilder()) ->withConfiguration(new Configuration(getenv('LOTGD_TESTS_CONFIG_PATH'))) ->withLogger($logger) ->withEntityManager($this->getEntityManager()) - ->withCwd(implode(DIRECTORY_SEPARATOR, [__DIR__, '..'])) + ->withCwd($this->getCwd()) ->create(); // Add Event listener to entity manager