Browse Source

Merge branch 'master' into 3.next

Mark Story 9 years ago
parent
commit
dccc883c57

+ 3 - 3
composer.json

@@ -76,8 +76,8 @@
             "@cs-check",
             "@test"
         ],
-        "cs-check": "vendor/bin/phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
-        "cs-fix": "vendor/bin/phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
-        "test": "vendor/bin/phpunit --colors=always"
+        "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
+        "cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
+        "test": "phpunit --colors=always"
     }
 }

+ 8 - 1
src/Console/Shell.php

@@ -49,6 +49,13 @@ class Shell
     const CODE_ERROR = 1;
 
     /**
+     * Default success code
+     *
+     * @var int
+     */
+    const CODE_SUCCESS = 0;
+
+    /**
      * Output constant making verbose shells.
      *
      * @var int
@@ -881,7 +888,7 @@ class Shell
      * @throws \Cake\Console\Exception\StopException
      * @return void
      */
-    protected function _stop($status = 0)
+    protected function _stop($status = self::CODE_SUCCESS)
     {
         throw new StopException('Halting error reached', $status);
     }

+ 2 - 2
src/Console/ShellDispatcher.php

@@ -184,10 +184,10 @@ class ShellDispatcher
             return $e->getCode();
         }
         if ($result === null || $result === true) {
-            return 0;
+            return Shell::CODE_SUCCESS;
         }
 
-        return 1;
+        return Shell::CODE_ERROR;
     }
 
     /**

+ 26 - 2
src/Validation/Validation.php

@@ -717,7 +717,7 @@ class Validation
     }
 
     /**
-     * Checks whether the length of a string is greater or equal to a minimal length.
+     * Checks whether the length of a string (in characters) is greater or equal to a minimal length.
      *
      * @param string $check The string to test
      * @param int $min The minimal string length
@@ -729,7 +729,7 @@ class Validation
     }
 
     /**
-     * Checks whether the length of a string is smaller or equal to a maximal length..
+     * Checks whether the length of a string (in characters) is smaller or equal to a maximal length.
      *
      * @param string $check The string to test
      * @param int $max The maximal string length
@@ -741,6 +741,30 @@ class Validation
     }
 
     /**
+     * Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
+     *
+     * @param string $check The string to test
+     * @param int $min The minimal string length (in bytes)
+     * @return bool Success
+     */
+    public static function minLengthBytes($check, $min)
+    {
+        return strlen($check) >= $min;
+    }
+
+    /**
+     * Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.
+     *
+     * @param string $check The string to test
+     * @param int $max The maximal string length
+     * @return bool Success
+     */
+    public static function maxLengthBytes($check, $max)
+    {
+        return strlen($check) <= $max;
+    }
+
+    /**
      * Checks that a value is a monetary amount.
      *
      * @param string $check Value to check

+ 40 - 0
src/Validation/Validator.php

@@ -1179,6 +1179,26 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      * Add a string length validation rule to a field.
      *
      * @param string $field The field you want to apply the rule to.
+     * @param int $min The minimum length required.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::minLengthBytes()
+     * @return $this
+     */
+    public function minLengthBytes($field, $min, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'minLengthBytes', $extra + [
+            'rule' => ['minLengthBytes', $min]
+        ]);
+    }
+
+    /**
+     * Add a string length validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
      * @param int $max The maximum length allowed.
      * @param string|null $message The error message when the rule fails.
      * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
@@ -1196,6 +1216,26 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * Add a string length validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int $max The maximum length allowed.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::maxLengthBytes()
+     * @return $this
+     */
+    public function maxLengthBytes($field, $max, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'maxLengthBytes', $extra + [
+            'rule' => ['maxLengthBytes', $max]
+        ]);
+    }
+
+    /**
      * Add a numeric value validation rule to a field.
      *
      * @param string $field The field you want to apply the rule to.

+ 1 - 3
src/View/View.php

@@ -501,9 +501,7 @@ class View implements EventDispatcherInterface
             list ($plugin, $name) = pluginSplit($name, true);
             $name = str_replace('/', DIRECTORY_SEPARATOR, $name);
             $file = $plugin . 'Element' . DIRECTORY_SEPARATOR . $name . $this->_ext;
-            throw new MissingElementException(
-                sprintf('Element file "%s" is missing', $file)
-            );
+            throw new MissingElementException([$file]);
         }
     }
 

+ 7 - 6
tests/TestCase/Console/ShellDispatcherTest.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Test\TestCase\Console;
 
+use Cake\Console\Shell;
 use Cake\Console\ShellDispatcher;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
@@ -204,12 +205,12 @@ class ShellDispatcherTest extends TestCase
 
         $dispatcher->args = ['mock_with_main'];
         $result = $dispatcher->dispatch();
-        $this->assertSame(0, $result);
+        $this->assertSame(Shell::CODE_SUCCESS, $result);
         $this->assertEquals([], $dispatcher->args);
 
         $dispatcher->args = ['mock_with_main'];
         $result = $dispatcher->dispatch();
-        $this->assertSame(0, $result);
+        $this->assertSame(Shell::CODE_SUCCESS, $result);
         $this->assertEquals([], $dispatcher->args);
     }
 
@@ -237,7 +238,7 @@ class ShellDispatcherTest extends TestCase
 
         $dispatcher->args = ['mock_without_main', 'initdb'];
         $result = $dispatcher->dispatch();
-        $this->assertEquals(0, $result);
+        $this->assertEquals(Shell::CODE_SUCCESS, $result);
     }
 
     /**
@@ -264,7 +265,7 @@ class ShellDispatcherTest extends TestCase
 
         $dispatcher->args = ['example'];
         $result = $dispatcher->dispatch();
-        $this->assertEquals(0, $result);
+        $this->assertEquals(Shell::CODE_SUCCESS, $result);
     }
 
     /**
@@ -291,7 +292,7 @@ class ShellDispatcherTest extends TestCase
 
         $dispatcher->args = ['Example'];
         $result = $dispatcher->dispatch();
-        $this->assertEquals(0, $result);
+        $this->assertEquals(Shell::CODE_SUCCESS, $result);
     }
 
     /**
@@ -318,7 +319,7 @@ class ShellDispatcherTest extends TestCase
 
         $dispatcher->args = ['sample'];
         $result = $dispatcher->dispatch();
-        $this->assertEquals(0, $result);
+        $this->assertEquals(Shell::CODE_SUCCESS, $result);
     }
 
     /**

+ 2 - 2
tests/TestCase/Console/ShellTest.php

@@ -67,7 +67,7 @@ class ShellTestShell extends Shell
      * @param int $status
      * @return void
      */
-    protected function _stop($status = 0)
+    protected function _stop($status = Shell::CODE_SUCCESS)
     {
         $this->stopped = $status;
     }
@@ -991,7 +991,7 @@ TEXT;
      *
      * @return void
      */
-    public function testRunCommandBaseclassMethod()
+    public function testRunCommandBaseClassMethod()
     {
         $shell = $this->getMockBuilder('Cake\Console\Shell')
             ->setMethods(['startup', 'getOptionParser', 'out', 'hr'])

+ 7 - 7
tests/TestCase/Utility/XmlTest.php

@@ -386,12 +386,12 @@ XML;
      */
     public function testFromArrayZeroValue()
     {
-        $xml = array(
-            'tag' => array(
+        $xml = [
+            'tag' => [
                 '@' => 0,
                 '@test' => 'A test'
-            )
-        );
+            ]
+        ];
         $obj = Xml::fromArray($xml);
         $xmlText = <<<XML
 <?xml version="1.0" encoding="UTF-8"?>
@@ -399,9 +399,9 @@ XML;
 XML;
         $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
 
-        $xml = array(
-            'tag' => array('0')
-        );
+        $xml = [
+            'tag' => ['0']
+        ];
         $obj = Xml::fromArray($xml);
         $xmlText = <<<XML
 <?xml version="1.0" encoding="UTF-8"?>

+ 32 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -2068,6 +2068,22 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * maxLengthBytes method
+     *
+     * @return void
+     */
+    public function testMaxLengthBytes()
+    {
+        $this->assertTrue(Validation::maxLengthBytes('ab', 3));
+        $this->assertTrue(Validation::maxLengthBytes('abc', 3));
+        $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 10));
+        $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 11));
+
+        $this->assertFalse(Validation::maxLengthBytes('abcd', 3));
+        $this->assertFalse(Validation::maxLengthBytes('ÆΔΩЖÇ', 9));
+    }
+
+    /**
      * testMinLength method
      *
      * @return void
@@ -2083,6 +2099,22 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * minLengthBytes method
+     *
+     * @return void
+     */
+    public function testMinLengthBytes()
+    {
+        $this->assertFalse(Validation::minLengthBytes('ab', 3));
+        $this->assertFalse(Validation::minLengthBytes('ÆΔΩЖÇ', 11));
+
+        $this->assertTrue(Validation::minLengthBytes('abc', 3));
+        $this->assertTrue(Validation::minLengthBytes('abcd', 3));
+        $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 10));
+        $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 9));
+    }
+
+    /**
      * testUrl method
      *
      * @return void

+ 24 - 0
tests/TestCase/Validation/ValidatorTest.php

@@ -1524,6 +1524,18 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the minLengthBytes proxy method
+     *
+     * @return void
+     */
+    public function testMinLengthBytes()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'minLengthBytes', 11, [11]);
+        $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
+    }
+
+    /**
      * Tests the maxLength proxy method
      *
      * @return void
@@ -1536,6 +1548,18 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the maxLengthBytes proxy method
+     *
+     * @return void
+     */
+    public function testMaxLengthBytes()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'maxLengthBytes', 9, [9]);
+        $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
+    }
+
+    /**
      * Tests the numeric proxy method
      *
      * @return void

+ 5 - 3
tests/TestCase/View/ViewTest.php

@@ -897,9 +897,10 @@ class ViewTest extends TestCase
     }
 
     /**
-     * Test elementInexistent method
+     * Test loading inexistent view element
      *
      * @expectedException \Cake\View\Exception\MissingElementException
+     * @expectedExceptionMessageRegExp $Element file \"Element[\\|/]non_existent_element\.ctp\" is missing$
      * @return void
      */
     public function testElementInexistent()
@@ -908,12 +909,13 @@ class ViewTest extends TestCase
     }
 
     /**
-     * Test elementInexistent3 method
+     * Test loading inexistent plugin view element
      *
      * @expectedException \Cake\View\Exception\MissingElementException
+     * @expectedExceptionMessageRegExp $Element file "test_plugin\.Element[\\|/]plugin_element\.ctp\" is missing$
      * @return void
      */
-    public function testElementInexistent3()
+    public function testElementInexistentPluginElement()
     {
         $this->View->element('test_plugin.plugin_element');
     }