From b195a4863b2b744256fbc6c434cd295966bd46b1 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 27 Jan 2016 00:15:13 +0100 Subject: [PATCH] #83 - cyclic alias exception and tests --- src/Exception/CyclicAliasException.php | 115 +++++++++++++ test/Exception/CyclicAliasExceptionTest.php | 174 ++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 src/Exception/CyclicAliasException.php create mode 100644 test/Exception/CyclicAliasExceptionTest.php diff --git a/src/Exception/CyclicAliasException.php b/src/Exception/CyclicAliasException.php new file mode 100644 index 00000000..260b2762 --- /dev/null +++ b/src/Exception/CyclicAliasException.php @@ -0,0 +1,115 @@ + $reference) { + $map[] = '"' . $alias . '" => "' . $reference . '"'; + } + + return "[\n" . implode("\n", $map) . "\n]"; + } + + /** + * @param string[][] $detectedCycles + * + * @return string + */ + private static function printCycles(array $detectedCycles) + { + return "[\n" . implode("\n", array_map([__CLASS__, 'printCycle'], $detectedCycles)) . "\n]"; + } + + /** + * @param string[] $detectedCycle + * + * @return string + */ + private static function printCycle(array $detectedCycle) + { + $fullCycle = array_keys($detectedCycle); + $fullCycle[] = reset($fullCycle); + + return implode( + ' => ', + array_map( + function ($cycle) { + return '"' . $cycle . '"'; + }, + $fullCycle + ) + ); + } +} diff --git a/test/Exception/CyclicAliasExceptionTest.php b/test/Exception/CyclicAliasExceptionTest.php new file mode 100644 index 00000000..4eb5532b --- /dev/null +++ b/test/Exception/CyclicAliasExceptionTest.php @@ -0,0 +1,174 @@ +getMessage()); + } + + /** + * @return string[][]|string[][][] + */ + public function aliasesProvider() + { + return [ + 'empty set' => [ + [], + 'A cycle was detected within the following aliases map: + +[ + +]' + ], + 'acyclic set' => [ + [ + 'b' => 'a', + 'd' => 'c', + ], + 'A cycle was detected within the following aliases map: + +[ +"b" => "a" +"d" => "c" +]' + ], + 'acyclic self-referencing set' => [ + [ + 'b' => 'a', + 'c' => 'b', + 'd' => 'c', + ], + 'A cycle was detected within the following aliases map: + +[ +"b" => "a" +"c" => "b" +"d" => "c" +]' + ], + 'cyclic set' => [ + [ + 'b' => 'a', + 'a' => 'b', + ], + 'A cycle was detected within the provided aliases: + +[ +"b" => "a" => "b" +"a" => "b" => "a" +] + +The cycle was detected in the following alias map: + +[ +"b" => "a" +"a" => "b" +]' + ], + 'cyclic set (indirect)' => [ + [ + 'b' => 'a', + 'c' => 'b', + 'a' => 'c', + ], + 'A cycle was detected within the provided aliases: + +[ +"b" => "a" => "c" => "b" +"c" => "b" => "a" => "c" +"a" => "c" => "b" => "a" +] + +The cycle was detected in the following alias map: + +[ +"b" => "a" +"c" => "b" +"a" => "c" +]' + ], + 'cyclic set + acyclic set' => [ + [ + 'b' => 'a', + 'a' => 'b', + 'd' => 'c', + ], + 'A cycle was detected within the provided aliases: + +[ +"b" => "a" => "b" +"a" => "b" => "a" +] + +The cycle was detected in the following alias map: + +[ +"b" => "a" +"a" => "b" +"d" => "c" +]' + ], + 'cyclic set + reference to cyclic set' => [ + [ + 'b' => 'a', + 'a' => 'b', + 'c' => 'a', + ], + 'A cycle was detected within the provided aliases: + +[ +"b" => "a" => "b" +"a" => "b" => "a" +"c" => "a" => "b" => "c" +] + +The cycle was detected in the following alias map: + +[ +"b" => "a" +"a" => "b" +"c" => "a" +]' + ], + ]; + } +}