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

chore(deps): update dependency phpunit/phpunit to v10 #88

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ vendor

# phpunit
.phpunit.result.cache
.phpunit.cache/
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"require-dev": {
"guzzlehttp/guzzle": "7.5.0",
"fluent/logger": "1.0.1",
"phpunit/phpunit": "9.6.3",
"phpunit/phpunit": "10.0.14",
"vimeo/psalm": "5.7.7",
"dealerdirect/phpcodesniffer-composer-installer": "1.0.0",
"phpcompatibility/php-compatibility": "9.3.5",
Expand Down
582 changes: 227 additions & 355 deletions composer.lock

Large diffs are not rendered by default.

30 changes: 11 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
bootstrap="./vendor/autoload.php"
verbose="true"
>
<testsuites>
<testsuite name="Kronos-Log">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false" addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage includeUncoveredFiles="false">
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Kronos-Log">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
53 changes: 53 additions & 0 deletions tests/ExtendedTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Kronos\Tests\Log;

use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\TestCase;

abstract class ExtendedTestCase extends TestCase
{
public static function withConsecutive(array $firstCallArguments, array ...$consecutiveCallsArguments)
{
$allConsecutiveCallsArguments = [$firstCallArguments, ...$consecutiveCallsArguments];

$maxNumberOfArguments = 0;
foreach ($allConsecutiveCallsArguments as $consecutiveCallArguments) {
$numberOfArguments = count($consecutiveCallArguments);
$maxNumberOfArguments = max($maxNumberOfArguments, $numberOfArguments);
}

$argumentList = [];
for ($argumentPosition = 0; $argumentPosition < $maxNumberOfArguments; $argumentPosition++) {
$argumentList[$argumentPosition] = array_column($allConsecutiveCallsArguments, $argumentPosition);
}

$mockedMethodCall = 0;
$callbackCall = 0;
foreach ($argumentList as $index => $argument) {
yield new Callback(
static function (mixed $actualArgument) use (
$argumentList,
&$mockedMethodCall,
&$callbackCall,
$index,
$maxNumberOfArguments
): bool {
$expected = $argumentList[$index][$mockedMethodCall] ?? null;

$callbackCall++;
$mockedMethodCall = (int) ($callbackCall / $maxNumberOfArguments);

if ($expected instanceof Constraint) {
self::assertThat($actualArgument, $expected);
} else {
self::assertEquals($expected, $actualArgument);
}

return true;
},
);
}
}
}
2 changes: 1 addition & 1 deletion tests/LogLevelHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_validateLogLevel_shouldThrowInvalidLogLevelWhenLevelIsUnkno
LogLevelHelper::validateLogLevel('unknown level');
}

public function provideLevels(): array
public static function provideLevels(): array

Choose a reason for hiding this comment

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

Tant qu'à la mettre statique, on pourrait aussi l'extraire dans une Fixture. C'est une classe de test, elle ne devrait pas partager des méthodes avec d'autres classes

{
return [
[LogLevel::ALERT, LogLevel::EMERGENCY],
Expand Down
2 changes: 1 addition & 1 deletion tests/LoggerDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function test_shouldNotLogWhenLoggerLevelIsHigherThanMessage($loggerLevel
$this->loggerDecorator->log($levelOfMessage, 'a message');
}

public function provideLowerLogLevels(): array
public static function provideLowerLogLevels(): array

Choose a reason for hiding this comment

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

Même commentaire que pour LogLevelHelperTest#provideLevels

{
return [
[LogLevel::INFO, LogLevel::DEBUG],
Expand Down
127 changes: 43 additions & 84 deletions tests/Writer/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace Kronos\Tests\Log\Writer;

use Exception;
use Kronos\Log\Adaptor\FileFactory;
use Kronos\Log\Adaptor\TTY;
use Kronos\Log\Enumeration\AnsiBackgroundColor;
use Kronos\Log\Enumeration\AnsiTextColor;
use Kronos\Log\Factory\Formatter;
use Kronos\Log\Formatter\Exception\TraceBuilder;
use Kronos\Log\Logger;
use Kronos\Log\Writer\Console;
use \Kronos\Log\Logger;
use Kronos\Tests\Log\ExtendedTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;
use Exception;
use PHPUnit_Framework_MockObject_MockObject;

class ConsoleTest extends \PHPUnit\Framework\TestCase
class ConsoleTest extends ExtendedTestCase
{

const LOGLEVEL_BELOW_ERROR = LogLevel::INFO;
const LOGLEVEL_ABOVE_WARNING = LogLevel::ERROR;
const A_MESSAGE = 'a message {key}';
Expand All @@ -37,36 +35,10 @@ class ConsoleTest extends \PHPUnit\Framework\TestCase
const PREVIOUS_EXCEPTION_LINE = 3;
const PREVIOUS_EXCEPTION_TITLE_LINE_FORMAT = "Previous exception: 'Previous exception message' in '%s' at line %i";

/**
* @var Console
*/
private $writer;

/**
* @var FileFactory&MockObject
*/
private $fileFactory;

/**
* @var TTY&MockObject
*/
private $stdout;

/**
* @var TTY&MockObject
*/
private $stderr;

/**
* @var TraceBuilder&MockObject
*/
private $exceptionTraceBuilder;

/**
* @var TraceBuilder&MockObject
*/
private $previousExceptionTraceBuilder;

private Console $writer;
private FileFactory&MockObject $fileFactory;
private TTY&MockObject $stdout;
private TTY&MockObject $stderr;

public function setUp(): void
{
Expand All @@ -78,9 +50,11 @@ public function test_NewConsole_Constructor_ShouldCreateAdaptorForStdoutAndStder
$this->fileFactory
->expects(self::exactly(2))
->method('createTTYAdaptor')
->withConsecutive(
[Console::STDOUT],
[Console::STDERR]
->with(
...self::withConsecutive(
[Console::STDOUT],
[Console::STDERR]
)
);

$this->writer = new Console($this->fileFactory);
Expand Down Expand Up @@ -119,7 +93,7 @@ public function test_ConsolePrependingLogLevelAndDateTime_LogWithLevelBelowError
{
$this->givenFactoryReturnFileAdaptors();
$this->expectsWriteToBeCalled($this->stdout,
self::matchesRegularExpression('/' . self::DATETIME_REGEX . '' . self::INTERPOLATED_MESSAGE_WITH_LOG_LEVEL . '/'));
self::matchesRegularExpression('/' . self::DATETIME_REGEX . self::INTERPOLATED_MESSAGE_WITH_LOG_LEVEL . '/'));
$this->writer = new Console($this->fileFactory);
$this->writer->setPrependLogLevel();
$this->writer->setPrependDateTime();
Expand All @@ -130,8 +104,8 @@ public function test_ConsolePrependingLogLevelAndDateTime_LogWithLevelBelowError
public function test_Console_SetForceAnsiColorSupport_ShouldCallSetForceAnsiColorSupportOnStdoutAndStdError()
{
$this->givenFactoryReturnFileAdaptors();
$this->expectsSetForceAnsiColorSupportToBeCalled($this->stdout, true);
$this->expectsSetForceAnsiColorSupportToBeCalled($this->stderr, true);
$this->expectsSetForceAnsiColorSupportToBeCalled($this->stdout);
$this->expectsSetForceAnsiColorSupportToBeCalled($this->stderr);
$this->writer = new Console($this->fileFactory);

$this->writer->setForceAnsiColorSupport();
Expand All @@ -140,8 +114,8 @@ public function test_Console_SetForceAnsiColorSupport_ShouldCallSetForceAnsiColo
public function test_Console_SetForceNoAnsiColorSupport_ShouldCallSetForceNoAnsiColorSupportOnStdoutAndStdError()
{
$this->givenFactoryReturnFileAdaptors();
$this->expectsSetForceNoAnsiColorSupportToBeCalled($this->stdout, true);
$this->expectsSetForceNoAnsiColorSupportToBeCalled($this->stderr, true);
$this->expectsSetForceNoAnsiColorSupportToBeCalled($this->stdout);
$this->expectsSetForceNoAnsiColorSupportToBeCalled($this->stderr);
$this->writer = new Console($this->fileFactory);

$this->writer->setForceNoAnsiColorSupport();
Expand All @@ -160,7 +134,7 @@ public function test_ContextContainingExceptionAndLogLevelLowerThanError_Log_Sho
$writer = new Console($this->fileFactory);
$context = [
self::CONTEXT_KEY => self::CONTEXT_VALUE,
Logger::EXCEPTION_CONTEXT => new \Exception(self::EXCEPTION_MESSAGE)
Logger::EXCEPTION_CONTEXT => new Exception(self::EXCEPTION_MESSAGE)
];

$writer->log(self::LOGLEVEL_BELOW_ERROR, self::A_MESSAGE, $context);
Expand All @@ -170,19 +144,19 @@ public function test_ContextContainingExceptionAndLogLevelIsErrorAndTraceBuilder
)
{
$this->givenFactoryReturnFileAdaptors();
$this->exceptionTraceBuilder = $this->createMock(TraceBuilder::class);
$exceptionTraceBuilder = $this->createMock(TraceBuilder::class);
$this->expectsWriteToBeCalledWithConsecutive($this->stderr, [
[self::INTERPOLATED_MESSAGE, AnsiTextColor::WHITE, AnsiBackgroundColor::RED],
[self::matches(self::EXCEPTION_TITLE_LINE_FORMAT)],
[self::anything()],
[''] // Because we can't mock exceptions, can't be sure it's really the stacktrace...
]);
$exception = new Exception(self::EXCEPTION_MESSAGE);
$this->exceptionTraceBuilder->expects(self::once())
$exceptionTraceBuilder->expects(self::once())
->method('getTraceAsString')
->willReturn($exception->getTraceAsString());

$writer = new Console($this->fileFactory, $this->exceptionTraceBuilder);
$writer = new Console($this->fileFactory, $exceptionTraceBuilder);
$context = [
self::CONTEXT_KEY => self::CONTEXT_VALUE,
Logger::EXCEPTION_CONTEXT => $exception
Expand Down Expand Up @@ -210,13 +184,13 @@ public function test_ContextContainingExceptionAndLogLevelIsErrorAndNoTraceBuild
$writer->log(LogLevel::ERROR, self::A_MESSAGE, $context);
}

public function test_ContextContainingExceptionWithPreviousExceptionAndLogLevelIsErrorAndPreviouxExceptionTraceBuilder_Log_ShouldWriteMessageAndStacktraceForPreviousException(
public function test_ContextContainingExceptionWithPreviousExceptionAndLogLevelIsErrorAndPreviousExceptionTraceBuilder_Log_ShouldWriteMessageAndStacktraceForPreviousException(
)
{
$this->givenFactoryReturnFileAdaptors();
$this->previousExceptionTraceBuilder = $this->createMock(TraceBuilder::class);
$previous_exception = new \Exception(self::PREVIOUS_EXCEPTION_MESSAGE);
$exception = new \Exception(self::EXCEPTION_MESSAGE, 0, $previous_exception);
$previousExceptionTraceBuilder = $this->createMock(TraceBuilder::class);
$previous_exception = new Exception(self::PREVIOUS_EXCEPTION_MESSAGE);
$exception = new Exception(self::EXCEPTION_MESSAGE, 0, $previous_exception);
$this->expectsWriteToBeCalledWithConsecutive($this->stderr, [
[self::INTERPOLATED_MESSAGE, AnsiTextColor::WHITE, AnsiBackgroundColor::RED],
[self::matches(self::EXCEPTION_TITLE_LINE_FORMAT)],
Expand All @@ -225,11 +199,11 @@ public function test_ContextContainingExceptionWithPreviousExceptionAndLogLevelI
[$previous_exception->getTraceAsString()],
['']
]);
$this->previousExceptionTraceBuilder->expects(self::once())
$previousExceptionTraceBuilder->expects(self::once())
->method('getTraceAsString')
->willReturn($previous_exception->getTraceAsString());

$writer = new Console($this->fileFactory, null, $this->previousExceptionTraceBuilder);
$writer = new Console($this->fileFactory, null, $previousExceptionTraceBuilder);
$context = [
self::CONTEXT_KEY => self::CONTEXT_VALUE,
Logger::EXCEPTION_CONTEXT => $exception
Expand All @@ -242,8 +216,8 @@ public function test_ContextContainingExceptionWithPreviousExceptionAndLogLevelI
)
{
$this->givenFactoryReturnFileAdaptors();
$previous_exception = new \Exception(self::PREVIOUS_EXCEPTION_MESSAGE);
$exception = new \Exception(self::EXCEPTION_MESSAGE, 0, $previous_exception);
$previous_exception = new Exception(self::PREVIOUS_EXCEPTION_MESSAGE);
$exception = new Exception(self::EXCEPTION_MESSAGE, 0, $previous_exception);
$this->expectsWriteToBeCalledWithConsecutive($this->stderr, [
[self::INTERPOLATED_MESSAGE, AnsiTextColor::WHITE, AnsiBackgroundColor::RED],
[self::matches(self::EXCEPTION_TITLE_LINE_FORMAT)],
Expand Down Expand Up @@ -274,45 +248,30 @@ private function givenFactoryReturnFileAdaptors()
]));
}

/**
* @param TTY&MockObject $file
* @param $message
* @param null $text_color
* @param null $background_color
*/
private function expectsWriteToBeCalled($file, $message, $text_color = null, $background_color = null)
private function expectsWriteToBeCalled(TTY&MockObject $file, $message, $text_color = null, $background_color = null)
{
$file->expects(self::once())->method('write')->with($message, $text_color, $background_color);
}

/**
* @param TTY&MockObject $file
* @param $with
*/
private function expectsSetForceAnsiColorSupportToBeCalled($file, $with)
private function expectsSetForceAnsiColorSupportToBeCalled(TTY&MockObject $file)
{
$file->expects(self::once())->method('setForceAnsiColorSupport')->with($with);
$file->expects(self::once())->method('setForceAnsiColorSupport')->with(true);
}

/**
* @param TTY&MockObject $file
* @param $with
*/
private function expectsSetForceNoAnsiColorSupportToBeCalled($file, $with)
private function expectsSetForceNoAnsiColorSupportToBeCalled(TTY&MockObject $file)
{
$file->expects(self::once())->method('setForceNoAnsiColorSupport')->with($with);
$file->expects(self::once())->method('setForceNoAnsiColorSupport')->with(true);
}

/**
* @param TTY&MockObject $file
* @param array $consecutive_args
*/
private function expectsWriteToBeCalledWithConsecutive($file, array $consecutive_args)
private function expectsWriteToBeCalledWithConsecutive(TTY&MockObject $file, array $consecutive_args)
{
$method = $file
$file
->expects(self::exactly(count($consecutive_args)))
->method('write');
call_user_func_array([$method, 'withConsecutive'], $consecutive_args);
->method('write')
->with(
...self::withConsecutive(
...$consecutive_args
)
);
}

}
Loading