From 4f0aab6db93f60c82c1ebc3b3a391fec9d40d077 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Sun, 21 Feb 2016 20:47:52 -0800 Subject: [PATCH] Avoid destroying the test database in the middle of tests. All tests now pass! --- app/Jobs/DeleteGenre.php | 2 +- app/Jobs/EncodeTrackFile.php | 2 +- app/Jobs/Job.php | 13 +++++++++++++ app/Jobs/UpdateSearchIndexForEntity.php | 2 +- app/Jobs/UpdateTagsForRenamedGenre.php | 2 +- .../2016_01_14_021607_setup_elasticsearch.php | 12 ++++++++++++ tests/TestCase.php | 5 ++++- 7 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/Jobs/DeleteGenre.php b/app/Jobs/DeleteGenre.php index e7bbc21f..4c633ec2 100644 --- a/app/Jobs/DeleteGenre.php +++ b/app/Jobs/DeleteGenre.php @@ -61,7 +61,7 @@ class DeleteGenre extends Job implements SelfHandling, ShouldQueue */ public function handle() { - DB::reconnect(); + $this->beforeHandle(); // The user who kicked off this job is used when generating revision log entries. Auth::login($this->executingUser); diff --git a/app/Jobs/EncodeTrackFile.php b/app/Jobs/EncodeTrackFile.php index 75a1972f..9bdfb564 100644 --- a/app/Jobs/EncodeTrackFile.php +++ b/app/Jobs/EncodeTrackFile.php @@ -85,7 +85,7 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue */ public function handle() { - DB::reconnect(); + $this->beforeHandle(); // Sanity check: was this file just generated, or is it already being processed? if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) { diff --git a/app/Jobs/Job.php b/app/Jobs/Job.php index b9a37582..4b215d15 100644 --- a/app/Jobs/Job.php +++ b/app/Jobs/Job.php @@ -20,6 +20,8 @@ namespace Poniverse\Ponyfm\Jobs; +use App; +use DB; use Illuminate\Bus\Queueable; abstract class Job @@ -36,4 +38,15 @@ abstract class Job */ use Queueable; + + /** + * This method should be called at the beginning of every job's handle() + * method. It ensures that we don't lose the in-memory database during + * testing by disconnecting from it - which causes tests to fail. + */ + protected function beforeHandle() { + if (App::environment() !== 'testing') { + DB::reconnect(); + } + } } diff --git a/app/Jobs/UpdateSearchIndexForEntity.php b/app/Jobs/UpdateSearchIndexForEntity.php index 177cb1cf..1e2ca678 100644 --- a/app/Jobs/UpdateSearchIndexForEntity.php +++ b/app/Jobs/UpdateSearchIndexForEntity.php @@ -52,7 +52,7 @@ class UpdateSearchIndexForEntity extends Job implements SelfHandling, ShouldQueu */ public function handle() { - DB::reconnect(); + $this->beforeHandle(); $this->entity->updateElasticsearchEntrySynchronously(); } } diff --git a/app/Jobs/UpdateTagsForRenamedGenre.php b/app/Jobs/UpdateTagsForRenamedGenre.php index 63484b79..ab948e1f 100644 --- a/app/Jobs/UpdateTagsForRenamedGenre.php +++ b/app/Jobs/UpdateTagsForRenamedGenre.php @@ -67,7 +67,7 @@ class UpdateTagsForRenamedGenre extends Job implements SelfHandling, ShouldQueue */ public function handle() { - DB::reconnect(); + $this->beforeHandle(); // The user who kicked off this job is used when generating revision log entries. Auth::login($this->executingUser); diff --git a/database/migrations/2016_01_14_021607_setup_elasticsearch.php b/database/migrations/2016_01_14_021607_setup_elasticsearch.php index b4980838..8b0eac0a 100644 --- a/database/migrations/2016_01_14_021607_setup_elasticsearch.php +++ b/database/migrations/2016_01_14_021607_setup_elasticsearch.php @@ -29,6 +29,12 @@ class SetupElasticsearch extends Migration */ public function up() { + // As of 2016-02-21, Elasticsearch is not used in tests. + // Letting this migration run will blow away all data in it. + if (App::environment() === 'testing') { + return; + } + $elasticsearch = Elasticsearch::connection(); $elasticsearch->indices()->create([ @@ -108,6 +114,12 @@ class SetupElasticsearch extends Migration */ public function down() { + // As of 2016-02-21, Elasticsearch is not used in tests. + // Letting this migration run will blow away all data in it. + if (App::environment() === 'testing') { + return; + } + $elasticsearch = Elasticsearch::connection(); $elasticsearch->indices()->delete(['index' => 'ponyfm']); diff --git a/tests/TestCase.php b/tests/TestCase.php index d9dbd870..aa85c0a2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -132,7 +132,10 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase * @param array $files */ protected function callUploadWithParameters(array $parameters, array $files = []) { - $this->expectsJobs(Poniverse\Ponyfm\Jobs\EncodeTrackFile::class); + $this->expectsJobs([ + Poniverse\Ponyfm\Jobs\EncodeTrackFile::class, + Poniverse\Ponyfm\Jobs\UpdateSearchIndexForEntity::class + ]); $this->user = factory(User::class)->create(); $file = $this->getTestFileForUpload('ponyfm-test.flac');