Browse Source

Add info() and warning() convenience methods to FlashMessage.

ADmad 5 years ago
parent
commit
2881ee91d8
2 changed files with 47 additions and 4 deletions
  1. 31 1
      src/Http/FlashMessage.php
  2. 16 3
      tests/TestCase/Http/FlashMessageTest.php

+ 31 - 1
src/Http/FlashMessage.php

@@ -172,7 +172,6 @@ class FlashMessage
     public function success(string $message, array $options = []): void
     {
         $options['element'] = 'success';
-
         $this->set($message, $options);
     }
 
@@ -189,7 +188,38 @@ class FlashMessage
     public function error(string $message, array $options = []): void
     {
         $options['element'] = 'error';
+        $this->set($message, $options);
+    }
+
+    /**
+     * Set a warning message.
+     *
+     * The `'element'` option will be set to  `'warning'`.
+     *
+     * @param string $message Message to flash.
+     * @param array $options An array of options.
+     * @return void
+     * @see FlashMessage::set() For list of valid options
+     */
+    public function warning(string $message, array $options = []): void
+    {
+        $options['element'] = 'warning';
+        $this->set($message, $options);
+    }
 
+    /**
+     * Set an info message.
+     *
+     * The `'element'` option will be set to  `'info'`.
+     *
+     * @param string $message Message to flash.
+     * @param array $options An array of options.
+     * @return void
+     * @see FlashMessage::set() For list of valid options
+     */
+    public function info(string $message, array $options = []): void
+    {
+        $options['element'] = 'info';
         $this->set($message, $options);
     }
 }

+ 16 - 3
tests/TestCase/Http/FlashMessageTest.php

@@ -229,16 +229,19 @@ class FlashMessageTest extends TestCase
         $this->assertEquals($expected, $result);
     }
 
-    public function testSuccess(): void
+    /**
+     * @dataProvider convenienceMethods
+     */
+    public function testConvenienceMethods(string $type): void
     {
         $this->assertNull($this->Session->read('Flash.flash'));
 
-        $this->Flash->success('It worked');
+        $this->Flash->{$type}('It worked');
         $expected = [
             [
                 'message' => 'It worked',
                 'key' => 'flash',
-                'element' => 'flash/success',
+                'element' => 'flash/' . $type,
                 'params' => [],
             ],
         ];
@@ -246,6 +249,16 @@ class FlashMessageTest extends TestCase
         $this->assertEquals($expected, $result);
     }
 
+    public function convenienceMethods(): array
+    {
+        return [
+            ['success'],
+            ['error'],
+            ['warning'],
+            ['info'],
+        ];
+    }
+
     public function testSuccessWithClear(): void
     {
         $this->assertNull($this->Session->read('Flash.flash'));