From 38075b9754d6aaa4c6e1b325aef3116a7cf99ec3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Feb 2022 17:35:16 +1100 Subject: [PATCH] Tests for basetransformer --- app/Import/Transformer/BaseTransformer.php | 21 ++- .../Import/CSV/BaseTransformerTest.php | 145 ++++++++++++++++++ 2 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 tests/Feature/Import/CSV/BaseTransformerTest.php diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index 63520b689a52..72ae3bf8b8ac 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -17,6 +17,7 @@ use App\Models\PaymentType; use App\Utils\Number; use Exception; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Cache; /** * Class BaseTransformer. @@ -38,23 +39,29 @@ class BaseTransformer public function getCurrencyByCode( $data, $key = 'client.currency_id' ) { - $code = array_key_exists( $key, $data ) ? $data[ $key ] : false; - return $this->maps['currencies'][ $code ] ?? $this->company->settings->currency_id; - } + $code = array_key_exists( $key, $data ) ? $data[ $key ] : false; + + $currencies = Cache::get('currencies'); + + $currency = $currencies->filter(function ($item) use($code) { + return $item->code == $code; + })->first(); + + return $currency ? $currency->id : $this->company->settings->currency_id; + + } public function getClient($client_name, $client_email) { - $clients = $this->company->clients(); - - $client_id_search = $clients->where( 'id_number', $client_name ); + $client_id_search = $this->company->clients()->where( 'id_number', $client_name ); if ( $client_id_search->count() >= 1 ) { return $client_id_search->first()->id; nlog("found via id number"); } - $client_name_search = $clients->where( 'name', $client_name ); + $client_name_search = $this->company->clients()->where( 'name', $client_name ); if ( $client_name_search->count() >= 1 ) { return $client_name_search->first()->id; diff --git a/tests/Feature/Import/CSV/BaseTransformerTest.php b/tests/Feature/Import/CSV/BaseTransformerTest.php new file mode 100644 index 000000000000..2d633535acdf --- /dev/null +++ b/tests/Feature/Import/CSV/BaseTransformerTest.php @@ -0,0 +1,145 @@ +withoutMiddleware( + ThrottleRequests::class + ); + config(['database.default' => config('ninja.db.default')]); + + // $this->faker = \Faker\Factory::create(); + + $this->makeTestData(); + + $this->withoutExceptionHandling(); + } + + public function testGetString() + { + $base_transformer = new BaseTransformer($this->company); + + $data = [ + 'key' => 'value' + ]; + + $field = 'key'; + + $this->assertEquals('value', $base_transformer->getString($data, $field)); + + } + + public function testGetCurrencyCode() + { + $base_transformer = new BaseTransformer($this->company); + + $code = ['client.currency_id' => "USD"]; + + $currency_id = $base_transformer->getCurrencyByCode($code); + + $this->assertEquals(1, $currency_id); + } + + public function testGetClient() + { + $base_transformer = new BaseTransformer($this->company); + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'id_number' => 'hit', + 'name' => 'magic ', + ]); + + $contact = ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + 'send_email' => true, + 'email' => 'test@gmail.com' + ]); + + + $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')); + + $this->assertEquals($client->id, $base_transformer->getClient(null, 'test@gmail.com')); + $this->assertNull($base_transformer->getClient('null', 'notest@gmail.com')); + + } + + + + + // public function testClientCsvImport() + // { + // $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); + // $hash = Str::random(32); + // $column_map = [ + // 1 => 'client.balance', + // 2 => 'client.paid_to_date', + // 0 => 'client.name', + // 19 => 'client.currency_id', + // 20 => 'client.public_notes', + // 21 => 'client.private_notes', + // 22 => 'contact.first_name', + // 23 => 'contact.last_name', + // ]; + + // $data = [ + // 'hash' => $hash, + // 'column_map' => [ 'client' => [ 'mapping' => $column_map ] ], + // 'skip_header' => true, + // 'import_type' => 'csv', + // ]; + + // $pre_import = Client::count(); + + // Cache::put( $hash . '-client', base64_encode( $csv ), 360 ); + + // CSVImport::dispatchNow( $data, $this->company ); + + // $this->assertGreaterThan( $pre_import, Client::count() ); + // } + +}