1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/tests/Integration/EventTest.php

923 lines
28 KiB
PHP
Raw Normal View History

2020-11-03 11:04:15 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-03 11:04:15 +01:00
*
2023-02-16 02:36:09 +01:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-03 11:04:15 +01:00
*/
2023-08-21 05:29:34 +02:00
2020-11-03 11:04:15 +01:00
namespace Tests\Integration;
2023-08-21 05:29:34 +02:00
use Tests\TestCase;
use App\Models\Quote;
use App\Models\Invoice;
use Tests\MockAccountData;
2023-08-21 05:38:57 +02:00
use App\Models\PurchaseOrder;
2023-08-21 05:29:34 +02:00
use App\Utils\Traits\MakesHash;
use App\Events\Task\TaskWasCreated;
use App\Events\Task\TaskWasDeleted;
use App\Events\Task\TaskWasUpdated;
use App\Events\User\UserWasCreated;
use App\Events\User\UserWasDeleted;
use App\Events\User\UserWasUpdated;
use App\Events\Task\TaskWasArchived;
use App\Events\Task\TaskWasRestored;
use App\Events\User\UserWasArchived;
use App\Events\User\UserWasRestored;
use App\Events\Quote\QuoteWasCreated;
use App\Events\Quote\QuoteWasDeleted;
use App\Events\Quote\QuoteWasUpdated;
use Illuminate\Support\Facades\Event;
use App\Events\Quote\QuoteWasApproved;
use App\Events\Quote\QuoteWasArchived;
use App\Events\Quote\QuoteWasRestored;
2020-11-03 11:04:15 +01:00
use App\Events\Client\ClientWasCreated;
use App\Events\Client\ClientWasDeleted;
use App\Events\Client\ClientWasUpdated;
2020-11-03 13:35:05 +01:00
use App\Events\Credit\CreditWasCreated;
use App\Events\Credit\CreditWasDeleted;
use App\Events\Credit\CreditWasUpdated;
2023-08-21 05:29:34 +02:00
use App\Events\Vendor\VendorWasCreated;
use App\Events\Vendor\VendorWasDeleted;
use App\Events\Vendor\VendorWasUpdated;
use Illuminate\Database\Eloquent\Model;
use App\Events\Client\ClientWasArchived;
use App\Events\Client\ClientWasRestored;
use App\Events\Credit\CreditWasArchived;
use App\Events\Credit\CreditWasRestored;
use App\Events\Vendor\VendorWasArchived;
use App\Events\Vendor\VendorWasRestored;
2020-11-03 13:39:41 +01:00
use App\Events\Expense\ExpenseWasCreated;
use App\Events\Expense\ExpenseWasDeleted;
use App\Events\Expense\ExpenseWasUpdated;
2020-11-03 11:04:15 +01:00
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasDeleted;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Events\Payment\PaymentWasCreated;
use App\Events\Payment\PaymentWasDeleted;
use App\Events\Payment\PaymentWasUpdated;
2023-08-21 05:29:34 +02:00
use App\Events\Expense\ExpenseWasArchived;
use App\Events\Expense\ExpenseWasRestored;
use App\Events\Invoice\InvoiceWasArchived;
use App\Events\Invoice\InvoiceWasRestored;
use App\Events\Payment\PaymentWasArchived;
use App\Events\Payment\PaymentWasRestored;
use App\Http\Middleware\PasswordProtection;
use Illuminate\Validation\ValidationException;
use App\Events\Subscription\SubscriptionWasCreated;
use App\Events\Subscription\SubscriptionWasDeleted;
use App\Events\Subscription\SubscriptionWasUpdated;
use Illuminate\Routing\Middleware\ThrottleRequests;
use App\Events\Subscription\SubscriptionWasArchived;
use App\Events\Subscription\SubscriptionWasRestored;
2023-02-16 02:36:09 +01:00
use App\Events\PurchaseOrder\PurchaseOrderWasCreated;
use App\Events\PurchaseOrder\PurchaseOrderWasDeleted;
use App\Events\PurchaseOrder\PurchaseOrderWasUpdated;
2023-08-21 05:29:34 +02:00
use App\Events\PurchaseOrder\PurchaseOrderWasArchived;
use App\Events\PurchaseOrder\PurchaseOrderWasRestored;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2021-05-10 12:20:52 +02:00
use App\Events\RecurringInvoice\RecurringInvoiceWasCreated;
use App\Events\RecurringInvoice\RecurringInvoiceWasDeleted;
use App\Events\RecurringInvoice\RecurringInvoiceWasUpdated;
2023-08-21 05:29:34 +02:00
use App\Events\RecurringInvoice\RecurringInvoiceWasArchived;
use App\Events\RecurringInvoice\RecurringInvoiceWasRestored;
2020-11-03 11:04:15 +01:00
/**
* @test
*/
class EventTest extends TestCase
{
use MockAccountData;
use MakesHash;
2021-01-14 05:31:45 +01:00
use DatabaseTransactions;
2020-11-03 11:04:15 +01:00
2023-08-21 05:38:57 +02:00
public $faker;
2023-08-21 05:29:34 +02:00
public function setUp(): void
2020-11-03 11:04:15 +01:00
{
parent::setUp();
$this->faker = \Faker\Factory::create();
$this->makeTestData();
2021-01-14 05:31:45 +01:00
Model::reguard();
$this->withoutMiddleware(
ThrottleRequests::class,
PasswordProtection::class
);
2020-11-03 11:04:15 +01:00
}
2020-11-03 13:39:41 +01:00
public function testExpenseEvents()
{
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 13:39:41 +01:00
$data = [
'public_notes' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/expenses/', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'public_notes' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/expenses/' . $arr['data']['id'], $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/expenses/bulk?action=archive', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/expenses/bulk?action=restore', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/expenses/bulk?action=delete', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(ExpenseWasCreated::class);
Event::assertDispatched(ExpenseWasUpdated::class);
Event::assertDispatched(ExpenseWasArchived::class);
Event::assertDispatched(ExpenseWasRestored::class);
Event::assertDispatched(ExpenseWasDeleted::class);
2020-11-03 13:39:41 +01:00
}
public function testVendorEvents()
{
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 13:39:41 +01:00
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/vendors/', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'name' => $this->faker->firstName,
'id_number' => 'Coolio',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/vendors/' . $arr['data']['id'], $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/vendors/bulk?action=archive', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/vendors/bulk?action=restore', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/vendors/bulk?action=delete', $data)
2020-11-03 13:39:41 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(VendorWasCreated::class);
Event::assertDispatched(VendorWasUpdated::class);
Event::assertDispatched(VendorWasArchived::class);
Event::assertDispatched(VendorWasRestored::class);
Event::assertDispatched(VendorWasDeleted::class);
2020-11-03 13:39:41 +01:00
}
2020-11-03 13:35:05 +01:00
public function testTaskEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'description' => 'dude',
];
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 13:35:05 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/tasks/', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'description' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/tasks/' . $arr['data']['id'], $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/tasks/bulk?action=archive', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/tasks/bulk?action=restore', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/tasks/bulk?action=delete', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(TaskWasCreated::class);
Event::assertDispatched(TaskWasUpdated::class);
Event::assertDispatched(TaskWasArchived::class);
Event::assertDispatched(TaskWasRestored::class);
Event::assertDispatched(TaskWasDeleted::class);
2020-11-03 13:35:05 +01:00
}
public function testCreditEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 13:35:05 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/credits/', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/credits/' . $arr['data']['id'], $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/credits/bulk?action=archive', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/credits/bulk?action=restore', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/credits/bulk?action=delete', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(CreditWasCreated::class);
Event::assertDispatched(CreditWasUpdated::class);
Event::assertDispatched(CreditWasArchived::class);
Event::assertDispatched(CreditWasRestored::class);
Event::assertDispatched(CreditWasDeleted::class);
2020-11-03 13:35:05 +01:00
}
2023-08-21 05:29:34 +02:00
2020-11-03 11:19:32 +01:00
public function testQuoteEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 11:19:32 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/quotes/', $data)
2020-11-03 11:19:32 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/quotes/' . $arr['data']['id'], $data)
2020-11-03 11:19:32 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
2020-11-03 13:35:05 +01:00
$quote = Quote::find($this->decodePrimaryKey($arr['data']['id']));
$quote->status_id = Quote::STATUS_SENT;
$quote->save();
2020-11-03 11:19:32 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/quotes/bulk?action=archive', $data)
2020-11-03 11:19:32 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/quotes/bulk?action=restore', $data)
2020-11-03 11:19:32 +01:00
->assertStatus(200);
2020-11-03 13:35:05 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/quotes/bulk?action=approve', $data)
2020-11-03 13:35:05 +01:00
->assertStatus(200);
2020-11-03 11:19:32 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/quotes/bulk?action=delete', $data)
2020-11-03 11:19:32 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(QuoteWasCreated::class);
Event::assertDispatched(QuoteWasUpdated::class);
Event::assertDispatched(QuoteWasArchived::class);
Event::assertDispatched(QuoteWasRestored::class);
Event::assertDispatched(QuoteWasDeleted::class);
Event::assertDispatched(QuoteWasApproved::class);
2020-11-03 11:19:32 +01:00
}
2020-11-03 11:04:15 +01:00
//@TODO paymentwasvoided
//@TODO paymentwasrefunded
public function testPaymentEvents()
{
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 11:04:15 +01:00
$data = [
'amount' => $this->invoice->amount,
'client_id' => $this->client->hashed_id,
'invoices' => [
[
'invoice_id' => $this->invoice->hashed_id,
'amount' => $this->invoice->amount,
],
],
'date' => '2020/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/payments?include=invoices', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'transaction_reference' => 'testing'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/payments/' . $arr['data']['id'], $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/payments/bulk?action=archive', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/payments/bulk?action=restore', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/payments/bulk?action=delete', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(PaymentWasCreated::class);
Event::assertDispatched(PaymentWasUpdated::class);
Event::assertDispatched(PaymentWasArchived::class);
Event::assertDispatched(PaymentWasRestored::class);
Event::assertDispatched(PaymentWasDeleted::class);
2020-11-03 11:04:15 +01:00
}
public function testInvoiceEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude',
];
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 11:04:15 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/invoices/', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/invoices/' . $arr['data']['id'], $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/invoices/bulk?action=archive', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/invoices/bulk?action=restore', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/invoices/bulk?action=delete', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(InvoiceWasCreated::class);
Event::assertDispatched(InvoiceWasUpdated::class);
Event::assertDispatched(InvoiceWasArchived::class);
Event::assertDispatched(InvoiceWasRestored::class);
Event::assertDispatched(InvoiceWasDeleted::class);
2020-11-03 11:04:15 +01:00
}
2021-05-10 12:20:52 +02:00
public function testRecurringInvoiceEvents()
{
/* Test fire new invoice */
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dudex',
'frequency_id' => 1,
];
2023-08-21 05:29:34 +02:00
Event::fake();
2021-05-10 12:20:52 +02:00
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/recurring_invoices/', $data);
2021-05-10 12:20:52 +02:00
} catch (ValidationException $e) {
2023-08-21 05:38:57 +02:00
// $message = json_decode($e->validator->getMessageBag(), 1);
2021-05-10 12:20:52 +02:00
}
$response->assertStatus(200);
$arr = $response->json();
$data = [
'client_id' => $this->client->hashed_id,
'number' => 'dude2',
'frequency_id' => 1,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/recurring_invoices/' . $arr['data']['id'], $data)
2021-05-10 12:20:52 +02:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/recurring_invoices/bulk?action=archive', $data)
2021-05-10 12:20:52 +02:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/recurring_invoices/bulk?action=restore', $data)
2021-05-10 12:20:52 +02:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/recurring_invoices/bulk?action=delete', $data)
2021-05-10 12:20:52 +02:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(RecurringInvoiceWasCreated::class);
Event::assertDispatched(RecurringInvoiceWasUpdated::class);
Event::assertDispatched(RecurringInvoiceWasArchived::class);
Event::assertDispatched(RecurringInvoiceWasRestored::class);
Event::assertDispatched(RecurringInvoiceWasDeleted::class);
2021-05-10 12:20:52 +02:00
}
2020-11-03 11:04:15 +01:00
public function testClientEvents()
{
2023-08-21 05:29:34 +02:00
Event::fake();
2020-11-03 11:04:15 +01:00
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/clients/', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$arr = $response->json();
$data = [
'name' => $this->faker->firstName,
'id_number' => 'Coolio',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/clients/' . $arr['data']['id'], $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/clients/bulk?action=archive', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/clients/bulk?action=restore', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/clients/bulk?action=delete', $data)
2020-11-03 11:04:15 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(ClientWasCreated::class);
Event::assertDispatched(ClientWasUpdated::class);
Event::assertDispatched(ClientWasArchived::class);
Event::assertDispatched(ClientWasRestored::class);
Event::assertDispatched(ClientWasDeleted::class);
2020-11-03 11:04:15 +01:00
}
2021-01-14 05:31:45 +01:00
public function testUserEvents()
{
$this->withoutMiddleware(PasswordProtection::class);
2023-08-21 05:29:34 +02:00
Event::fake();
2021-01-14 05:31:45 +01:00
$data = [
'first_name' => 'hey',
'last_name' => 'you',
'email' => 'bob1@good.ole.boys.com',
'company_user' => [
'is_admin' => false,
'is_owner' => false,
'permissions' => 'create_client,create_invoice',
],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/users?include=company_user', $data)
2021-01-14 05:31:45 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
2021-01-14 05:31:45 +01:00
$arr = $response->json();
$data = [
'first_name' => 'hasdasdy',
'last_name' => 'you',
'email' => 'bob1@good.ole.boys.com',
'company_user' => [
'is_admin' => false,
'is_owner' => false,
'permissions' => 'create_client,create_invoice',
],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/users/' . $arr['data']['id'], $data)
2021-01-14 05:31:45 +01:00
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/users/bulk?action=archive', $data)
2021-01-14 05:31:45 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/users/bulk?action=restore', $data)
2021-01-14 05:31:45 +01:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/users/bulk?action=delete', $data)
2021-01-14 05:31:45 +01:00
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(UserWasCreated::class);
Event::assertDispatched(UserWasUpdated::class);
Event::assertDispatched(UserWasArchived::class);
Event::assertDispatched(UserWasRestored::class);
Event::assertDispatched(UserWasDeleted::class);
2021-01-14 05:31:45 +01:00
}
public function testSubscriptionEvents()
{
2023-08-21 05:29:34 +02:00
Event::fake();
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/subscriptions/', $data)
->assertStatus(200);
$arr = $response->json();
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->putJson('/api/v1/subscriptions/' . $arr['data']['id'], $data)
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/subscriptions/bulk?action=archive', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/subscriptions/bulk?action=restore', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-07-01 11:24:22 +02:00
])->postJson('/api/v1/subscriptions/bulk?action=delete', $data)
->assertStatus(200);
2023-08-21 05:29:34 +02:00
Event::assertDispatched(SubscriptionWasCreated::class);
Event::assertDispatched(SubscriptionWasUpdated::class);
Event::assertDispatched(SubscriptionWasArchived::class);
Event::assertDispatched(SubscriptionWasRestored::class);
Event::assertDispatched(SubscriptionWasDeleted::class);
}
2021-01-14 05:31:45 +01:00
2022-06-09 04:24:42 +02:00
2023-08-21 05:38:57 +02:00
public function testPurchaseOrderEvents()
2023-08-21 05:29:34 +02:00
{
/* Test fire new invoice */
$data = [
2023-08-21 05:38:57 +02:00
'vendor_id' => $this->vendor->hashed_id,
2023-08-21 05:29:34 +02:00
'number' => 'dude',
];
Event::fake();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/purchase_orders/', $data)
->assertStatus(200);
$arr = $response->json();
$data = [
2023-08-21 05:38:57 +02:00
'vendor_id' => $this->vendor->hashed_id,
2023-08-21 05:29:34 +02:00
'number' => 'dude2',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->putJson('/api/v1/purchase_orders/' . $arr['data']['id'], $data)
->assertStatus(200);
$data = [
'ids' => [$arr['data']['id']],
];
$quote = PurchaseOrder::find($this->decodePrimaryKey($arr['data']['id']));
$quote->status_id = PurchaseOrder::STATUS_SENT;
$quote->save();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/purchase_orders/bulk?action=archive', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/purchase_orders/bulk?action=restore', $data)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2023-08-21 07:05:57 +02:00
])->postJson('/api/v1/purchase_orders/bulk?action=mark_sent', $data)
2023-08-21 05:29:34 +02:00
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/purchase_orders/bulk?action=delete', $data)
->assertStatus(200);
Event::assertDispatched(PurchaseOrderWasCreated::class);
Event::assertDispatched(PurchaseOrderWasUpdated::class);
Event::assertDispatched(PurchaseOrderWasArchived::class);
Event::assertDispatched(PurchaseOrderWasRestored::class);
Event::assertDispatched(PurchaseOrderWasDeleted::class);
}
2020-11-03 11:04:15 +01:00
}