Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to PHPUnit 7 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 33 additions & 69 deletions tests/Listener/Measurement/TimeAndMemoryTestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace PhpunitMemoryAndTimeUsageListener\Listener\Measurement;

use PHPUnit\Framework\TestCase;
use PhpunitMemoryAndTimeUsageListener\Domain\Measurement\MemoryMeasurement;
use PhpunitMemoryAndTimeUsageListener\Domain\Measurement\TestMeasurement;
use PhpunitMemoryAndTimeUsageListener\Domain\Measurement\TimeMeasurement;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Test;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort use statements please 😄


class TimeAndMemoryTestListener implements \PHPUnit_Framework_TestListener
class TimeAndMemoryTestListener implements TestListener
{
/** @var int */

use TestListenerDefaultImplementation;

/** @var int */
protected $testSuitesRunning = 0;

/** @var TestMeasurement[] */
Expand Down Expand Up @@ -48,100 +56,56 @@ public function __construct($configurationOptions = array())
}

/**
* @param \PHPUnit_Framework_Test $test
* @param Test $test
*/
public function startTest(\PHPUnit_Framework_Test $test)
public function startTest(Test $test): void
{
$this->memoryUsage = memory_get_usage();
$this->memoryPeakIncrease = memory_get_peak_usage();
}

/**
* @param \PHPUnit_Framework_Test $test
* @param Test $test
* @param $time
*/
public function endTest(\PHPUnit_Framework_Test $test, $time)
public function endTest(Test $test, float $time): void
{
$this->executionTime = new TimeMeasurement($time);
$this->memoryUsage = memory_get_usage() - ($this->memoryUsage);
$this->memoryPeakIncrease = memory_get_peak_usage() - ($this->memoryPeakIncrease);
$this->memoryUsage = memory_get_usage() - $this->memoryUsage;
$this->memoryPeakIncrease = memory_get_peak_usage() - $this->memoryPeakIncrease;

if ($this->haveToSaveTestMeasurement($time)) {
if ($this->haveToSaveTestMeasurement()) {
$name = ($test instanceof TestCase) ? $test->getName() : '';
$this->testMeasurementCollection[] = new TestMeasurement(
$test->getName(),
$name,
get_class($test),
$this->executionTime,
(new MemoryMeasurement($this->memoryUsage)),
(new MemoryMeasurement($this->memoryPeakIncrease))
new MemoryMeasurement($this->memoryUsage),
new MemoryMeasurement($this->memoryPeakIncrease)
);
}
}

/**
* @param \PHPUnit_Framework_Test $test
* @param \Exception $exception
* @param float $time
*/
public function addError(\PHPUnit_Framework_Test $test, \Exception $exception, $time)
{
}

/**
* @param \PHPUnit_Framework_Test $test
* @param \PHPUnit_Framework_AssertionFailedError $exception
* @param float $time
*/
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $exception, $time)
{
}

/**
* @param \PHPUnit_Framework_Test $test
* @param \Exception $exception
* @param float $time
*/
public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $exception, $time)
{
}

/**
* @param \PHPUnit_Framework_Test $test
* @param \Exception $exception
* @param float $time
*/
public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $exception, $time)
{
}

/**
* @param \PHPUnit_Framework_Test $test
* @param \Exception $exception
* @param float $time
*/
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $exception, $time)
{
}

/**
* @param \PHPUnit_Framework_TestSuite $suite
* @param TestSuite $suite
*/
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function startTestSuite(TestSuite $suite): void
{
$this->testSuitesRunning++;
}

/**
* @param \PHPUnit_Framework_TestSuite $suite
* @param TestSuite $suite
*/
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function endTestSuite(TestSuite $suite): void
{
$this->testSuitesRunning--;

if ((0 === $this->testSuitesRunning) && (0 < count($this->testMeasurementCollection))) {
echo PHP_EOL . "Time & Memory measurement results: " . PHP_EOL;
echo PHP_EOL . 'Time & Memory measurement results: ' . PHP_EOL;
$i = 1;
foreach ($this->testMeasurementCollection as $testMeasurement) {
echo PHP_EOL . $i . " - " . $testMeasurement->measuredInformationMessage();
echo PHP_EOL . $i . ' - ' . $testMeasurement->measuredInformationMessage();
$i++;
}
}
Expand All @@ -150,7 +114,7 @@ public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
/**
* @return bool
*/
protected function haveToSaveTestMeasurement()
protected function haveToSaveTestMeasurement(): bool
{
return ((false === $this->showOnlyIfEdgeIsExceeded)
|| ((true === $this->showOnlyIfEdgeIsExceeded)
Expand All @@ -167,7 +131,7 @@ protected function haveToSaveTestMeasurement()
*
* @return bool
*/
protected function isAPotentialCriticalTimeUsage()
protected function isAPotentialCriticalTimeUsage(): bool
{
return $this->checkEdgeIsOverTaken($this->executionTime->timeInMilliseconds(), $this->executionTimeEdge);
}
Expand All @@ -177,7 +141,7 @@ protected function isAPotentialCriticalTimeUsage()
*
* @return bool
*/
protected function isAPotentialCriticalMemoryUsage()
protected function isAPotentialCriticalMemoryUsage(): bool
{
return $this->checkEdgeIsOverTaken($this->memoryUsage, $this->memoryUsageEdge);
}
Expand All @@ -187,7 +151,7 @@ protected function isAPotentialCriticalMemoryUsage()
*
* @return bool
*/
protected function isAPotentialCriticalMemoryPeakUsage()
protected function isAPotentialCriticalMemoryPeakUsage(): bool
{
return $this->checkEdgeIsOverTaken($this->memoryPeakIncrease, $this->memoryPeakDifferenceEdge);
}
Expand All @@ -197,15 +161,15 @@ protected function isAPotentialCriticalMemoryPeakUsage()
* @param $edgeValue
* @return bool
*/
protected function checkEdgeIsOverTaken($value, $edgeValue)
protected function checkEdgeIsOverTaken($value, $edgeValue): bool
{
return ($value >= $edgeValue);
}

/**
* @param $configurationOptions
*/
protected function setConfigurationOptions($configurationOptions)
protected function setConfigurationOptions($configurationOptions): void
{
if (isset($configurationOptions['showOnlyIfEdgeIsExceeded'])) {
$this->showOnlyIfEdgeIsExceeded = $configurationOptions['showOnlyIfEdgeIsExceeded'];
Expand Down