Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event after authors sync #10

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Events/Webinar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace EscolaLms\Webinar\Events;

use EscolaLms\Core\Models\User;
use EscolaLms\Webinar\Models\Webinar as WebinarModel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

abstract class Webinar
{
use Dispatchable, SerializesModels;

private User $user;
private WebinarModel $webinar;

public function __construct(User $user, WebinarModel $webinar)
{
$this->user = $user;
$this->webinar = $webinar;
}

public function getUser(): User
{
return $this->user;
}

public function getWebinar(): WebinarModel
{
return $this->webinar;
}
}
7 changes: 7 additions & 0 deletions src/Events/WebinarAuthorAssigned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EscolaLms\Webinar\Events;

class WebinarAuthorAssigned extends Webinar
{
}
7 changes: 7 additions & 0 deletions src/Events/WebinarAuthorUnassigned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EscolaLms\Webinar\Events;

class WebinarAuthorUnassigned extends Webinar
{
}
27 changes: 26 additions & 1 deletion src/Strategies/Relations/WebinarWithAuthorsStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace EscolaLms\Webinar\Strategies\Relations;

use EscolaLms\Core\Models\User;
use EscolaLms\Webinar\Events\WebinarAuthorAssigned;
use EscolaLms\Webinar\Events\WebinarAuthorUnassigned;
use EscolaLms\Webinar\Strategies\Contracts\RelationStrategyContract;
use EscolaLms\Webinar\Models\Webinar;

Expand All @@ -17,6 +20,28 @@ public function __construct(array $params) {

public function setRelation(): void
{
$this->webinar->authors()->sync($this->data['authors']);
$changes = $this->webinar->authors()->sync($this->data['authors']);
$this->dispatchEventForAuthorsAttachedToWebinar($changes['attached']);
$this->dispatchEventForAuthorsDetachedFromWebinar($changes['detached']);
}

private function dispatchEventForAuthorsAttachedToWebinar(array $users = []): void
{
foreach ($users as $attached) {
$user = is_int($attached) ? User::find($attached) : $attached;
if ($user) {
event(new WebinarAuthorAssigned($user, $this->webinar));
}
}
}

private function dispatchEventForAuthorsDetachedFromWebinar(array $users = []): void
{
foreach ($users as $detached) {
$user = is_int($detached) ? User::find($detached) : $detached;
if ($user) {
event(new WebinarAuthorUnassigned($user, $this->webinar));
}
}
}
}
8 changes: 8 additions & 0 deletions tests/APIs/WebinarStoreApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace EscolaLms\Consultations\Tests\APIs;

use EscolaLms\Tags\Models\Tag;
use EscolaLms\Webinar\Events\WebinarAuthorAssigned;
use EscolaLms\Webinar\Events\WebinarAuthorUnassigned;
use EscolaLms\Webinar\Tests\TestCase;
use EscolaLms\Webinar\Database\Seeders\WebinarsPermissionSeeder;
use EscolaLms\Webinar\Models\Webinar;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Testing\Fluent\AssertableJson;

class WebinarStoreApiTest extends TestCase
Expand All @@ -34,6 +37,8 @@ public function testWebinarStoreUnauthorized(): void

public function testWebinarStore(): void
{
Event::fake([WebinarAuthorAssigned::class, WebinarAuthorUnassigned::class]);

$webinar = Webinar::factory()->make()->toArray();
$authors = config('auth.providers.users.model')::factory(2)->create()->pluck('id')->toArray();
$tags = ['Event', 'Webinar'];
Expand Down Expand Up @@ -78,6 +83,9 @@ public function testWebinarStore(): void
)
->etc()
);

Event::assertDispatchedTimes(WebinarAuthorAssigned::class, 2);
Event::assertDispatchedTimes(WebinarAuthorUnassigned::class, 0);
}

public function testWebinarStoreRequiredValidation(): void
Expand Down
31 changes: 31 additions & 0 deletions tests/APIs/WebinarUpdateApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace EscolaLms\Webinar\Tests\APIs;

use EscolaLms\Webinar\Events\WebinarAuthorAssigned;
use EscolaLms\Webinar\Events\WebinarAuthorUnassigned;
use EscolaLms\Webinar\Tests\TestCase;
use EscolaLms\Webinar\Database\Seeders\WebinarsPermissionSeeder;
use EscolaLms\Webinar\Models\Webinar;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Testing\Fluent\AssertableJson;

class WebinarUpdateApiTest extends TestCase
Expand Down Expand Up @@ -83,6 +86,34 @@ public function testWebinarUpdate(): void
);
}

public function testWebinarUpdateAuthors(): void
{
Event::fake([WebinarAuthorAssigned::class, WebinarAuthorUnassigned::class]);

$author1 = config('auth.providers.users.model')::factory()->create();
$author2 = config('auth.providers.users.model')::factory()->create();

$this->webinar->authors()->sync($author1->getKey());

$response = $this->actingAs($this->user, 'api')->json(
'POST',
$this->apiUrl,
['authors' => [$author2->getKey()]]
);

$response->assertOk();

Event::assertDispatched(WebinarAuthorAssigned::class, function (WebinarAuthorAssigned $event) use ($author2){
$this->assertEquals($author2->getKey(), $event->getUser()->getKey());
return true;
});

Event::assertDispatched(WebinarAuthorUnassigned::class, function (WebinarAuthorUnassigned $event) use ($author1){
$this->assertEquals($author1->getKey(), $event->getUser()->getKey());
return true;
});
}

public function testWebinarUpdateFailed(): void
{
$webinar = Webinar::factory()->create();
Expand Down