From 0a88645d7ff97faccba2662e1c3158a340ff940c Mon Sep 17 00:00:00 2001 From: Uziel Bueno Date: Tue, 24 Jul 2018 00:07:45 -0500 Subject: [PATCH 1/2] WIP --- composer.json | 6 +- phpunit.xml | 1 - readme.md | 95 +++-- src/SweetAlert/SweetAlertNotifier.php | 167 ++++++--- src/SweetAlert/SweetAlertServiceProvider.php | 40 +- src/views/alert.blade.php | 8 +- src/views/alert.twig | 12 +- tests/SweetAlertNotifierTest.php | 366 +++++++++++-------- 8 files changed, 421 insertions(+), 274 deletions(-) diff --git a/composer.json b/composer.json index 90a9739..df7528f 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": ">=5.4.0", + "php": ">=7.0", "illuminate/support": "~5.0", "illuminate/session": "~5.0" }, @@ -19,11 +19,11 @@ "UxWeb\\SweetAlert\\": "src/SweetAlert/" }, "files": [ - "src/SweetAlert/functions.php" + "src/SweetAlert/functions.php" ] }, "require-dev": { - "phpunit/phpunit": "^6.0", + "phpunit/phpunit": "^7.0", "mockery/mockery": "0.9.*" }, "extra": { diff --git a/phpunit.xml b/phpunit.xml index 494e211..f5a78f5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,7 +9,6 @@ processIsolation="false" stopOnError="false" stopOnFailure="false" - syntaxCheck="true" verbose="true" > diff --git a/readme.md b/readme.md index ccc5774..74d978c 100644 --- a/readme.md +++ b/readme.md @@ -2,15 +2,14 @@ ![A success alert](http://i.imgur.com/1XySJiz.png) - [![StyleCI](https://styleci.io/repos/38935942/shield)](https://styleci.io/repos/38935942) ## Installation First, pull in the package through Composer. -``` - composer require uxweb/sweet-alert +```bash +composer require uxweb/sweet-alert ``` If using laravel < 5.5 include the service provider and alias within `config/app.php`. @@ -25,27 +24,34 @@ If using laravel < 5.5 include the service provider and alias within `config/app ]; ``` -> Note that this package works only by using the [BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT"](http://t4t5.github.io/sweetalert/). +> Note that this package works only by using the [BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT"](https://sweetalert.js.org/). Finally, install the Sweet Alert Javascript library through yarn or npm Install using Yarn: + +```bash +yarn add sweetalert@^2.0 --dev ``` - yarn add sweetalert:1.1.3 --dev -``` + Install using Npm: -``` - npm install sweetalert:1.1.3 --save-dev + +```bash +npm install sweetalert@^2.0 --save-dev ``` +> Note that this version of the package only works with sweetalert v2. If you need v1 please install the last release of version 1 of the PHP package and js library. + ## Usage ### Using the Facade First import the Alert facade in your controller. + ```php use Alert; ``` + Within your controllers, before you perform a redirect... ```php @@ -58,6 +64,7 @@ public function store() ``` Here are some examples on how you can use the facade: + ```php Alert::message('Message', 'Optional Title'); @@ -124,6 +131,7 @@ For a general information alert, just do: `alert('Some message');` (same as `ale ### Using the Middleware #### Middleware Groups + First register the middleware in web middleware groups by simply adding the middleware class `UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class` into the $middlewareGroups of your app/Http/Kernel.php class: ```php @@ -142,7 +150,8 @@ First register the middleware in web middleware groups by simply adding the midd > Make sure you register the middleware within the 'web' group only. -#### Route middleware +#### Route Middleware + Or if you would like to assign the middleware to specific routes only, you should add the middleware to `$routeMiddleware` in `app/Http/Kernel.php` file: ```php @@ -168,6 +177,7 @@ return redirect()->back()->with('errors', 'Profile updated!'); > **NOTE**: When using the middleware it will make an alert to display if it detects any of the following keys flashed into the session: `errors`, `success`, `warning`, `info`, `message`, `basic`. ### The View + Finally, to display the alert in the browser, you may use (or modify) the view that is included with this package. Simply include it in your layout view: ```html @@ -196,61 +206,73 @@ Finally, to display the alert in the browser, you may use (or modify) the view t > **REMEMBER**: Always include the .css and .js files from the sweet-alert library. ### Final Considerations + By default, all alerts will dismiss after a sensible default number of seconds. But not to worry, if you need to specify a different time you can: ```php - // -> Remember!, the number is set in milliseconds - alert('Hello World!')->autoclose(3000); +// -> Remember!, the number is set in milliseconds +alert('Hello World!')->autoclose(3000); ``` Also, if you need the alert to be persistent on the page until the user dismiss it by pressing the alert confirmation button: ```php - // -> The text will appear in the button - alert('Hello World!')->persistent("Close this"); +// -> The text will appear in the button +alert('Hello World!')->persistent("Close this"); ``` You can render html in your message with the html() method like this: ```php - // -> html will be evaluated - alert('Click me')->html()->persistent("No, thanks"); +// -> html will be evaluated +alert('Click me')->html()->persistent("No, thanks"); ``` ## Customize -If you need to customize the alert message partial, run: +### Config + +If you need to customize the default configuration options for this package just export the configuration file: + +```bash +php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=config +``` + +A `sweet-alert.php` configuration file will be published to your `config` directory. By now, the only configuration that can be changed is the timer for all autoclose alerts. + +### View + +If you need to customize the included alert message view, run: ```bash - php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" +php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=views ``` The package view is located in the `resources/views/vendor/sweet/` directory. You can customize this view to fit your needs. -A `sweet-alert.php` configuration file will be published to your `config` directory as well, this will allow you to set the default timer for all autoclose alerts. - -### Configuration Options +#### Configuration Options You have access to the following configuration options to build a custom view: ```php - Session::get('sweet_alert.text') - Session::get('sweet_alert.type') - Session::get('sweet_alert.title') - Session::get('sweet_alert.confirmButtonText') - Session::get('sweet_alert.showConfirmButton') - Session::get('sweet_alert.allowOutsideClick') - Session::get('sweet_alert.timer') +Session::get('sweet_alert.text') +Session::get('sweet_alert.title') +Session::get('sweet_alert.icon') +Session::get('sweet_alert.closeOnClickOutside') +Session::get('sweet_alert.buttons') +Session::get('sweet_alert.timer') ``` -Please check the CONFIGURATION section in the [website](http://t4t5.github.io/sweetalert/) for all other options available. +Please check the CONFIGURATION section in the [website](https://sweetalert.js.org/docs/#configuration) for all other options available. ### Default View +The `sweet_alert.alert` session key contains a JSON configuration object to pass it directly to Sweet Alert. + ```html @if (Session::has('sweet_alert.alert')) @endif diff --git a/src/views/alert.twig b/src/views/alert.twig index fe2a131..ada65b3 100644 --- a/src/views/alert.twig +++ b/src/views/alert.twig @@ -1,5 +1,11 @@ {% if session_has('sweet_alert.alert') %} - + {% endif %} diff --git a/tests/SweetAlertNotifierTest.php b/tests/SweetAlertNotifierTest.php index 2150c9d..ca7b144 100644 --- a/tests/SweetAlertNotifierTest.php +++ b/tests/SweetAlertNotifierTest.php @@ -7,6 +7,98 @@ class SweetAlertNotifierTest extends TestCase { + /** @test */ + function text_is_empty_by_default() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message(); + + $this->assertEquals('', $notifier->getConfig('text')); + } + + /** @test */ + function default_timer_is_1800_milliseconds() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Good News!'); + + $this->assertEquals(1800, $notifier->getConfig('timer')); + } + + /** @test */ + function buttons_config_is_false_by_default() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Good News!'); + + $buttonsConfig = [ + 'confirm' => false, + 'cancel' => false, + ]; + $this->assertEquals($buttonsConfig, $notifier->getConfig('buttons')); + } + + /** @test */ + function first_parameter_of_alert_message_is_the_config_text() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Hello World!'); + + $this->assertEquals('Hello World!', $notifier->getConfig('text')); + } + + /** @test */ + function title_key_is_not_present_in_config_when_alert_title_is_not_set() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Hello World!'); + + $this->assertArrayNotHasKey('title', $notifier->getConfig()); + } + + /** @test */ + function second_parameter_of_alert_message_is_the_config_title() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Hello World!', 'This is the title'); + + $this->assertEquals('This is the title', $notifier->getConfig('title')); + } + + /** @test */ + function third_parameter_of_alert_message_is_the_config_icon() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Hello World!', 'This is the title', 'info'); + + $this->assertEquals('info', $notifier->getConfig('icon')); + } + + /** @test */ + function icon_key_is_not_present_in_config_when_alert_icon_is_not_set() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->message('Hello World!', 'This is the title'); + + $this->assertArrayNotHasKey('icon', $notifier->getConfig()); + } + /** @test */ public function it_flashes_config_for_a_basic_alert() { @@ -16,262 +108,216 @@ public function it_flashes_config_for_a_basic_alert() $notifier->basic('Basic Alert!', 'Alert'); $expectedConfig = [ - 'timer' => 1800, - 'title' => 'Alert', 'text' => 'Basic Alert!', - 'showConfirmButton' => false, + 'title' => 'Alert', ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldHaveReceived('flash')->with('sweet_alert.timer', $expectedConfig['timer'])->once(); $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once(); $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text'])->once(); - $session->shouldHaveReceived('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton'])->once(); - $session->shouldHaveReceived('flash')->with('sweet_alert.alert', $expectedJsonConfig)->once(); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $session->shouldHaveReceived('flash')->with('sweet_alert.alert', nonEmptyString())->once(); + $this->assertArraySubset($expectedConfig, $notifier->getConfig()); } /** @test */ public function it_flashes_config_for_a_info_alert() { - $session = m::mock(SessionStore::class); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'timer' => 1800, - 'title' => 'Alert', - 'text' => 'Info Alert!', - 'showConfirmButton' => false, - 'type' => 'info', - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); - $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.type', $expectedConfig['type']); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); $notifier->info('Info Alert!', 'Alert'); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $expectedConfig = [ + 'text' => 'Info Alert!', + 'title' => 'Alert', + 'icon' => 'info', + ]; + $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once(); + $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text'])->once(); + $session->shouldHaveReceived('flash')->with('sweet_alert.icon', $expectedConfig['icon'])->once(); + $session->shouldHaveReceived('flash')->with('sweet_alert.alert', nonEmptyString())->once(); + $this->assertArraySubset($expectedConfig, $notifier->getConfig()); } /** @test */ public function it_flashes_config_for_a_success_alert() { - $session = m::mock(SessionStore::class); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); + + $notifier->success('Well Done!', 'Success!'); + $expectedConfig = [ - 'timer' => 1800, 'title' => 'Success!', 'text' => 'Well Done!', - 'showConfirmButton' => false, - 'type' => 'success', + 'icon' => 'success', ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.type', $expectedConfig['type']); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); - - $notifier->success('Well Done!', 'Success!'); - - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $session->shouldReceive('flash')->with('sweet_alert.icon', $expectedConfig['icon']); + $session->shouldHaveReceived('flash')->with('sweet_alert.alert', nonEmptyString())->once(); + $this->assertArraySubset($expectedConfig, $notifier->getConfig()); } /** @test */ public function it_flashes_config_for_a_warning_alert() { - $session = m::mock(SessionStore::class); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); + + $notifier->warning('Hey cowboy!', 'Watch Out!'); + $expectedConfig = [ - 'timer' => 1800, 'title' => 'Watch Out!', 'text' => 'Hey cowboy!', - 'showConfirmButton' => false, - 'type' => 'warning', + 'icon' => 'warning', ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.type', $expectedConfig['type']); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); - - $notifier->warning('Hey cowboy!', 'Watch Out!'); - - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $session->shouldReceive('flash')->with('sweet_alert.icon', $expectedConfig['icon']); + $session->shouldHaveReceived('flash')->with('sweet_alert.alert', nonEmptyString())->once(); + $this->assertArraySubset($expectedConfig, $notifier->getConfig()); } /** @test */ public function it_flashes_config_for_a_error_alert() { - $session = m::mock(SessionStore::class); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); + + $notifier->error('Something wrong happened!', 'Whoops!'); + $expectedConfig = [ - 'timer' => 1800, 'title' => 'Whoops!', 'text' => 'Something wrong happened!', - 'showConfirmButton' => false, - 'type' => 'error', + 'icon' => 'error', ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); - $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); - $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.type', $expectedConfig['type']); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); - - $notifier->error('Something wrong happened!', 'Whoops!'); - - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title']); + $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text']); + $session->shouldHaveReceived('flash')->with('sweet_alert.icon', $expectedConfig['icon']); + $session->shouldHaveReceived('flash')->with('sweet_alert.alert', nonEmptyString())->once(); + $this->assertArraySubset($expectedConfig, $notifier->getConfig()); } /** @test */ - public function it_flashes_timer_config_using_an_autoclose_alert() + public function autoclose_can_be_customized_for_an_alert_message() { - $session = m::mock(SessionStore::class); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'timer' => 2000, - 'title' => 'Alert', - 'text' => 'Hello!', - 'showConfirmButton' => false, - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldReceive('flash')->with('sweet_alert.timer', 1800); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); - $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); - $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.alert', json_encode(array_merge($expectedConfig, ['timer' => 1800]))); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); $notifier->message('Hello!', 'Alert')->autoclose(2000); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $this->assertEquals(2000, $notifier->getConfig('timer')); + $session->shouldHaveReceived('flash')->with('sweet_alert.timer', 2000); } + // /** @test */ + // public function it_flashes_the_message_as_the_alert_title_if_no_title_is_passed_for_json_config() + // { + // $this->('Review this functionality to have the same api as in v1'); + // $session = m::mock(SessionStore::class); + // $notifier = new SweetAlertNotifier($session); + // $expectedConfig = [ + // 'text' => 'This should be the title!', + // ]; + // $expectedJsonConfig = json_encode([ + // 'title' => 'This should be the title!', + // ]); + // $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); + + // $notifier->message('This should be the title!'); + // } + /** @test */ - public function it_flashes_the_message_as_the_alert_title_if_no_title_is_passed_for_json_config() + public function timer_option_is_not_present_in_config_when_using_a_persistent_alert() { $session = m::mock(SessionStore::class); + $session->shouldReceive('flash')->atLeast(1); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'timer' => 1800, - 'title' => '', - 'text' => 'This should be the title!', - 'showConfirmButton' => false, - ]; - $expectedJsonConfig = json_encode([ - 'timer' => 1800, - 'title' => 'This should be the title!', - 'showConfirmButton' => false, - ]); - $session->shouldReceive('flash')->with('sweet_alert.timer', $expectedConfig['timer']); - $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); - $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); - $session->shouldReceive('flash')->with('sweet_alert.showConfirmButton', $expectedConfig['showConfirmButton']); - $session->shouldReceive('flash')->with('sweet_alert.alert', $expectedJsonConfig); - $notifier->message('This should be the title!'); + $notifier->message('Please, read with care!', 'Alert')->persistent('Got it!'); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $this->assertArrayNotHasKey('timer', $notifier->getConfig()); } /** @test */ - public function it_removes_the_timer_option_from_config_when_using_a_persistent_alert() + public function it_will_add_the_content_option_to_config_when_using_an_html_alert() { $session = m::mock(SessionStore::class); $session->shouldReceive('flash')->atLeast(1); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'title' => 'Alert', - 'text' => 'Please, read with care!', - 'showConfirmButton' => true, - 'confirmButtonText' => 'Got it!', - 'allowOutsideClick' => false, - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $notifier->message('Please, read with care!', 'Alert')->persistent('Got it!'); + $notifier->message('This should be bold!', 'Alert')->html(); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $this->assertEquals('This should be bold!', $notifier->getConfig('content')); } /** @test */ - public function it_will_add_the_html_option_to_config_when_using_an_html_alert() + public function allows_to_configure_a_confirm_button_for_an_alert() { $session = m::mock(SessionStore::class); $session->shouldReceive('flash')->atLeast(1); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'timer' => 1800, - 'title' => 'Alert', - 'text' => 'This should be bold!', - 'showConfirmButton' => false, - 'html' => true, - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $notifier->message('This should be bold!', 'Alert')->html(); + $notifier->basic('Basic Alert!', 'Alert')->confirmButton('ok!'); + $this->assertArraySubset(['text' => 'ok!', 'visible' => true], $notifier->getConfig('buttons')['confirm']); + $this->assertFalse($notifier->getConfig('closeOnClickOutside')); + } - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + /** @test */ + public function allows_to_configure_a_cancel_button_for_an_alert() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->basic('Basic Alert!', 'Alert')->cancelButton('Cancel!'); + + $this->assertArraySubset(['text' => 'Cancel!', 'visible' => true], $notifier->getConfig('buttons')['cancel']); + $this->assertFalse($notifier->getConfig('closeOnClickOutside')); } /** @test */ - public function it_show_a_confirm_button_with_custom_text() + function close_on_click_outside_config_can_be_enabled() { - $session = m::mock(SessionStore::class); - $session->shouldReceive('flash')->atLeast(1); + $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); - $expectedConfig = [ - 'title' => 'Alert', - 'text' => 'Basic Alert!', - 'showConfirmButton' => true, - 'confirmButtonText' => 'ok!', - 'allowOutsideClick' => false, - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $notifier->basic('Basic Alert!', 'Alert')->confirmButton('ok!'); + $notifier->basic('Basic Alert!', 'Alert')->closeOnClickOutside(); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $this->assertTrue($notifier->getConfig('closeOnClickOutside')); } /** @test */ - public function it_show_a_cancel_button_with_custom_text() + function close_on_click_outside_config_can_be_disabled() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); - $notifier->basic('Basic Alert!', 'Alert')->cancelButton('Cancel!'); + $notifier->basic('Basic Alert!', 'Alert')->closeOnClickOutside(false); - $expectedConfig = [ - 'title' => 'Alert', - 'text' => 'Basic Alert!', - 'showConfirmButton' => false, - 'showCancelButton' => true, - 'cancelButtonText' => 'Cancel!', - 'allowOutsideClick' => false, - ]; - $expectedJsonConfig = json_encode($expectedConfig); - $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title']); - $this->assertEquals($expectedConfig, $notifier->getConfig()); - $this->assertEquals($expectedJsonConfig, $notifier->getJsonConfig()); + $this->assertFalse($notifier->getConfig('closeOnClickOutside')); + } + + /** @test */ + function additional_buttons_can_be_added() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->basic('Basic Alert!', 'Alert')->addButton('pay', 'Confirm Payment'); + + $this->assertArraySubset(['text' => 'Confirm Payment', 'visible' => true], $notifier->getConfig('buttons')['pay']); + $this->assertFalse($notifier->getConfig('closeOnClickOutside')); + } + + /** @test */ + function additional_config_can_be_added_to_configure_alert_message() + { + $session = m::spy(SessionStore::class); + $notifier = new SweetAlertNotifier($session); + + $notifier->basic('Basic Alert!', 'Alert')->setConfig(['dangerMode' => true]); + + $this->assertTrue($notifier->getConfig('dangerMode')); + $session->shouldHaveReceived('flash')->with('sweet_alert.dangerMode', true); } public function tearDown() From 086f205af6dc4a9ce59468c408d95dec73922e6d Mon Sep 17 00:00:00 2001 From: Uziel Bueno Date: Tue, 24 Jul 2018 05:09:39 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/SweetAlert/SweetAlertNotifier.php | 20 ++++----- src/SweetAlert/SweetAlertServiceProvider.php | 6 +-- tests/SweetAlertNotifierTest.php | 44 ++++++++++---------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/SweetAlert/SweetAlertNotifier.php b/src/SweetAlert/SweetAlertNotifier.php index 37b69d7..93bd357 100644 --- a/src/SweetAlert/SweetAlertNotifier.php +++ b/src/SweetAlert/SweetAlertNotifier.php @@ -23,10 +23,10 @@ class SweetAlertNotifier protected $config; protected $defaultButtonConfig = [ - 'text' => '', - 'value' => null, - 'visible' => false, - 'className' => '', + 'text' => '', + 'value' => null, + 'visible' => false, + 'className' => '', 'closeModal' => true, ]; @@ -50,10 +50,10 @@ public function __construct(SessionStore $session) protected function setDefaultConfig() { $this->config = [ - 'timer' => config('sweet-alert.autoclose', self::TIMER_MILLISECONDS), - 'text' => '', + 'timer' => config('sweet-alert.autoclose', self::TIMER_MILLISECONDS), + 'text' => '', 'buttons' => [ - 'cancel' => false, + 'cancel' => false, 'confirm' => false, ], ]; @@ -198,7 +198,7 @@ public function confirmButton($buttonText = 'OK', $overrides = []) * Add a cancel button to the alert. * * @param string $buttonText - * @param array $overrides + * @param array $overrides * * @return \UxWeb\SweetAlert\SweetAlertNotifier $this */ @@ -214,7 +214,7 @@ public function cancelButton($buttonText = 'Cancel', $overrides = []) * * @param string $key * @param string $buttonText - * @param array $overrides + * @param array $overrides * * @return \UxWeb\SweetAlert\SweetAlertNotifier $this */ @@ -223,7 +223,7 @@ public function addButton($key, $buttonText, $overrides = []) $this->config['buttons'][$key] = array_merge( $this->defaultButtonConfig, [ - 'text' => $buttonText, + 'text' => $buttonText, 'visible' => true, ], $overrides diff --git a/src/SweetAlert/SweetAlertServiceProvider.php b/src/SweetAlert/SweetAlertServiceProvider.php index bc7e36f..f03b013 100644 --- a/src/SweetAlert/SweetAlertServiceProvider.php +++ b/src/SweetAlert/SweetAlertServiceProvider.php @@ -13,14 +13,14 @@ class SweetAlertServiceProvider extends ServiceProvider */ public function boot() { - $this->loadViewsFrom(__DIR__ . '/../views', 'sweet'); + $this->loadViewsFrom(__DIR__.'/../views', 'sweet'); $this->publishes([ - __DIR__ . '/../config/sweet-alert.php' => config_path('sweet-alert.php'), + __DIR__.'/../config/sweet-alert.php' => config_path('sweet-alert.php'), ], 'config'); $this->publishes([ - __DIR__ . '/../views' => base_path('resources/views/vendor/sweet'), + __DIR__.'/../views' => base_path('resources/views/vendor/sweet'), ], 'views'); } diff --git a/tests/SweetAlertNotifierTest.php b/tests/SweetAlertNotifierTest.php index ca7b144..71f43dd 100644 --- a/tests/SweetAlertNotifierTest.php +++ b/tests/SweetAlertNotifierTest.php @@ -8,7 +8,7 @@ class SweetAlertNotifierTest extends TestCase { /** @test */ - function text_is_empty_by_default() + public function text_is_empty_by_default() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -19,7 +19,7 @@ function text_is_empty_by_default() } /** @test */ - function default_timer_is_1800_milliseconds() + public function default_timer_is_1800_milliseconds() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -30,7 +30,7 @@ function default_timer_is_1800_milliseconds() } /** @test */ - function buttons_config_is_false_by_default() + public function buttons_config_is_false_by_default() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -39,13 +39,13 @@ function buttons_config_is_false_by_default() $buttonsConfig = [ 'confirm' => false, - 'cancel' => false, + 'cancel' => false, ]; $this->assertEquals($buttonsConfig, $notifier->getConfig('buttons')); } /** @test */ - function first_parameter_of_alert_message_is_the_config_text() + public function first_parameter_of_alert_message_is_the_config_text() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -56,7 +56,7 @@ function first_parameter_of_alert_message_is_the_config_text() } /** @test */ - function title_key_is_not_present_in_config_when_alert_title_is_not_set() + public function title_key_is_not_present_in_config_when_alert_title_is_not_set() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -67,7 +67,7 @@ function title_key_is_not_present_in_config_when_alert_title_is_not_set() } /** @test */ - function second_parameter_of_alert_message_is_the_config_title() + public function second_parameter_of_alert_message_is_the_config_title() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -78,7 +78,7 @@ function second_parameter_of_alert_message_is_the_config_title() } /** @test */ - function third_parameter_of_alert_message_is_the_config_icon() + public function third_parameter_of_alert_message_is_the_config_icon() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -89,7 +89,7 @@ function third_parameter_of_alert_message_is_the_config_icon() } /** @test */ - function icon_key_is_not_present_in_config_when_alert_icon_is_not_set() + public function icon_key_is_not_present_in_config_when_alert_icon_is_not_set() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -108,7 +108,7 @@ public function it_flashes_config_for_a_basic_alert() $notifier->basic('Basic Alert!', 'Alert'); $expectedConfig = [ - 'text' => 'Basic Alert!', + 'text' => 'Basic Alert!', 'title' => 'Alert', ]; $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once(); @@ -126,9 +126,9 @@ public function it_flashes_config_for_a_info_alert() $notifier->info('Info Alert!', 'Alert'); $expectedConfig = [ - 'text' => 'Info Alert!', + 'text' => 'Info Alert!', 'title' => 'Alert', - 'icon' => 'info', + 'icon' => 'info', ]; $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once(); $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text'])->once(); @@ -147,8 +147,8 @@ public function it_flashes_config_for_a_success_alert() $expectedConfig = [ 'title' => 'Success!', - 'text' => 'Well Done!', - 'icon' => 'success', + 'text' => 'Well Done!', + 'icon' => 'success', ]; $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); @@ -167,8 +167,8 @@ public function it_flashes_config_for_a_warning_alert() $expectedConfig = [ 'title' => 'Watch Out!', - 'text' => 'Hey cowboy!', - 'icon' => 'warning', + 'text' => 'Hey cowboy!', + 'icon' => 'warning', ]; $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']); $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']); @@ -187,8 +187,8 @@ public function it_flashes_config_for_a_error_alert() $expectedConfig = [ 'title' => 'Whoops!', - 'text' => 'Something wrong happened!', - 'icon' => 'error', + 'text' => 'Something wrong happened!', + 'icon' => 'error', ]; $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title']); $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text']); @@ -275,7 +275,7 @@ public function allows_to_configure_a_cancel_button_for_an_alert() } /** @test */ - function close_on_click_outside_config_can_be_enabled() + public function close_on_click_outside_config_can_be_enabled() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -286,7 +286,7 @@ function close_on_click_outside_config_can_be_enabled() } /** @test */ - function close_on_click_outside_config_can_be_disabled() + public function close_on_click_outside_config_can_be_disabled() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -297,7 +297,7 @@ function close_on_click_outside_config_can_be_disabled() } /** @test */ - function additional_buttons_can_be_added() + public function additional_buttons_can_be_added() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session); @@ -309,7 +309,7 @@ function additional_buttons_can_be_added() } /** @test */ - function additional_config_can_be_added_to_configure_alert_message() + public function additional_config_can_be_added_to_configure_alert_message() { $session = m::spy(SessionStore::class); $notifier = new SweetAlertNotifier($session);