Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / phpspec / prophecy / src / Prophecy / Call / CallCenter.php
index 53b80f058ba84e4d232b8ae32398ed54ec021312..bc936c8f1dbd5ce5cd26437509e8709c5520c175 100644 (file)
@@ -96,6 +96,7 @@ class CallCenter
         // Sort matches by their score value
         @usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; });
 
+        $score = $matches[0][0];
         // If Highest rated method prophecy has a promise - execute it or return null instead
         $methodProphecy = $matches[0][1];
         $returnValue = null;
@@ -115,9 +116,10 @@ class CallCenter
             );
         }
 
-        $this->recordedCalls[] = new Call(
+        $this->recordedCalls[] = $call = new Call(
             $methodName, $arguments, $returnValue, $exception, $file, $line
         );
+        $call->addScore($methodProphecy->getArgumentsWildcard(), $score);
 
         if (null !== $exception) {
             throw $exception;
@@ -139,7 +141,7 @@ class CallCenter
         return array_values(
             array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) {
                 return $methodName === $call->getMethodName()
-                    && 0 < $wildcard->scoreArguments($call->getArguments())
+                    && 0 < $call->getScore($wildcard)
                 ;
             })
         );
@@ -149,23 +151,79 @@ class CallCenter
                                                    array $arguments)
     {
         $classname = get_class($prophecy->reveal());
-        $argstring = implode(', ', array_map(array($this->util, 'stringify'), $arguments));
-        $expected  = implode("\n", array_map(function (MethodProphecy $methodProphecy) {
-            return sprintf('  - %s(%s)',
+        $indentationLength = 8; // looks good
+        $argstring = implode(
+            ",\n",
+            $this->indentArguments(
+                array_map(array($this->util, 'stringify'), $arguments),
+                $indentationLength
+            )
+        );
+
+        $expected = array();
+
+        foreach (call_user_func_array('array_merge', $prophecy->getMethodProphecies()) as $methodProphecy) {
+            $expected[] = sprintf(
+                "  - %s(\n" .
+                "%s\n" .
+                "    )",
                 $methodProphecy->getMethodName(),
-                $methodProphecy->getArgumentsWildcard()
+                implode(
+                    ",\n",
+                    $this->indentArguments(
+                        array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()),
+                        $indentationLength
+                    )
+                )
             );
-        }, call_user_func_array('array_merge', $prophecy->getMethodProphecies())));
+        }
 
         return new UnexpectedCallException(
             sprintf(
-                "Method call:\n".
-                "  - %s(%s)\n".
-                "on %s was not expected, expected calls were:\n%s",
-
-                $methodName, $argstring, $classname, $expected
+                "Unexpected method call on %s:\n".
+                "  - %s(\n".
+                "%s\n".
+                "    )\n".
+                "expected calls were:\n".
+                "%s",
+
+                $classname, $methodName, $argstring, implode("\n", $expected)
             ),
             $prophecy, $methodName, $arguments
+
+        );
+    }
+
+    private function formatExceptionMessage(MethodProphecy $methodProphecy)
+    {
+        return sprintf(
+            "  - %s(\n".
+            "%s\n".
+            "    )",
+            $methodProphecy->getMethodName(),
+            implode(
+                ",\n",
+                $this->indentArguments(
+                    array_map(
+                        function ($token) {
+                            return (string) $token;
+                        },
+                        $methodProphecy->getArgumentsWildcard()->getTokens()
+                    ),
+                    $indentationLength
+                )
+            )
+        );
+    }
+
+    private function indentArguments(array $arguments, $indentationLength)
+    {
+        return preg_replace_callback(
+            '/^/m',
+            function () use ($indentationLength) {
+                return str_repeat(' ', $indentationLength);
+            },
+            $arguments
         );
     }
 }