I am trying to run a group of unit tests after reading an example here which require an active database connection. It seems to work for one unit test, but it will fail with multiple tests due to redefinition of a constant contained in a Laravel Command class. Here are the errors I receive after running PHP Unit:
PHPUnit 3.7.31 by Sebastian Bergmann.
FEE
Time: 203 ms, Memory: 5.00Mb
There were 2 errors:
1) MessageControllerTest::testSixMonths
ErrorException: Constant SHARE_SEND_EXPIRATION already defined
src/app/commands/DeleteExpiredSends.php:31
src/app/start/artisan.php:14
src/vendor/laravel/framework/src/Illuminate/Console/start.php:57
src/vendor/laravel/framework/src/Illuminate/Console/Application.php:30
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:70
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:45
src/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:206
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:21
2) MessageControllerTest::testOneYear
ErrorException: Constant SHARE_SEND_EXPIRATION already defined
src/app/commands/DeleteExpiredSends.php:31
src/app/start/artisan.php:14
src/vendor/laravel/framework/src/Illuminate/Console/start.php:57
src/vendor/laravel/framework/src/Illuminate/Console/Application.php:30
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:70
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:45
src/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:206
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:21
FAILURES!
Tests: 3, Assertions: 1, Errors: 2.
And here is my unit test:
<?php
class MessageControllerTest extends TestCase
{
/**
* setUp() : set up the unit tests by initializing the database
*/
public function setUp()
{
// call the parent's setup method
parent::setUp();
// init the DB
$this->migrateDatabase();
}
/**
* createApplication() : bootstrap the app in order to execute
* tests
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
// set the unitTesting flag to true
$unitTesting = true;
// set the environment variable to 'testing'
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
/**
* migrateDatabase() : load the sqlite database into memory
*/
private function migrateDatabase()
{
Artisan::call('migrate');
}
/**
* testHundredFilesMessage() : tests to see if custom message will
* return after 100 messages have been sent by the same user
*/
public function testHundredFilesMessage()
{
// TODO
$this->assertTrue(false);
}
/**
* testSixMonthMessage() : test the six months message
*/
public function testSixMonthMessage()
{
// TODO
$this->assertTrue(false);
}
/**
* testOneYearMessage() : test the one-year message
*/
public function testOneYearMessage()
{
// TODO
$this->assertTrue(false);
}
}
The first test seems to pass, but all subsequent tests throw errors. I am guessing that somehow the createApplication() method gets called several times which might explain why the Command class DeleteExpiredSends gets redefined; the class shows a define statement in its constructor:
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
define("SHARE_SEND_EXPIRATION", 14);
}
If anyone could tell me how to avoid redefinition for this unit test, so as to avoid modifying the internals of the Command class, it would be most helpful. It only seems to be an issue where this unit test is concerned.
definewas used, that code was written by another developer and I am not entirely sure about the specifics of that particular API. I amended my question above.defines elsewhere in the application, I will run into the same problem even after eliminating thedefinein question.