2022-02-01 07:35:16 +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)
|
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2022-02-01 07:35:16 +01:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-02-01 07:35:16 +01:00
|
|
|
namespace Tests\Feature\Import\CSV;
|
|
|
|
|
|
|
|
use App\Import\Transformer\BaseTransformer;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Product;
|
2022-02-01 10:04:03 +01:00
|
|
|
use App\Models\TaxRate;
|
2022-02-01 07:35:16 +01:00
|
|
|
use App\Models\Vendor;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Import\Transformer\BaseTransformer
|
|
|
|
*/
|
|
|
|
class BaseTransformerTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MockAccountData;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2022-02-01 07:35:16 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
config(['database.default' => config('ninja.db.default')]);
|
|
|
|
|
|
|
|
// $this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-02-01 07:35:16 +01:00
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetString()
|
|
|
|
{
|
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'key' => 'value',
|
2022-02-01 07:35:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$field = 'key';
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals('value', $base_transformer->getString($data, $field));
|
2022-02-01 07:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCurrencyCode()
|
|
|
|
{
|
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$code = ['client.currency_id' => 'USD'];
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$currency_id = $base_transformer->getCurrencyByCode($code);
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals(1, $currency_id);
|
2022-02-01 07:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClient()
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
2022-02-01 07:35:16 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Client::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'id_number' => 'hit',
|
|
|
|
'name' => 'magic ',
|
|
|
|
]);
|
2022-02-01 07:35:16 +01:00
|
|
|
|
|
|
|
$contact = ClientContact::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
'email' => 'test@gmail.com',
|
2022-02-01 07:35:16 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($client->id, $base_transformer->getClient('hit', 'null'));
|
|
|
|
$this->assertEquals($client->id, $base_transformer->getClient('magic', 'null'));
|
|
|
|
$this->assertEquals($client->id, $base_transformer->getClient('nomagic', 'test@gmail.com'));
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals($client->id, $base_transformer->getClient(null, 'test@gmail.com'));
|
2023-04-13 07:28:26 +02:00
|
|
|
$this->assertNotNull($base_transformer->getClient('null', 'notest@gmail.com'));
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-02-01 10:04:03 +01:00
|
|
|
$this->assertEquals($client->id, $base_transformer->getClientId(' magic'));
|
|
|
|
$this->assertEquals($client->id, $base_transformer->getClientId('Magic '));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-02-01 10:04:03 +01:00
|
|
|
public function testGetContact()
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
2022-02-01 07:56:13 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Client::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'id_number' => 'hit',
|
|
|
|
'name' => 'magic ',
|
|
|
|
]);
|
2022-02-01 07:56:13 +01:00
|
|
|
|
2022-02-01 10:04:03 +01:00
|
|
|
$contact = ClientContact::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
'email' => 'test@gmail.com',
|
2022-02-01 10:04:03 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($contact->id, $base_transformer->getContact('TeSt@gmail.com')->id);
|
|
|
|
$this->assertEquals($contact->id, $base_transformer->getContact('TeSt@gmail.com ')->id);
|
|
|
|
$this->assertEquals($contact->id, $base_transformer->getContact('TeSt@gmaiL.com')->id);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testHasClient()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Client::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'id_number' => 'hit',
|
|
|
|
'name' => 'maGic ',
|
|
|
|
]);
|
2022-02-01 07:56:13 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertTrue($base_transformer->hasClient('magic'));
|
|
|
|
$this->assertTrue($base_transformer->hasClient('Magic'));
|
|
|
|
$this->assertTrue($base_transformer->hasClient('Ma gi c '));
|
|
|
|
}
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testHasVendor()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Vendor::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'id_number' => 'hit',
|
|
|
|
'name' => 'maGic ',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertTrue($base_transformer->hasVendor('magic'));
|
|
|
|
$this->assertTrue($base_transformer->hasVendor('Magic'));
|
|
|
|
$this->assertTrue($base_transformer->hasVendor('Ma gi c '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testHasProduct()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Product::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'product_key' => 'HiT ',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertTrue($base_transformer->hasProduct('hit'));
|
|
|
|
$this->assertTrue($base_transformer->hasProduct(' hIt'));
|
|
|
|
$this->assertTrue($base_transformer->hasProduct(' h i T '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testGetCountryId()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals(840, $base_transformer->getCountryId('us'));
|
|
|
|
$this->assertEquals(840, $base_transformer->getCountryId('US'));
|
|
|
|
$this->assertEquals(840, $base_transformer->getCountryId('United States'));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testGetTaxRate()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = TaxRate::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'rate' => '10',
|
|
|
|
'name' => 'GST',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals(10, $base_transformer->getTaxRate('gst'));
|
|
|
|
$this->assertEquals(10, $base_transformer->getTaxRate(' GST'));
|
|
|
|
$this->assertEquals(10, $base_transformer->getTaxRate(' gS t '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testGetTaxName()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = TaxRate::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'rate' => '17.5',
|
|
|
|
'name' => 'VAT',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals('VAT', $base_transformer->getTaxName('vat'));
|
|
|
|
$this->assertEquals('VAT', $base_transformer->getTaxName(' VaT'));
|
|
|
|
$this->assertEquals('VAT', $base_transformer->getTaxName(' va T '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testGetInvoiceId()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice = Invoice::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'number' => 'trick_number_123',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals($invoice->id, $base_transformer->getInvoiceId('TRICK_number_123'));
|
|
|
|
$this->assertEquals($invoice->id, $base_transformer->getInvoiceId(' TRICK_number_123'));
|
|
|
|
$this->assertEquals($invoice->id, $base_transformer->getInvoiceId(' TRICK_number_123 '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testHasInvoiceWithNumber()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Invoice::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'number' => 'tricky_number_123',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertTrue($base_transformer->hasInvoice('TRICKY_number_123'));
|
|
|
|
$this->assertTrue($base_transformer->hasInvoice(' TRICKY_number_123'));
|
|
|
|
$this->assertTrue($base_transformer->hasInvoice(' TRICKY_number_123 '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testInvoiceClientId()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$client = Invoice::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'number' => 'tricky_number_123',
|
|
|
|
]);
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals($this->client->id, $base_transformer->getInvoiceClientId('TRICKY_number_123'));
|
|
|
|
$this->assertEquals($this->client->id, $base_transformer->getInvoiceClientId(' TRICKY_number_123'));
|
|
|
|
$this->assertEquals($this->client->id, $base_transformer->getInvoiceClientId(' TRICKY_number_123 '));
|
|
|
|
}
|
2022-02-01 10:04:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testGetVendorId()
|
|
|
|
{
|
2022-02-01 10:04:03 +01:00
|
|
|
$base_transformer = new BaseTransformer($this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$vendor = Vendor::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'id_number' => 'hit',
|
|
|
|
'name' => 'maGic ',
|
|
|
|
]);
|
2022-02-01 07:35:16 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->assertEquals($vendor->id, $base_transformer->getVendorId('magic'));
|
|
|
|
$this->assertEquals($vendor->id, $base_transformer->getVendorId('Magic'));
|
|
|
|
$this->assertEquals($vendor->id, $base_transformer->getVendorId('Ma gi c '));
|
|
|
|
}
|
2022-02-01 07:35:16 +01:00
|
|
|
}
|