Пример #1
0
 /**
  * Get the difference between the supplied strings.
  *
  * @param string $from The from value.
  * @param string $to   The to value.
  *
  * @return string The difference.
  */
 public function difference($from, $to)
 {
     $flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
     $from = preg_split('/(\\W+)/u', $from, -1, $flags);
     $to = preg_split('/(\\W+)/u', $to, -1, $flags);
     $matcher = new DifferenceSequenceMatcher($from, $to);
     $diff = '';
     foreach ($matcher->getOpcodes() as $opcode) {
         list($tag, $i1, $i2, $j1, $j2) = $opcode;
         if ($tag === 'equal') {
             $diff .= implode(array_slice($from, $i1, $i2 - $i1));
         } else {
             if ($tag === 'replace' || $tag === 'delete') {
                 $diff .= $this->removeStart . implode(array_slice($from, $i1, $i2 - $i1)) . $this->removeEnd;
             }
             if ($tag === 'replace' || $tag === 'insert') {
                 $diff .= $this->addStart . implode(array_slice($to, $j1, $j2 - $j1)) . $this->addEnd;
             }
         }
     }
     return $diff;
 }
Пример #2
0
 /**
  * Render a failed inOrder() verification.
  *
  * @param array<Event> $expected The expected events.
  * @param array<Event> $actual   The actual events.
  *
  * @return string The rendered failure message.
  */
 public function renderInOrder(array $expected, array $actual)
 {
     if (empty($expected)) {
         return $this->reset . 'Expected events.' . PHP_EOL . $this->failStart . 'No events recorded.' . $this->reset;
     }
     $from = $this->renderEvents($expected);
     $to = $this->renderEvents($actual);
     $matcher = new DifferenceSequenceMatcher($from, $to);
     $diff = array();
     foreach ($matcher->getOpcodes() as $opcode) {
         list($tag, $i1, $i2, $j1, $j2) = $opcode;
         if ($tag === 'equal') {
             foreach (array_slice($from, $i1, $i2 - $i1) as $event) {
                 $diff[] = '    ' . $this->pass . '   ' . $event;
             }
         } else {
             if ($tag === 'replace' || $tag === 'delete') {
                 foreach (array_slice($from, $i1, $i2 - $i1) as $event) {
                     $diff[] = '    ' . $this->fail . ' ' . $this->removeStart . $event . $this->removeEnd;
                 }
             }
             if ($tag === 'replace' || $tag === 'insert') {
                 foreach (array_slice($to, $j1, $j2 - $j1) as $event) {
                     $diff[] = '    - ' . $this->addStart . $event . $this->addEnd;
                 }
             }
         }
     }
     $renderedExpected = array();
     foreach ($from as $event) {
         $renderedExpected[] = '    - ' . $event;
     }
     $renderedActual = array();
     foreach ($to as $event) {
         $renderedActual[] = '    - ' . $event;
     }
     return $this->reset . 'Expected events in order:' . PHP_EOL . implode(PHP_EOL, $renderedExpected) . PHP_EOL . 'Actual order:' . PHP_EOL . implode(PHP_EOL, $renderedActual) . PHP_EOL . 'Difference:' . PHP_EOL . implode(PHP_EOL, $diff);
 }