gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

ArrayComparator.php (4585B)


      1 <?php
      2 /*
      3  * This file is part of the Comparator package.
      4  *
      5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
      6  *
      7  * For the full copyright and license information, please view the LICENSE
      8  * file that was distributed with this source code.
      9  */
     10 
     11 namespace SebastianBergmann\Comparator;
     12 
     13 /**
     14  * Compares arrays for equality.
     15  */
     16 class ArrayComparator extends Comparator
     17 {
     18     /**
     19      * Returns whether the comparator can compare two values.
     20      *
     21      * @param  mixed $expected The first value to compare
     22      * @param  mixed $actual   The second value to compare
     23      * @return bool
     24      */
     25     public function accepts($expected, $actual)
     26     {
     27         return is_array($expected) && is_array($actual);
     28     }
     29 
     30     /**
     31      * Asserts that two values are equal.
     32      *
     33      * @param  mixed             $expected     The first value to compare
     34      * @param  mixed             $actual       The second value to compare
     35      * @param  float             $delta        The allowed numerical distance between two values to
     36      *                                         consider them equal
     37      * @param  bool              $canonicalize If set to TRUE, arrays are sorted before
     38      *                                         comparison
     39      * @param  bool              $ignoreCase   If set to TRUE, upper- and lowercasing is
     40      *                                         ignored when comparing string values
     41      * @param  array             $processed
     42      * @throws ComparisonFailure Thrown when the comparison
     43      *                                        fails. Contains information about the
     44      *                                        specific errors that lead to the failure.
     45      */
     46     public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
     47     {
     48         if ($canonicalize) {
     49             sort($expected);
     50             sort($actual);
     51         }
     52 
     53         $remaining = $actual;
     54         $expString = $actString = "Array (\n";
     55         $equal     = true;
     56 
     57         foreach ($expected as $key => $value) {
     58             unset($remaining[$key]);
     59 
     60             if (!array_key_exists($key, $actual)) {
     61                 $expString .= sprintf(
     62                     "    %s => %s\n",
     63                     $this->exporter->export($key),
     64                     $this->exporter->shortenedExport($value)
     65                 );
     66 
     67                 $equal = false;
     68 
     69                 continue;
     70             }
     71 
     72             try {
     73                 $comparator = $this->factory->getComparatorFor($value, $actual[$key]);
     74                 $comparator->assertEquals($value, $actual[$key], $delta, $canonicalize, $ignoreCase, $processed);
     75 
     76                 $expString .= sprintf(
     77                     "    %s => %s\n",
     78                     $this->exporter->export($key),
     79                     $this->exporter->shortenedExport($value)
     80                 );
     81                 $actString .= sprintf(
     82                     "    %s => %s\n",
     83                     $this->exporter->export($key),
     84                     $this->exporter->shortenedExport($actual[$key])
     85                 );
     86             } catch (ComparisonFailure $e) {
     87                 $expString .= sprintf(
     88                     "    %s => %s\n",
     89                     $this->exporter->export($key),
     90                     $e->getExpectedAsString()
     91                     ? $this->indent($e->getExpectedAsString())
     92                     : $this->exporter->shortenedExport($e->getExpected())
     93                 );
     94 
     95                 $actString .= sprintf(
     96                     "    %s => %s\n",
     97                     $this->exporter->export($key),
     98                     $e->getActualAsString()
     99                     ? $this->indent($e->getActualAsString())
    100                     : $this->exporter->shortenedExport($e->getActual())
    101                 );
    102 
    103                 $equal = false;
    104             }
    105         }
    106 
    107         foreach ($remaining as $key => $value) {
    108             $actString .= sprintf(
    109                 "    %s => %s\n",
    110                 $this->exporter->export($key),
    111                 $this->exporter->shortenedExport($value)
    112             );
    113 
    114             $equal = false;
    115         }
    116 
    117         $expString .= ')';
    118         $actString .= ')';
    119 
    120         if (!$equal) {
    121             throw new ComparisonFailure(
    122                 $expected,
    123                 $actual,
    124                 $expString,
    125                 $actString,
    126                 false,
    127                 'Failed asserting that two arrays are equal.'
    128             );
    129         }
    130     }
    131 
    132     protected function indent($lines)
    133     {
    134         return trim(str_replace("\n", "\n    ", $lines));
    135     }
    136 }