]> git.mxchange.org Git - friendica.git/commitdiff
make ACLFormatter::expand() nullable and return an empty array
authorPhilipp Holzer <admin+github@philipp.info>
Fri, 1 Nov 2019 14:43:16 +0000 (15:43 +0100)
committerPhilipp Holzer <admin+github@philipp.info>
Fri, 1 Nov 2019 14:43:16 +0000 (15:43 +0100)
- optimize tests

src/Util/ACLFormatter.php
tests/src/Util/ACLFormaterTest.php

index d194e2e67eda93e3de339e5a8b96a9d6063c44b6..a7d851508d4e22d1c9441b959eb541c56e2875b9 100644 (file)
@@ -14,13 +14,13 @@ final class ACLFormatter
         *
         * @param string|null $ids A angle-bracketed list of IDs
         *
-        * @return array|null The array based on the IDs (null in case there is no list)
+        * @return array The array based on the IDs (empty in case there is no list)
         */
        public function expand(string $ids = null)
        {
-               // In case there is no ID list, return null (=> no ACL set)
+               // In case there is no ID list, return empty array (=> no ACL set)
                if (!isset($ids)) {
-                       return null;
+                       return [];
                }
 
                // turn string array of angle-bracketed elements into numeric array
index 630a30e40b86b8d72ecffa27dd12a2df7505b53f..ae875856ac6d899ab048469f9e9163660fee6a2e 100644 (file)
@@ -12,155 +12,96 @@ use PHPUnit\Framework\TestCase;
  */
 class ACLFormaterTest extends TestCase
 {
-       /**
-        * test expand_acl, perfect input
-        */
-       public function testExpandAclNormal()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text='<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>';
-               $this->assertEquals(array('1', '2', '3', Group::FOLLOWERS, Group::MUTUALS), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test with a big number
-        */
-       public function testExpandAclBigNumber()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text='<1><' . PHP_INT_MAX . '><15>';
-               $this->assertEquals(array('1', (string)PHP_INT_MAX, '15'), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test with a string in it.
-        *
-        * @todo is this valid input? Otherwise: should there be an exception?
-        */
-       public function testExpandAclString()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text="<1><279012><tt>";
-               $this->assertEquals(array('1', '279012'), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test with a ' ' in it.
-        *
-        * @todo is this valid input? Otherwise: should there be an exception?
-        */
-       public function testExpandAclSpace()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text="<1><279 012><32>";
-               $this->assertEquals(array('1', '32'), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test empty input
-        */
-       public function testExpandAclEmpty()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text="";
-               $this->assertEquals(array(), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test invalid input, no < at all
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclNoBrackets()
-       {
-               $aclFormatter = new ACLFormatter();
-
-               $text="According to documentation, that's invalid. "; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test invalid input, just open <
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclJustOneBracket1()
+       public function assertAcl($text, array $assert = [])
        {
                $aclFormatter = new ACLFormatter();
 
-               $text="<Another invalid string"; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
-       }
+               $acl = $aclFormatter->expand($text);
 
-       /**
-        * test invalid input, just close >
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclJustOneBracket2()
-       {
-               $aclFormatter = new ACLFormatter();
+               $this->assertEquals($assert, $acl);
 
-               $text="Another invalid> string"; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
+               $this->assertMergable($acl);
        }
 
-       /**
-        * test invalid input, just close >
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclCloseOnly()
+       public function assertMergable(array $aclOne, array $aclTwo = [])
        {
-               $aclFormatter = new ACLFormatter();
+               $this->assertTrue(is_array($aclOne));
+               $this->assertTrue(is_array($aclTwo));
 
-               $text="Another> invalid> string>"; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
-       }
-
-       /**
-        * test invalid input, just open <
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclOpenOnly()
-       {
-               $aclFormatter = new ACLFormatter();
+               $aclMerged = array_unique(array_merge($aclOne, $aclTwo));
+               $this->assertTrue(is_array($aclMerged));
 
-               $text="<Another< invalid string<"; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
+               return $aclMerged;
        }
 
-       /**
-        * test invalid input, open and close do not match
-        *
-        * @todo should there be an exception?
-        */
-       public function testExpandAclNoMatching1()
+       public function dataExpand()
        {
-               $aclFormatter = new ACLFormatter();
-
-               $text="<Another<> invalid <string>"; //should be invalid
-               $this->assertEquals(array(), $aclFormatter->expand($text));
+               return [
+                       'normal' => [
+                               'input' => '<1><2><3><' . Group::FOLLOWERS . '><' . Group::MUTUALS . '>',
+                               'assert' => ['1', '2', '3', Group::FOLLOWERS, Group::MUTUALS],
+                       ],
+                       'nigNumber' => [
+                               'input' => '<1><' . PHP_INT_MAX . '><15>',
+                               'assert' => ['1', (string)PHP_INT_MAX, '15'],
+                       ],
+                       'string' => [
+                               'input' => '<1><279012><tt>',
+                               'assert' => ['1', '279012'],
+                       ],
+                       'space' => [
+                               'input' => '<1><279 012><32>',
+                               'assert' => ['1', '32'],
+                       ],
+                       'empty' => [
+                               'input' => '',
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'noBrackets' => [
+                               'input' => 'According to documentation, that\'s invalid. ', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'justOneBracket' => [
+                               'input' => '<Another invalid string', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'justOneBracket2' => [
+                               'input' => 'Another invalid> string', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'closeOnly' => [
+                               'input' => 'Another> invalid> string>', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'openOnly' => [
+                               'input' => '<Another< invalid string<', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception?
+                       'noMatching1' => [
+                               'input' => '<Another<> invalid <string>', //should be invalid
+                               'assert' => [],
+                       ],
+                       /// @todo should there be an exception? Or array(1, 3)
+                       //  (This should be array(1,3) - mike)
+                       'emptyMatch' => [
+                               'input' => '<1><><3>', //should be invalid
+                               'assert' => ['1', '3'],
+                       ],
+               ];
        }
 
        /**
-        * test invalid input, empty <>
-        *
-        * @todo should there be an exception? Or array(1, 3)
-        * (This should be array(1,3) - mike)
+        * @dataProvider dataExpand
         */
-       public function testExpandAclEmptyMatch()
+       public function testExpand($input, array $assert)
        {
-               $aclFormatter = new ACLFormatter();
-
-               $text="<1><><3>";
-               $this->assertEquals(array('1', '3'), $aclFormatter->expand($text));
+               $this->assertAcl($input, $assert);
        }
 
        /**
@@ -170,8 +111,14 @@ class ACLFormaterTest extends TestCase
        {
                $aclFormatter = new ACLFormatter();
 
-               $this->assertNull($aclFormatter->expand(null));
-               $this->assertNull($aclFormatter->expand());
+               $allow_people = $aclFormatter->expand();
+               $allow_groups = $aclFormatter->expand();
+
+               $this->assertEmpty($aclFormatter->expand(null));
+               $this->assertEmpty($aclFormatter->expand());
+
+               $recipients   = array_unique(array_merge($allow_people, $allow_groups));
+               $this->assertEmpty($recipients);
        }
 
        public function dataAclToString()