diff --git a/src/Api/Settings.php b/src/Api/Settings.php new file mode 100644 index 0000000..5cac848 --- /dev/null +++ b/src/Api/Settings.php @@ -0,0 +1,283 @@ + 'required', + 'data.name' => 'required', + ]); + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->post( + 'c/'.$this->company_id.'/settings/payment_methods', + $body + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new MethodEntity($settings); + } + public function mDetail( + int $payment_method_id, + ?array $additional_data = [] + ) { + $additional_data = $this->data($additional_data, [ + 'fields', 'fieldset', + ]); + + $response = $this->get( + 'c/'.$this->company_id.'/settings/payment_methods'.$payment_method_id, + $additional_data + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new MethodEntity($settings); + } + + public function mEdit( + int $payment_method_id, + ?array $body = [] + ){ + $validator = Validator::make($body, [ + 'data' => 'required', + 'data.name' => 'required', + ]); + + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->put( + 'c/' . $this->company_id . '/settings/payment_methods/' . $payment_method_id, + $body + ); + + if (!$response->success) { + return new Error($response->data); + } + + $payment_method_id = $response->data->data; + + return new MethodEntity($payment_method_id); + } + + public function mDelete( + int $payment_method_id + ) + { + $response = $this->destroy( + 'c/' . $this->company_id . '/settings/payment_methods/' . $payment_method_id + ); + + if (!$response->success) { + return new Error($response->data); + } + + return 'Payment method deleted'; + } + + //===Account=== + + public function aCreate( + array $body = [] + ) { + $validator = Validator::make($body, [ + 'data' => 'required', + 'data.name' => 'required', + ]); + + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->post( + 'c/'.$this->company_id.'/settings/payment_accounts', + $body + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new AccountEntity($settings); + } + + public function aDetail( + int $payment_account_id, + ?array $additional_data = [] + ) { + $additional_data = $this->data($additional_data, [ + 'fields', 'fieldset', + ]); + + $response = $this->get( + 'c/'.$this->company_id.'/settings/payment_accounts' . $payment_account_id, + $additional_data + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new AccountEntity($settings); + } + + public function aEdit( + int $payment_account_id, + ?array $body = [] + ){ + $validator = Validator::make($body, [ + 'data' => 'required', + 'data.name' => 'required', + ]); + + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->put( + 'c/' . $this->company_id . '/settings/payment_accounts/' . $payment_account_id, + $body + ); + + if (!$response->success) { + return new Error($response->data); + } + + $payment_account_id = $response->data->data; + + return new AccountEntity($payment_account_id); + } + + public function aDelete( + int $payment_account_id + ) + { + $response = $this->destroy( + 'c/' . $this->company_id . '/settings/payment_accounts/' . $payment_account_id + ); + + if (!$response->success) { + return new Error($response->data); + } + + return 'Payment method deleted'; + } + + //===Vat_Type=== + public function vtCreate( + array $body = [] + ) { + $validator = Validator::make($body, [ + 'data' => 'required', + 'data.name' => 'required', + ]); + + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->post( + 'c/'.$this->company_id.'/settings/vat_types', + $body + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new VatTypeEntity($settings); + } + + public function vtDetail( + int $vat_type_id, + ?array $additional_data = [] + ) { + $additional_data = $this->data($additional_data, [ + 'fields', 'fieldset', + ]); + + $response = $this->get( + 'c/'.$this->company_id.'/settings/vat_types'.$vat_type_id, + $additional_data + ); + + if (! $response->success) { + return new Error($response->data); + } + + $settings = $response->data->data; + + return new VatTypeEntity($settings); + } + + public function vtEdit( + int $vat_type_id, + ?array $body = [] + ){ + $validator = Validator::make($body, [ + 'data' => 'required', + 'data.name' => 'required', + ]); + + if ($validator->fails()) { + return $validator->errors(); + } + + $response = $this->put( + 'c/' . $this->company_id . '/settings/vat_types/' . $vat_type_id, + $body + ); + + if (!$response->success) { + return new Error($response->data); + } + + $vat_type_id = $response->data->data; + + return new VatTypeEntity($vat_type_id); + } + + public function vtDelete( + int $vat_type_id + ){ + $response = $this->destroy( + 'c/' . $this->company_id . '/settings/vat_types/' . $vat_type_id + ); + + if (!$response->success) { + return new Error($response->data); + } + + return 'Vat type deleted'; + } +} diff --git a/src/Entities/Settings/Settings.php b/src/Entities/Settings/Settings.php new file mode 100644 index 0000000..a3ac923 --- /dev/null +++ b/src/Entities/Settings/Settings.php @@ -0,0 +1,93 @@ +first_page_url)) { + return null; + } + + return $this->changePage($this->first_page_url); + } + + public function goToLastPage() + { + if (is_null($this->last_page_url)) { + return null; + } + + return $this->changePage($this->last_page_url); + } + + public function goToPrevPage() + { + if (! $this->hasPrevPage()) { + return null; + } + + return $this->changePage($this->prev_page_url); + } + + public function goToNextPage() + { + if (! $this->hasNextPage()) { + return null; + } + + return $this->changePage($this->next_page_url); + } + + // helpers + + private function changePage( + string $url + ) { + $query_params = $this->getQueryParams($url); + + $settings = new Settings(); + + return $settings->list( + $query_params + ); + } +} diff --git a/tests/Fake/ClientFakeResponse.php b/tests/Fake/ClientFakeResponse.php index 4071c97..31e372a 100644 --- a/tests/Fake/ClientFakeResponse.php +++ b/tests/Fake/ClientFakeResponse.php @@ -14,7 +14,7 @@ public function getClientsFakeList( 'data' => [ (new SingleClient())->getClientFakeDetail($params), (new SingleClient())->getClientFakeDetail($params), - ], + ] ], (new PaginationFakeResponse())->getPaginationFake($params) )); diff --git a/tests/Fake/Settings/DefaultPaymentAccount.php b/tests/Fake/Settings/DefaultPaymentAccount.php new file mode 100644 index 0000000..713bb5c --- /dev/null +++ b/tests/Fake/Settings/DefaultPaymentAccount.php @@ -0,0 +1,22 @@ + $this->value($params, 'id', 1), + 'name' => $this->value($params, 'name', 'fake_name'), + 'type' => $this->value($params, 'type', 'fake_type'), + 'iban' => $this->value($params, 'iban', 'fake_iban'), + 'sia' => $this->value($params, 'sia', 'fake_sia'), + 'cuc' => $this->value($params, 'sia', 'fake_cuc'), + 'virtual' => $this->value($params, 'is_default', true), + ]; + } +} diff --git a/tests/Fake/Settings/SinglePaymentMethod.php b/tests/Fake/Settings/SinglePaymentMethod.php new file mode 100644 index 0000000..963f30c --- /dev/null +++ b/tests/Fake/Settings/SinglePaymentMethod.php @@ -0,0 +1,28 @@ + $this->value($params, 'id', 1), + 'name' => $this->value($params, 'name', 'fake_name'), + 'type' => $this->value($params, 'type', 'fake_type'), + 'is_default' => $this->value($params, 'is_default', true), + 'default_payment_account' => (new DefaultPaymentAccount())->getDefaultPaymentDetail($params), + 'details' => $this->value($params, 'details', [ + 'title' => $this->value($params, 'id', 'fake_title'), + 'description' => $this->value($params, 'name', 'fake_description'), + ]), + 'bank_iban' => $this->value($params, 'bank_iban', 'fake_bank_iban'), + 'bank_name' => $this->value($params, 'bank_name', 'fake_bank_name'), + 'bank_beneficiary' => $this->value($params, 'bank_beneficiary', 'fake_bank_beneficiary'), + 'ei_payment_method' => $this->value($params, 'ei_payment_method', 'fake_ei_payment_method'), + ]; + } +} diff --git a/tests/Fake/SettingsFakeResponse.php b/tests/Fake/SettingsFakeResponse.php new file mode 100644 index 0000000..0fb0a6f --- /dev/null +++ b/tests/Fake/SettingsFakeResponse.php @@ -0,0 +1,19 @@ + (object) [ + (new SinglePaymentMethod())->getPaymentMethodFakeDetail($params), + ], + ] + ); + } +} diff --git a/tests/Feature/SettingsEntityTest.php b/tests/Feature/SettingsEntityTest.php new file mode 100644 index 0000000..46be392 --- /dev/null +++ b/tests/Feature/SettingsEntityTest.php @@ -0,0 +1,421 @@ + Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->mCreate([ + 'data' => [ + 'name' => 'Test', + 'default_payment_account' => [ + 'name' => 'test' + ] + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MethodEntity::class, $response); + } + + public function test_validation_error_on_create_payment_method() + { + Http::fake([ + 'settings/payment_methods' => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->mCreate([]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->mCreate([ + 'data' => [ + 'value' => '1', + ], + ]); + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + } + + public function test_detail_payment_method() + { + $payment_method_id = 1; + + Http::fake([ + 'settings/payment_methods'.$payment_method_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->mDetail($payment_method_id); + + $this->assertNotNull($response); + $this->assertInstanceOf(MethodEntity::class, $response); + } + + public function test_edit_payment_method() + { + $payment_method_id = 1; + $setting_name = 'Test Updated'; + + Http::fake([ + 'settings/payment_methods/' . $payment_method_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail([ + 'data' => [ + 'name' => 'Test', + 'default_payment_account' => [ + 'name' => 'test' + ] + ], + ]) + ), + ]); + + $settings = new Settings(); + $response = $settings->mEdit($payment_method_id, [ + 'data' => [ + 'name' => 'Test', + 'default_payment_account' => [ + 'name' => 'test' + ] + ], + 'data_s' => [ + 'name' => $setting_name, + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MethodEntity::class, $response); + } + + public function test_validation_error_on_edit_payment_method() + { + $payment_method_id = 1; + + Http::fake([ + 'settings/payment_methods'.$payment_method_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->mEdit($payment_method_id, []); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->mEdit($payment_method_id, [ + 'data' => [ + 'code' => 'test', + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data.name', $response->messages()); + } + + public function test_delete_payment_method() + { + $payment_method_id = 1; + + Http::fake([ + 'settings/payment_methods/' . $payment_method_id => Http::response(), + ]); + + $settings = new Settings(); + $response = $settings->mDelete($payment_method_id); + + $this->assertEquals('Payment method deleted', $response); + } + + //Payment Account + + public function test_create_payment_account() + { + Http::fake([ + 'settings/payment_accounts' => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->aCreate([ + 'data' => [ + 'name' => 'Test', + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(AccountEntity::class, $response); + } + + public function test_validation_error_on_create_payment_account() + { + Http::fake([ + 'settings/payment_accounts' => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->aCreate([]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->aCreate([ + 'data' => [ + 'value' => '1', + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + } + + public function test_detail_payment_account() + { + $payment_account_id = 1; + + Http::fake([ + 'settings/payment_accounts'.$payment_account_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->aDetail($payment_account_id); + + $this->assertNotNull($response); + $this->assertInstanceOf(AccountEntity::class, $response); + } + + + public function test_edit_payment_account() + { + $payment_account_id = 1; + $setting_name = 'Test Updated'; + + Http::fake([ + 'settings/payment_accounts/' . $payment_account_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail([ + 'name' => $setting_name, + ]) + ), + ]); + + $settings = new Settings(); + $response = $settings->aEdit($payment_account_id, [ + 'data' => [ + 'name' => $setting_name, + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(AccountEntity::class, $response); + } + + public function test_validation_error_on_edit_payment_account() + { + $payment_account_id = 1; + + Http::fake([ + 'settings/payment_accounts'.$payment_account_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->mEdit($payment_account_id, []); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->mEdit($payment_account_id, [ + 'data' => [ + 'code' => 'test', + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data.name', $response->messages()); + } + + public function test_delete_payment_account() + { + $payment_account_id = 1; + + Http::fake([ + 'settings/payment_accounts/' . $payment_account_id => Http::response(), + ]); + + $settings = new Settings(); + $response = $settings->aDelete($payment_account_id); + + $this->assertEquals('Payment method deleted', $response); + } + + //Vat_Type + + public function test_create_vat_type() + { + Http::fake([ + 'settings/vat_types' => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->vtCreate([ + 'data' => [ + 'value' => 1, + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + } + + public function test_validation_error_on_create_vat_type() + { + $settings = new Settings(); + $response = $settings->vtCreate([]); + + Http::fake([ + 'settings/vat_types' => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->vtCreate([ + 'data' => [ + 'value' => 1, + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + } + + public function test_detail_vat_type() + { + $vat_type_id = 1; + + Http::fake([ + 'settings/vat_types'.$vat_type_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->vtDetail($vat_type_id); + + $this->assertNotNull($response); + $this->assertInstanceOf(VatTypeEntity::class, $response); + } + + + public function test_edit_vat_type() + { + $vat_type_id = 1; + $setting_name = 'Test Updated'; + + Http::fake([ + 'settings/vat_types/' . $vat_type_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail([ + 'name' => $setting_name, + ]) + ), + ]); + + $settings = new Settings(); + $response = $settings->vtEdit($vat_type_id, [ + 'data' => [ + 'name' => $setting_name, + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(VatTypeEntity::class, $response); + } + + public function test_validation_error_on_edit_vat_type() + { + $vat_type_id = 1; + + Http::fake([ + 'settings/vat_types'.$vat_type_id => Http::response( + (new SettingsFakeResponse())->getSettingsFakeDetail() + ), + ]); + + $settings = new Settings(); + $response = $settings->vtEdit($vat_type_id, []); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data', $response->messages()); + + $settings = new Settings(); + $response = $settings->vtEdit($vat_type_id, [ + 'data' => [ + 'code' => 'test', + ], + ]); + + $this->assertNotNull($response); + $this->assertInstanceOf(MessageBag::class, $response); + $this->assertArrayHasKey('data.name', $response->messages()); + } + + public function test_delete_vat_type() + { + $vat_type_id = 1; + + Http::fake([ + 'settings/vat_types/' . $vat_type_id => Http::response(), + ]); + + $settings = new Settings(); + $response = $settings->vtDelete($vat_type_id); + + $this->assertEquals('Vat type deleted', $response); + } +}