Skip to content

Commit

Permalink
Added test for events (#56)
Browse files Browse the repository at this point in the history
* Emitted correct event

* added tests to verify events emitted

* use ::class instead of strings

* apply style fixes

* more style fixes

* added new line
  • Loading branch information
vistik authored and tomschlick committed Sep 11, 2018
1 parent 5dd3d5b commit 75535df
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Symfony\Component\HttpFoundation\Response;
use TomSchlick\RequestMigrations\Events\RequestHasMigrated;
use TomSchlick\RequestMigrations\Events\RequestIsMigrating;
use TomSchlick\RequestMigrations\Events\ResponseHasMigrated;
use TomSchlick\RequestMigrations\Events\ResponseIsMigrating;
Expand Down Expand Up @@ -121,7 +122,7 @@ public function processRequestMigrations() : Request

$this->request = $class->migrateRequest($originalRequest);

Event::fire(new RequestIsMigrating($class, $originalRequest, $this->request));
Event::fire(new RequestHasMigrated($class, $originalRequest, $this->request));
});

return $this->request;
Expand Down
77 changes: 77 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace TomSchlick\RequestMigrations\Tests;

use Illuminate\Support\Facades\Event;
use TomSchlick\RequestMigrations\Events\RequestHasMigrated;
use TomSchlick\RequestMigrations\Events\RequestIsMigrating;
use TomSchlick\RequestMigrations\Tests\Migrations\PostBodyMigration;
use TomSchlick\RequestMigrations\Tests\Migrations\GroupNameMigration;
use TomSchlick\RequestMigrations\Tests\Migrations\PostTitleMigration;

class EventTest extends TestCase
{
/** @test */
public function event_are_emitted_on_migration()
{
// Given
Event::fake();

$response = $this->post(
'posts',
[
'headline' => 'This is going to be an awesome article!',
'content' => 'Awesome content! I told you!',
],
[
'x-api-request-version' => '2017-01-01',
'x-api-response-version' => '2017-01-01',
]
);

$expectedEvents = [
PostBodyMigration::class,
GroupNameMigration::class,
PostTitleMigration::class,
];

Event::assertDispatched(RequestIsMigrating::class, function (RequestIsMigrating $event) use (&$expectedEvents) {
$migration = get_class($event->migration);

// Remove the emitted event from expected array
if (($key = array_search($migration, $expectedEvents)) !== false) {
unset($expectedEvents[$key]);
}

return true;
});

$this->assertCount(0, $expectedEvents);

$expectedEvents = [
PostBodyMigration::class,
GroupNameMigration::class,
PostTitleMigration::class,
];

Event::assertDispatched(RequestHasMigrated::class, function (RequestHasMigrated $event) use (&$expectedEvents) {
$migration = get_class($event->migration);

// Remove the emitted event from expected array
if (($key = array_search($migration, $expectedEvents)) !== false) {
unset($expectedEvents[$key]);
}

return true;
});

$this->assertCount(0, $expectedEvents);

$response->assertJson([
'request' => [
'title' => 'This is going to be an awesome article!',
'body' => 'Awesome content! I told you!',
],
]);
}
}

0 comments on commit 75535df

Please sign in to comment.