2021-03-09 15:25:50 +01:00
|
|
|
<?php
|
2021-03-09 23:23:13 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-03-09 23:23:13 +01:00
|
|
|
*/
|
2021-03-09 15:25:50 +01:00
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2024-01-14 02:34:40 +01:00
|
|
|
use Tests\TestCase;
|
|
|
|
use App\Models\Invoice;
|
2021-03-09 15:25:50 +01:00
|
|
|
use App\Models\Product;
|
2024-01-14 02:34:40 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Illuminate\Support\Str;
|
2021-04-01 11:56:50 +02:00
|
|
|
use App\Models\Subscription;
|
2021-03-09 15:25:50 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2021-04-14 08:48:32 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2024-01-14 02:34:40 +01:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2021-03-09 15:25:50 +01:00
|
|
|
|
2021-03-09 23:23:13 +01:00
|
|
|
/**
|
|
|
|
* @test
|
2021-03-25 11:55:59 +01:00
|
|
|
* @covers App\Http\Controllers\SubscriptionController
|
2021-03-09 23:23:13 +01:00
|
|
|
*/
|
2021-03-25 11:55:59 +01:00
|
|
|
class SubscriptionApiTest extends TestCase
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
2024-01-14 02:34:40 +01:00
|
|
|
protected $faker;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp(): void
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
2021-04-14 08:48:32 +02:00
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
|
2021-03-09 15:25:50 +01:00
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
}
|
|
|
|
|
2024-01-14 02:34:40 +01:00
|
|
|
public function testAssignInvoice()
|
|
|
|
{
|
|
|
|
$i = Invoice::factory()
|
|
|
|
->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$s = Subscription::factory()
|
|
|
|
->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'ids' => [$s->hashed_id],
|
|
|
|
'entity' => 'invoice',
|
|
|
|
'entity_id' => $i->hashed_id,
|
|
|
|
'action' => 'assign_invoice'
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->postJson('/api/v1/subscriptions/bulk', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$i = $i->fresh();
|
|
|
|
|
|
|
|
$this->assertEquals($s->id, $i->subscription_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssignRecurringInvoice()
|
|
|
|
{
|
|
|
|
$i = RecurringInvoice::factory()
|
|
|
|
->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$s = Subscription::factory()
|
|
|
|
->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'ids' => [$s->hashed_id],
|
|
|
|
'entity' => 'recurring_invoice',
|
|
|
|
'entity_id' => $i->hashed_id,
|
|
|
|
'action' => 'assign_invoice'
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->postJson('/api/v1/subscriptions/bulk', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$i = $i->fresh();
|
|
|
|
|
|
|
|
$this->assertEquals($s->id, $i->subscription_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-19 01:52:07 +01:00
|
|
|
public function testSubscriptionFilter()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/subscriptions?filter=xx')
|
|
|
|
->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function testSubscriptionsGet()
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
$product = Product::factory()->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
$billing_subscription = Subscription::factory()->create([
|
2021-03-25 20:42:25 +01:00
|
|
|
'product_ids' => $product->id,
|
2021-03-09 15:25:50 +01:00
|
|
|
'company_id' => $this->company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'name' => Str::random(5),
|
2021-03-09 15:25:50 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-06-21 11:57:17 +02:00
|
|
|
])->get('/api/v1/subscriptions/'.$this->encodePrimaryKey($billing_subscription->id));
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
// nlog($response);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-03-09 15:25:50 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function testSubscriptionsPost()
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
$product = Product::factory()->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-11-24 06:49:03 +01:00
|
|
|
])->post('/api/v1/subscriptions', ['product_ids' => $product->hashed_id, 'allow_cancellation' => true, 'name' => Str::random(5)]);
|
2021-03-09 15:25:50 +01:00
|
|
|
|
2021-03-27 04:22:19 +01:00
|
|
|
// nlog($response);
|
2021-03-09 15:25:50 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function testSubscriptionPut()
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
$product = Product::factory()->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response1 = $this
|
2022-06-21 11:57:17 +02:00
|
|
|
->withHeaders(['X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token])
|
2022-11-24 06:49:03 +01:00
|
|
|
->post('/api/v1/subscriptions', ['product_ids' => $product->hashed_id, 'name' => Str::random(5)])
|
2021-03-09 15:25:50 +01:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
|
2021-04-14 08:48:32 +02:00
|
|
|
// try {
|
2022-06-21 11:57:17 +02:00
|
|
|
$response2 = $this
|
|
|
|
->withHeaders(['X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token])
|
|
|
|
->put('/api/v1/subscriptions/'.$response1['data']['id'], ['allow_cancellation' => true])
|
2021-03-09 15:25:50 +01:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
2022-06-21 11:57:17 +02:00
|
|
|
// }catch(ValidationException $e) {
|
|
|
|
// nlog($e->validator->getMessageBag());
|
2021-04-14 08:48:32 +02:00
|
|
|
// }
|
2021-03-09 15:25:50 +01:00
|
|
|
|
|
|
|
$this->assertNotEquals($response1['data']['allow_cancellation'], $response2['data']['allow_cancellation']);
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function testSubscriptionDeleted()
|
2021-03-09 15:25:50 +01:00
|
|
|
{
|
|
|
|
$product = Product::factory()->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
$billing_subscription = Subscription::factory()->create([
|
2021-03-25 20:42:25 +01:00
|
|
|
'product_ids' => $product->id,
|
2021-03-09 15:25:50 +01:00
|
|
|
'company_id' => $this->company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'name' => Str::random(5),
|
2021-03-09 15:25:50 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this
|
|
|
|
->withHeaders(['X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token])
|
2022-06-21 11:57:17 +02:00
|
|
|
->delete('/api/v1/subscriptions/'.$this->encodePrimaryKey($billing_subscription->id))
|
2021-03-09 15:25:50 +01:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
}
|
|
|
|
}
|