Browse Source

Fix ConsoleOutput styles() API.

mscherer 6 years ago
parent
commit
43af5df64d

+ 26 - 6
src/Console/ConsoleIo.php

@@ -414,18 +414,38 @@ class ConsoleIo
     }
 
     /**
-     * Add a new output style or get defined styles.
+     * @return array
+     * @see \Cake\Console\ConsoleOutput::styles()
+     */
+    public function styles(): array
+    {
+        return $this->_out->styles();
+    }
+
+    /**
+     * Get defined styles.
+     *
+     * @param string $style
+     * @return array
+     * @see \Cake\Console\ConsoleOutput::getStyle()
+     */
+    public function getStyle(string $style): array
+    {
+        return $this->_out->getStyle($style);
+    }
+
+    /**
+     * Add a new output style or
      *
      * @param string|null $style The style to get or create.
      * @param array|false|null $definition The array definition of the style to change or create a style
      *   or false to remove a style.
-     * @return array|true|null If you are getting styles, the style or null will be returned. If you are creating/modifying
-     *   styles true will be returned.
-     * @see \Cake\Console\ConsoleOutput::styles()
+     * @return void
+     * @see \Cake\Console\ConsoleOutput::setStyle()
      */
-    public function styles(?string $style = null, $definition = null)
+    public function setStyle(string $style, array $definition): void
     {
-        return $this->_out->styles($style, $definition);
+        $this->_out->setStyle($style, $definition);
     }
 
     /**

+ 31 - 32
src/Console/ConsoleOutput.php

@@ -230,8 +230,7 @@ class ConsoleOutput
      */
     protected function _replaceTags(array $matches): string
     {
-        /** @var array $style */
-        $style = $this->styles($matches['tag']);
+        $style = $this->getStyle($matches['tag']);
         if (empty($style)) {
             return '<' . $matches['tag'] . '>' . $matches['text'] . '</' . $matches['tag'] . '>';
         }
@@ -265,56 +264,56 @@ class ConsoleOutput
     }
 
     /**
-     * Get the current styles offered, or append new ones in.
+     * Gets the current styles offered
      *
-     * ### Get a style definition
-     *
-     * ```
-     * $output->styles('error');
-     * ```
-     *
-     * ### Get all the style definitions
-     *
-     * ```
-     * $output->styles();
-     * ```
+     * @param string $style The style to get.
+     * @return array The style or empty array.
+     */
+    public function getStyle(string $style): array
+    {
+        return static::$_styles[$style] ?? [];
+    }
+
+    /**
+     * Sets style.
      *
-     * ### Create or modify an existing style
+     * ### Creates or modifies an existing style.
      *
      * ```
-     * $output->styles('annoy', ['text' => 'purple', 'background' => 'yellow', 'blink' => true]);
+     * $output->setStyle('annoy', ['text' => 'purple', 'background' => 'yellow', 'blink' => true]);
      * ```
      *
      * ### Remove a style
      *
      * ```
-     * $this->output->styles('annoy', false);
+     * $this->output->setStyle('annoy', []);
      * ```
      *
-     * @param string|null $style The style to get or create.
-     * @param array|false|null $definition The array definition of the style to change or create a style
-     *   or false to remove a style.
-     * @return array|true|null If you are getting styles, the style or null will be returned. If you are creating/modifying
-     *   styles true will be returned.
+     * @param string $style The style to get or create.
+     * @param array $definition The array definition of the style to change or create..
+     * @return void
      */
-    public function styles(?string $style = null, $definition = null)
+    public function setStyle(string $style, array $definition): void
     {
-        if ($style === null && $definition === null) {
-            return static::$_styles;
-        }
-        if ($definition === null) {
-            return static::$_styles[$style] ?? null;
-        }
-        if ($definition === false) {
+        if (!$definition) {
             /** @psalm-suppress PossiblyNullArrayOffset */
             unset(static::$_styles[$style]);
 
-            return true;
+            return;
         }
+
         /** @psalm-suppress PossiblyNullArrayOffset */
         static::$_styles[$style] = $definition;
+    }
 
-        return true;
+    /**
+     * Gets all the style definitions.
+     *
+     * @return array
+     */
+    public function styles(): array
+    {
+        return static::$_styles;
     }
 
     /**

+ 0 - 1
src/Shell/Helper/TableHelper.php

@@ -71,7 +71,6 @@ class TableHelper extends Helper
             return mb_strwidth($text);
         }
 
-        /** @var array $styles */
         $styles = $this->_io->styles();
         $tags = implode('|', array_keys($styles));
         $text = preg_replace('#</?(?:' . $tags . ')>#', '', $text);

+ 28 - 3
tests/TestCase/Console/ConsoleIoTest.php

@@ -545,6 +545,32 @@ class ConsoleIoTest extends TestCase
     }
 
     /**
+     * Ensure that setStyle() just proxies to stdout.
+     *
+     * @return void
+     */
+    public function testSetStyle()
+    {
+        $this->out->expects($this->once())
+            ->method('setStyle')
+            ->with('name', ['props']);
+        $this->io->setStyle('name', ['props']);
+    }
+
+    /**
+     * Ensure that getStyle() just proxies to stdout.
+     *
+     * @return void
+     */
+    public function testGetStyle()
+    {
+        $this->out->expects($this->once())
+            ->method('getStyle')
+            ->with('name');
+        $this->io->getStyle('name');
+    }
+
+    /**
      * Ensure that styles() just proxies to stdout.
      *
      * @return void
@@ -552,9 +578,8 @@ class ConsoleIoTest extends TestCase
     public function testStyles()
     {
         $this->out->expects($this->once())
-            ->method('styles')
-            ->with('name', 'props');
-        $this->io->styles('name', 'props');
+            ->method('styles');
+        $this->io->styles();
     }
 
     /**

+ 13 - 8
tests/TestCase/Console/ConsoleOutputTest.php

@@ -27,6 +27,11 @@ use Cake\TestSuite\TestCase;
 class ConsoleOutputTest extends TestCase
 {
     /**
+     * @var \Cake\Console\ConsoleOutput|\PHPUnit\Framework\MockObject\MockObject
+     */
+    protected $output;
+
+    /**
      * setup
      *
      * @return void
@@ -34,7 +39,7 @@ class ConsoleOutputTest extends TestCase
     public function setUp(): void
     {
         parent::setUp();
-        $this->output = $this->getMockBuilder('Cake\Console\ConsoleOutput')
+        $this->output = $this->getMockBuilder(ConsoleOutput::class)
             ->setMethods(['_write'])
             ->getMock();
         $this->output->setOutputAs(ConsoleOutput::COLOR);
@@ -110,11 +115,11 @@ class ConsoleOutputTest extends TestCase
      */
     public function testStylesGet()
     {
-        $result = $this->output->styles('error');
+        $result = $this->output->getStyle('error');
         $expected = ['text' => 'red'];
         $this->assertEquals($expected, $result);
 
-        $this->assertNull($this->output->styles('made_up_goop'));
+        $this->assertSame([], $this->output->getStyle('made_up_goop'));
 
         $result = $this->output->styles();
         $this->assertNotEmpty($result, 'Error is missing');
@@ -128,13 +133,13 @@ class ConsoleOutputTest extends TestCase
      */
     public function testStylesAdding()
     {
-        $this->output->styles('test', ['text' => 'red', 'background' => 'black']);
-        $result = $this->output->styles('test');
+        $this->output->setStyle('test', ['text' => 'red', 'background' => 'black']);
+        $result = $this->output->getStyle('test');
         $expected = ['text' => 'red', 'background' => 'black'];
         $this->assertEquals($expected, $result);
 
-        $this->assertTrue($this->output->styles('test', false), 'Removing a style should return true.');
-        $this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
+        $this->output->setStyle('test', []);
+        $this->assertSame([], $this->output->getStyle('test'), 'Removed styles should be empty.');
     }
 
     /**
@@ -170,7 +175,7 @@ class ConsoleOutputTest extends TestCase
      */
     public function testFormattingCustom()
     {
-        $this->output->styles('annoying', [
+        $this->output->setStyle('annoying', [
             'text' => 'magenta',
             'background' => 'cyan',
             'blink' => true,