From ba756c6040bf896a8223a5a3117c4e48e7116a5d Mon Sep 17 00:00:00 2001 From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Date: Mon, 23 Dec 2024 15:35:07 +0000 Subject: [PATCH] Update test scripts for improved coverage and precision settings. Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --- composer.json | 8 ++-- tests/ExtensionTest.php | 57 +++++++++++++++++++++++++ tests/Models/BannerTest.php | 43 +++++++++++++++++++ tests/Models/CaptchaSettingsTest.php | 14 +++++++ tests/Models/MailchimpSettingsTest.php | 58 ++++++++++++++++++++++++++ tests/Models/SubscriberTest.php | 17 +++++++- 6 files changed, 192 insertions(+), 5 deletions(-) create mode 100644 tests/Models/CaptchaSettingsTest.php create mode 100644 tests/Models/MailchimpSettingsTest.php diff --git a/composer.json b/composer.json index a649d51..2472608 100644 --- a/composer.json +++ b/composer.json @@ -10,15 +10,15 @@ } ], "require": { - "tastyigniter/core": "^v4.0@beta", - "tastyigniter/ti-ext-cart": "^v4.0@beta", + "tastyigniter/core": "^v4.0@beta || ^v4.0@dev", + "tastyigniter/ti-ext-cart": "^v4.0@beta || ^v4.0@dev", "drewm/mailchimp-api": "^2.4" }, "require-dev": { "laravel/pint": "^1.2", "larastan/larastan": "^2.4.0", "sampoyigi/testbench": "dev-main as 1.0", - "pestphp/pest-plugin-laravel": "^2.0" + "pestphp/pest-plugin-laravel": "^3.0" }, "autoload": { "psr-4": { @@ -56,5 +56,5 @@ }, "sort-packages": true }, - "minimum-stability": "beta" + "minimum-stability": "dev" } diff --git a/tests/ExtensionTest.php b/tests/ExtensionTest.php index 954578a..00cd590 100644 --- a/tests/ExtensionTest.php +++ b/tests/ExtensionTest.php @@ -2,7 +2,40 @@ namespace Igniter\Frontend\Tests; +use DrewM\MailChimp\MailChimp; +use Igniter\Frontend\Classes\ReCaptcha; use Igniter\Frontend\Extension; +use Igniter\Frontend\Models\MailchimpSettings; +use Illuminate\Support\Facades\Validator; + +it('creates mailchimp service correctly', function() { + MailchimpSettings::set([ + 'api_key' => 'some-api-key', + 'list_id' => 'some_list_id', + ]); + + $service = resolve(MailChimp::class); + + expect($service)->toBeInstanceOf(MailChimp::class); +}); + +it('creates recaptcha service correctly', function() { + $service = resolve('recaptcha'); + + expect($service)->toBeInstanceOf(ReCaptcha::class); +}); + +it('registers recaptcha validation rule correctly', function() { + $recaptcha = mock(ReCaptcha::class); + $recaptcha->shouldReceive('verifyResponse')->andReturnTrue()->once(); + app()->instance('recaptcha', $recaptcha); + + $validated = Validator::make(['g-recaptcha-response' => 'token'], [ + 'g-recaptcha-response' => 'required|recaptcha', + ])->passes(); + + expect($validated)->toBeTrue(); +}); it('registers permissions', function() { $extension = new Extension(app()); @@ -16,6 +49,30 @@ ]); }); +it('registers system settings', function() { + $extension = new Extension(app()); + + $result = $extension->registerSettings(); + + expect($result)->toHaveKey('captchasettings') + ->and($result['captchasettings'])->toMatchArray([ + 'label' => 'reCaptcha Settings', + 'description' => 'Manage google reCAPTCHA settings.', + 'icon' => 'fa fa-gear', + 'model' => \Igniter\Frontend\Models\CaptchaSettings::class, + 'permissions' => ['Igniter.FrontEnd.ManageSettings'], + ]) + ->and($result)->toHaveKey('mailchimpsettings') + ->and($result['mailchimpsettings'])->toMatchArray([ + 'label' => 'Mailchimp Settings', + 'description' => 'Manage Mailchimp API settings.', + 'icon' => 'fa fa-gear', + 'model' => \Igniter\Frontend\Models\MailchimpSettings::class, + 'permissions' => ['Igniter.FrontEnd.ManageSettings'], + ]); +}); + + it('registers navigation', function() { $extension = new Extension(app()); diff --git a/tests/Models/BannerTest.php b/tests/Models/BannerTest.php index 10c1a33..048f085 100644 --- a/tests/Models/BannerTest.php +++ b/tests/Models/BannerTest.php @@ -11,6 +11,14 @@ expect($banner->type_label)->toBe('Image'); }); +it('returns dropdown options for language id', function() { + $banner = Banner::create(['name' => 'Banner Name', 'type' => 'image']); + + $result = $banner->getLanguageIdOptions(); + + expect($result->all())->toContain($banner->name); +}); + it('returns image thumb', function() { $banner = Banner::make(['image_code' => serialize(['path' => 'banner.jpg'])]); @@ -19,12 +27,47 @@ ]))->toContain('banner.jpg'); }); +it('returns image thumb when image_code is empty', function() { + $banner = Banner::make(['image_code' => '']); + + expect($banner->getImageThumb(['no_photo' => 'no_photo.jpg']))->toContain('no_photo.jpg'); +}); + it('returns image thumb with no photo', function() { $banner = Banner::make(['image_code' => serialize(['path' => ''])]); expect($banner->getImageThumb(['no_photo' => 'no_photo.jpg']))->toContain('no_photo.jpg'); }); +it('returns empty array when image_code is empty', function() { + $banner = Banner::make(['image_code' => '']); + + $result = $banner->getCarouselThumbs(); + + expect($result)->toBe([]); +}); + +it('returns empty array when image_code paths is not an array', function() { + $banner = Banner::make(['image_code' => serialize(['paths' => 'not_an_array'])]); + + $result = $banner->getCarouselThumbs(); + + expect($result)->toBe([]); +}); + +it('returns array of thumbnails when image_code paths is an array', function() { + $banner = Banner::make(['image_code' => serialize(['paths' => ['path/to/image1.jpg', 'path/to/image2.jpg']])]); + + $result = $banner->getCarouselThumbs(); + + expect($result[0]['name'])->toBe('image1.jpg') + ->and($result[0]['path'])->toBe('path/to/image1.jpg') + ->and($result[0]['url'])->not->toBeEmpty() + ->and($result[1]['name'])->toBe('image2.jpg') + ->and($result[1]['path'])->toBe('path/to/image2.jpg') + ->and($result[1]['url'])->not->toBeEmpty(); +}); + it('configures banner model correctly', function() { $banner = new Banner; diff --git a/tests/Models/CaptchaSettingsTest.php b/tests/Models/CaptchaSettingsTest.php new file mode 100644 index 0000000..04006a3 --- /dev/null +++ b/tests/Models/CaptchaSettingsTest.php @@ -0,0 +1,14 @@ +implement)->toContain(SettingsModel::class) + ->and($model->settingsCode)->toEqual('igniter_frontend_captchasettings') + ->and($model->settingsFieldsConfig)->toEqual('captchasettings'); +}); diff --git a/tests/Models/MailchimpSettingsTest.php b/tests/Models/MailchimpSettingsTest.php new file mode 100644 index 0000000..56b6537 --- /dev/null +++ b/tests/Models/MailchimpSettingsTest.php @@ -0,0 +1,58 @@ + 'some-api-key', + 'list_id' => 'some-list-id', + ]); + + $result = MailchimpSettings::isConfigured(); + + expect($result)->toBeTrue(); +}); + +it('returns false when api_key is not configured', function() { + MailchimpSettings::set([ + 'api_key' => '', + 'list_id' => 'some-list-id', + ]); + + $result = MailchimpSettings::isConfigured(); + + expect($result)->toBeFalse(); +}); + +it('returns false when list_id is not configured', function() { + MailchimpSettings::set([ + 'api_key' => 'some-api-key', + 'list_id' => '', + ]); + + $result = MailchimpSettings::isConfigured(); + + expect($result)->toBeFalse(); +}); + +it('returns false when both api_key and list_id are not configured', function() { + MailchimpSettings::set([ + 'api_key' => '', + 'list_id' => '', + ]); + + $result = MailchimpSettings::isConfigured(); + + expect($result)->toBeFalse(); +}); + +it('configures captcha settings model correctly', function() { + $model = new MailchimpSettings; + + expect($model->implement)->toContain(SettingsModel::class) + ->and($model->settingsCode)->toEqual('igniter_frontend_mailchimpsettings') + ->and($model->settingsFieldsConfig)->toEqual('mailchimpsettings'); +}); diff --git a/tests/Models/SubscriberTest.php b/tests/Models/SubscriberTest.php index dd92bd8..6fd9542 100644 --- a/tests/Models/SubscriberTest.php +++ b/tests/Models/SubscriberTest.php @@ -2,10 +2,11 @@ namespace Igniter\Frontend\Tests\Models; +use Igniter\Frontend\Models\MailchimpSettings; use Igniter\Frontend\Models\Subscriber; use Illuminate\Support\Facades\Event; -it('subscribes an email', function() { +it('subscribes an email to the database', function() { Event::fake(); $email = 'email@example.com'; @@ -15,6 +16,20 @@ Event::assertDispatched('igniter.frontend.subscribed'); }); +it('subscribes to Mailchimp when listId is provided and configured', function() { + Event::fake(); + MailchimpSettings::set([ + 'api_key' => 'some-api-key', + 'list_id' => 'some-list-id', + ]); + + $email = 'email@example.com'; + $listId = 'list-id'; + Subscriber::subscribe($email, $listId); + + Event::assertDispatched('igniter.frontend.subscribed'); +}); + it('configures subscriber model correctly', function() { $slider = new Subscriber();