From 0bab308fbb8a1887aa6f7469993ff84c93cf2177 Mon Sep 17 00:00:00 2001 From: Orhan Mekic Date: Mon, 27 Jul 2015 20:21:55 +0200 Subject: [PATCH] Use the app already created by Codeception module and add support for mocking events (also mocking jobs and facades should also work now, but I have not tested this) --- .../Testing/CodeceptionLaravelUnitTest.php | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/src/STS/Testing/CodeceptionLaravelUnitTest.php b/src/STS/Testing/CodeceptionLaravelUnitTest.php index 0488af3..d21f503 100644 --- a/src/STS/Testing/CodeceptionLaravelUnitTest.php +++ b/src/STS/Testing/CodeceptionLaravelUnitTest.php @@ -1,16 +1,21 @@ findBasePath(); - $app = require $basePath . '/bootstrap/app.php'; - $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); - - return $app; + //application is already created by the Codeception module and that's the one we will use + //so this is not needed in our case } /** - * Setup the test environment. + * Setup the test environment. We weed to use the app already initiated by + * the Codeception module * * @return void */ public function setUp() { - if ( ! $this->app) - { - $this->refreshApplication(); - } + //Codecetion Module will initiate the app + parent::setUp(); - return parent::setUp(); + //now lets grab the app from Codeception Module + $laravelCodeceptionModule = $this->getModule('Laravel5'); + $this->app = $laravelCodeceptionModule->getApplication(); } /** - * Clean up the testing environment before the next test. + * Run all callbacks and clean up the testing environment before the next test. * * @return void */ public function tearDown() { - if ($this->app) - { - $this->app->flush(); + if ($this->app) { + foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { + call_user_func($callback); + } } + // the app is coming from Codeception module, Codeception Module will also flush down the application + parent::tearDown(); + } - return parent::tearDown(); + /** + * Add a callback to execute just before we flush the application + * + * @return void + */ + protected function beforeApplicationDestroyed(callable $callback) + { + $this->beforeApplicationDestroyedCallbacks[] = $callback; } /** @@ -67,18 +81,22 @@ protected function findBasePath() $reflector = new \ReflectionClass(get_called_class()); $currentPath = $reflector->getFileName(); $basePath = substr($currentPath,0, strpos($currentPath, "/tests/")); - if(file_exists($basePath . '/bootstrap/app.php')) { return $basePath; } - // We couldn't figure it out automatically, let's look for help if(defined('LARAVEL_BASE') && file_exists(LARAVEL_BASE . '/bootstrap/app.php')) { return LARAVEL_BASE; } + //If all fails Codeception can give us the app root directory + $rootDir = \Codeception\Configuration::projectDir(); + if(file_exists($rootDir . '/bootstrap/app.php')) { + return $rootDir; + } + // We need to full out exit here, not just throw an exception print("Unable to determine your base Laravel path, and didn't find a valid LARAVEL_BASE defined. Please define that in your _bootstrap.php file."); exit(1); } -} +} \ No newline at end of file