Skip to content

Commit

Permalink
Use custom Prophecy token to clearly see what's not matched
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Mar 23, 2024
1 parent b9c4e32 commit 3b4351c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 32 deletions.
85 changes: 85 additions & 0 deletions tests/SVNBuddy/ProphecyToken/ProgressBarOutputToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* This file is part of the SVN-Buddy library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <[email protected]>
* @link https://github.com/console-helpers/svn-buddy
*/

namespace Tests\ConsoleHelpers\SVNBuddy\ProphecyToken;


use Prophecy\Argument\Token\TokenInterface;

class ProgressBarOutputToken implements TokenInterface
{

/**
* Value.
*
* @var string
*/
private $_value;

/**
* Creates token for matching to progress bar output.
*
* @param string $output Output.
*/
public function __construct($output)
{
$this->_value = $output;
}

/**
* @inheritDoc
*/
public function scoreArgument($argument)
{
return $this->normalize($argument) === $this->normalize($this->_value) ? 6 : false;
}

/**
* Normalizes text.
*
* @param string $text Text.
*
* @return string
*/
protected function normalize($text)
{
$ret = $text;

// Ignore the unlimited progress bar on PHP 5.6.
if ( \PHP_VERSION_ID < 70000 ) {
$ret = preg_replace('/\[[>-]+\]/', '[----------------------------]', $ret);
}

// Ignore memory consumption, because it might vary on different PHP versions.
$ret = preg_replace('#<info>\d+\.\d+ MiB\s+</info>#', '<info>0.0 MiB</info>', $ret);

// Ignore time consumption, because it might vary on different PHP versions.
$ret = preg_replace('/(<\s+)?\d+(\.\d+)? (sec|min)(s)?([\s]+)?/', '5 min', $ret);

return $ret;
}

/**
* @inheritDoc
*/
public function isLast()
{
return false;
}

/**
* @inheritDoc
*/
public function __toString()
{
return sprintf('progress bar output(%s)', $this->_value);
}

}
34 changes: 2 additions & 32 deletions tests/SVNBuddy/Repository/RevisionLog/RevisionLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Tests\ConsoleHelpers\SVNBuddy\ProphecyToken\ProgressBarOutputToken;
use Tests\ConsoleHelpers\SVNBuddy\ProphecyToken\SimpleXMLElementToken;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;

Expand Down Expand Up @@ -246,41 +247,10 @@ public function testRefreshWithoutCacheWithOutput($is_verbose)
protected function expectProgressBarOutput(ObjectProphecy $output, array $lines)
{
foreach ( $lines as $expected_line ) {
$expected_line = $this->normalizeProgressBarOutput($expected_line);

$output->write(
Argument::that(function ($actual_line) use ($expected_line) {
return $this->normalizeProgressBarOutput($actual_line) === $expected_line;
})
)->shouldBeCalled();
$output->write(new ProgressBarOutputToken($expected_line))->shouldBeCalled();
}
}

/**
* Normalizes progress bar output.
*
* @param string $output Output.
*
* @return string
*/
protected function normalizeProgressBarOutput($output)
{
$ret = $output;

// Ignore the unlimited progress bar on PHP 5.6.
if ( \PHP_VERSION_ID < 70000 ) {
$ret = preg_replace('/\[[>-]+\]/', '[----------------------------]', $ret);
}

// Ignore memory consumption, because it might vary on different PHP versions.
$ret = preg_replace('#<info>\d+\.\d+ MiB\s+</info>#', '<info>0.0 MiB</info>', $ret);

// Ignore time consumption, because it might vary on different PHP versions.
$ret = preg_replace('/(<\s+)?\d+(\.\d+)? (sec|min)(s)?/', '5 min', $ret);

return $ret;
}

public function refreshWithoutCacheWithOutputDataProvider()
{
return array(
Expand Down

0 comments on commit 3b4351c

Please sign in to comment.