Avoid destroying the test database in the middle of tests. All tests now pass!

This commit is contained in:
Peter Deltchev 2016-02-21 20:47:52 -08:00
parent b38026b218
commit 4f0aab6db9
7 changed files with 33 additions and 5 deletions

View file

@ -61,7 +61,7 @@ class DeleteGenre extends Job implements SelfHandling, ShouldQueue
*/ */
public function handle() public function handle()
{ {
DB::reconnect(); $this->beforeHandle();
// The user who kicked off this job is used when generating revision log entries. // The user who kicked off this job is used when generating revision log entries.
Auth::login($this->executingUser); Auth::login($this->executingUser);

View file

@ -85,7 +85,7 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue
*/ */
public function handle() public function handle()
{ {
DB::reconnect(); $this->beforeHandle();
// Sanity check: was this file just generated, or is it already being processed? // Sanity check: was this file just generated, or is it already being processed?
if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) { if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) {

View file

@ -20,6 +20,8 @@
namespace Poniverse\Ponyfm\Jobs; namespace Poniverse\Ponyfm\Jobs;
use App;
use DB;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
abstract class Job abstract class Job
@ -36,4 +38,15 @@ abstract class Job
*/ */
use Queueable; 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();
}
}
} }

View file

@ -52,7 +52,7 @@ class UpdateSearchIndexForEntity extends Job implements SelfHandling, ShouldQueu
*/ */
public function handle() public function handle()
{ {
DB::reconnect(); $this->beforeHandle();
$this->entity->updateElasticsearchEntrySynchronously(); $this->entity->updateElasticsearchEntrySynchronously();
} }
} }

View file

@ -67,7 +67,7 @@ class UpdateTagsForRenamedGenre extends Job implements SelfHandling, ShouldQueue
*/ */
public function handle() public function handle()
{ {
DB::reconnect(); $this->beforeHandle();
// The user who kicked off this job is used when generating revision log entries. // The user who kicked off this job is used when generating revision log entries.
Auth::login($this->executingUser); Auth::login($this->executingUser);

View file

@ -29,6 +29,12 @@ class SetupElasticsearch extends Migration
*/ */
public function up() 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 = Elasticsearch::connection();
$elasticsearch->indices()->create([ $elasticsearch->indices()->create([
@ -108,6 +114,12 @@ class SetupElasticsearch extends Migration
*/ */
public function down() 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 = Elasticsearch::connection();
$elasticsearch->indices()->delete(['index' => 'ponyfm']); $elasticsearch->indices()->delete(['index' => 'ponyfm']);

View file

@ -132,7 +132,10 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
* @param array $files * @param array $files
*/ */
protected function callUploadWithParameters(array $parameters, 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(); $this->user = factory(User::class)->create();
$file = $this->getTestFileForUpload('ponyfm-test.flac'); $file = $this->getTestFileForUpload('ponyfm-test.flac');