Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/test-php81
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 committed Mar 28, 2022
2 parents 7332db1 + cc6cabf commit 9ac9cdf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
17 changes: 17 additions & 0 deletions src/Consultations/ReminderAboutTermVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EscolaLms\TemplatesSms\Consultations;

class ReminderAboutTermVariables extends CommonConsultationVariables
{
public static function defaultSectionsContent(): array
{
return [
'content' => __('Hello :user_name! I would like to remind you about the upcoming consultation :consultation, which will take place :proposed_term.', [
'user_name' => self::VAR_USER_NAME,
'consultation' => self::VAR_CONSULTATION_TITLE,
'proposed_term' => self::VAR_CONSULTATION_PROPOSED_TERM
]),
];
}
}
2 changes: 1 addition & 1 deletion src/Consultations/ReportTermVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function requiredVariablesInSection(string $sectionKey): array
public static function variablesFromEvent(EventWrapper $event): array
{
return array_merge(parent::variablesFromEvent($event), [
self::VAR_CONSULTATION_BUYER_NAME => $event->getConsultationTerm()->orderItem->order->user->name,
self::VAR_CONSULTATION_BUYER_NAME => $event->getConsultationTerm()->user->name,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Providers/ConsultationTemplatesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\TemplatesSms\Providers;

use EscolaLms\Consultations\Events\ApprovedTerm;
use EscolaLms\Consultations\Events\ReminderAboutTerm;
use EscolaLms\Consultations\Events\ReportTerm;
use EscolaLms\Templates\Facades\Template;
use EscolaLms\TemplatesSms\Consultations\ApprovedTermVariables;
Expand All @@ -16,5 +17,6 @@ public function boot()
{
Template::register(ReportTerm::class, SmsChannel::class, ReportTermVariables::class);
Template::register(ApprovedTerm::class, SmsChannel::class, ApprovedTermVariables::class);
Template::register(ReminderAboutTerm::class, SmsChannel::class, ReportTermVariables::class);
}
}
103 changes: 42 additions & 61 deletions tests/Api/ConsultationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@

namespace EscolaLms\TemplatesSms\Tests\Api;

use EscolaLms\Cart\Events\OrderPaid;
use EscolaLms\Cart\Models\Order;
use EscolaLms\Cart\Models\OrderItem;
use EscolaLms\Cart\Models\Product;
use EscolaLms\Cart\Models\ProductProductable;
use EscolaLms\Cart\Models\User;
use EscolaLms\Consultations\Events\ApprovedTerm;
use EscolaLms\Consultations\Events\ReportTerm;
use EscolaLms\Consultations\Listeners\ReportTermListener;
use EscolaLms\Consultations\Enum\ConsultationTermReminderStatusEnum;
use EscolaLms\Consultations\Enum\ConsultationTermStatusEnum;
use EscolaLms\Consultations\Jobs\ReminderAboutConsultationJob;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Models\ConsultationTerm;
use EscolaLms\Consultations\Repositories\Contracts\ConsultationTermsRepositoryContract;
use EscolaLms\Templates\Models\Template;
use EscolaLms\Templates\Models\TemplateSection;
use EscolaLms\TemplatesSms\Core\SmsChannel;
use EscolaLms\Consultations\Models\ConsultationUserPivot;
use EscolaLms\Core\Models\User;
use EscolaLms\TemplatesSms\Database\Seeders\TemplateSmsSeeder;
use EscolaLms\TemplatesSms\Facades\Sms;
use EscolaLms\TemplatesSms\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;

class ConsultationApiTest extends TestCase
{
use DatabaseTransactions;
private Consultation $consultation;
private ConsultationUserPivot $consultationUserPivot;

protected function setUp(): void
{
Expand All @@ -46,82 +38,71 @@ protected function setUp(): void

public function testConsultationReportTerm(): void
{
$this->initVariable();
Sms::fake();

$orderItem = $this->createOrder()->items()->first();
$this->response = $this->actingAs($this->user, 'api')
->json('POST',
'/api/consultations/report-term/' . $orderItem->getKey(),
'/api/consultations/report-term/' . $this->consultationUserPivot->getKey(),
[
'term' => now()->modify('+1 day')->format('Y-m-d H:i:s')
]
);

$consultationTermsRepositoryContract = app(ConsultationTermsRepositoryContract::class);
$consultationTerm = $consultationTermsRepositoryContract->findByOrderItem($orderItem->getKey());
$this->assertSms($consultationTerm);
$this->assertSms($this->consultationUserPivot);
}

public function testConsultationApprovedTerm(): void
{
$this->initVariable();
Sms::fake();

$orderItem = $this->createOrder()->items()->first();
$this->response = $this->actingAs($this->user, 'api')
->json('POST',
'/api/consultations/report-term/' . $orderItem->getKey(),
'/api/consultations/report-term/' . $this->consultationUserPivot->getKey(),
[
'term' => now()->modify('+1 day')->format('Y-m-d H:i:s')
]
);

$consultationTermsRepositoryContract = app(ConsultationTermsRepositoryContract::class);
$consultationTerm = $consultationTermsRepositoryContract->findByOrderItem($orderItem->getKey());
$this->consultationUserPivot->refresh();
$this->response = $this->actingAs($this->user, 'api')->json(
'GET',
'/api/consultations/approve-term/' . $consultationTerm->getKey()
'/api/consultations/approve-term/' . $this->consultationUserPivot->getKey()
);

$this->assertSms($consultationTerm);
$this->assertSms($this->consultationUserPivot);
}

private function createOrder(): Order
public function testReminderAboutConsultationBeforeHour()
{
$consultationsForOrder = Consultation::factory(3)->create();
$price = $consultationsForOrder->reduce(fn ($acc, Consultation $consultation) => $acc + $consultation->getBuyablePrice(), 0);
$order = Order::factory()->afterCreating(
fn (Order $order) => $order->items()->saveMany(
$consultationsForOrder->map(
function (Consultation $consultation) {
$product = Product::factory()->create();
$product->productables()->save(new ProductProductable([
'productable_id' => $consultation->getKey(),
'productable_type' => $consultation->getMorphClass(),
]));
return OrderItem::query()->make([
'quantity' => 1,
'buyable_id' => $product->getKey(),
'buyable_type' => Product::class,
]);
}
)
)
)->create([
Sms::fake();
$this->consultation = Consultation::factory()->create();
$this->consultationUserPivot = ConsultationUserPivot::factory([
'consultation_id' => $this->consultation->getKey(),
'user_id' => $this->user->getKey(),
'total' => $price,
'subtotal' => $price,
]);

Event::fakeFor(function () use ($order) {
$event = new OrderPaid($order, $this->user);
$listener = app(ReportTermListener::class);
$listener->handle($event);
});
'executed_at' => now()->modify('+1 hour')->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::APPROVED
])->create();
$this->assertTrue($this->consultationUserPivot->reminder_status === null);
$job = new ReminderAboutConsultationJob(ConsultationTermReminderStatusEnum::REMINDED_HOUR_BEFORE);
$job->handle();
$this->consultationUserPivot->refresh();
$this->assertSms($this->consultationUserPivot);
$this->assertTrue(
$this->consultationUserPivot->reminder_status === ConsultationTermReminderStatusEnum::REMINDED_HOUR_BEFORE
);
}

return Order::whereUserId($this->user->getKey())->first();
private function initVariable(): void
{
$this->consultation = Consultation::factory()->create();
$this->consultationUserPivot = ConsultationUserPivot::factory([
'consultation_id' => $this->consultation->getKey(),
'user_id' => $this->user->getKey(),
'executed_at' => null,
'executed_status' => ConsultationTermStatusEnum::NOT_REPORTED,
])->create();
}

private function assertSms(ConsultationTerm $consultationTerm): void
private function assertSms(ConsultationUserPivot $consultationTerm): void
{
Sms::assertSent(function ($sms) use ($consultationTerm) {
return $sms->to === $this->user->phone
Expand Down
2 changes: 1 addition & 1 deletion tests/Api/TemplateApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EscolaLms\TemplatesSms\Tests\Api;

use EscolaLms\Cart\Models\User;
use EscolaLms\Core\Models\User;
use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\Templates\Events\ManuallyTriggeredEvent;
use EscolaLms\Templates\Listeners\TemplateEventListener;
Expand Down

0 comments on commit 9ac9cdf

Please sign in to comment.