Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
Use createMock() and use import instead of FQCN
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored and nicolas-grekas committed Jan 27, 2021
1 parent edfa86f commit af4987a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Tests/DebugClassLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Debug\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent;
use Symfony\Component\Debug\DebugClassLoader;

/**
Expand Down Expand Up @@ -179,7 +180,7 @@ public function testDeprecatedSuperInSameNamespace()
$e = error_reporting(0);
trigger_error('', E_USER_NOTICE);

class_exists(\Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent::class, true);
class_exists(ExtendsDeprecatedParent::class, true);

error_reporting($e);
restore_error_handler();
Expand Down
22 changes: 12 additions & 10 deletions Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
namespace Symfony\Component\Debug\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Symfony\Component\Debug\BufferingLogger;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
use Symfony\Component\Debug\Exception\SilencedErrorContext;
use Symfony\Component\Debug\Tests\Fixtures\ErrorHandlerThatUsesThePreviousOne;
use Symfony\Component\Debug\Tests\Fixtures\LoggerThatSetAnErrorHandler;
Expand Down Expand Up @@ -72,7 +74,7 @@ public function testRegister()

public function testErrorGetLast()
{
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$handler = ErrorHandler::register();
$handler->setDefaultLogger($logger);
$handler->screamAt(\E_ALL);
Expand Down Expand Up @@ -150,7 +152,7 @@ public function testConstruct()
public function testDefaultLogger()
{
try {
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$handler = ErrorHandler::register();

$handler->setDefaultLogger($logger, \E_NOTICE);
Expand Down Expand Up @@ -225,7 +227,7 @@ public function testHandleError()
restore_error_handler();
restore_exception_handler();

$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);

$warnArgCheck = function ($logLevel, $message, $context) {
$this->assertEquals('info', $logLevel);
Expand All @@ -250,7 +252,7 @@ public function testHandleError()
restore_error_handler();
restore_exception_handler();

$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);

$line = null;
$logArgCheck = function ($level, $message, $context) use (&$line) {
Expand Down Expand Up @@ -355,7 +357,7 @@ public function testHandleDeprecation()
$this->assertSame('User Deprecated: Foo deprecation', $exception->getMessage());
};

$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('log')
Expand All @@ -370,7 +372,7 @@ public function testHandleDeprecation()
public function testHandleException()
{
try {
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$handler = ErrorHandler::register();

$exception = new \Exception('foo');
Expand Down Expand Up @@ -450,7 +452,7 @@ public function testBootstrappingLogger()

$bootLogger->log(LogLevel::WARNING, 'Foo message', ['exception' => $exception]);

$mockLogger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$mockLogger = $this->createMock(LoggerInterface::class);
$mockLogger->expects($this->once())
->method('log')
->with(LogLevel::WARNING, 'Foo message', ['exception' => $exception]);
Expand All @@ -465,7 +467,7 @@ public function testSettingLoggerWhenExceptionIsBuffered()

$exception = new \Exception('Foo message');

$mockLogger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$mockLogger = $this->createMock(LoggerInterface::class);
$mockLogger->expects($this->once())
->method('log')
->with(LogLevel::CRITICAL, 'Uncaught Exception: Foo message', ['exception' => $exception]);
Expand All @@ -480,7 +482,7 @@ public function testSettingLoggerWhenExceptionIsBuffered()
public function testHandleFatalError()
{
try {
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$handler = ErrorHandler::register();

$error = [
Expand Down Expand Up @@ -527,7 +529,7 @@ public function testHandleErrorException()

$handler->handleException($exception);

$this->assertInstanceOf(\Symfony\Component\Debug\Exception\ClassNotFoundException::class, $args[0]);
$this->assertInstanceOf(ClassNotFoundException::class, $args[0]);
$this->assertStringStartsWith("Attempted to load class \"IReallyReallyDoNotExistAnywhereInTheRepositoryISwear\" from the global namespace.\nDid you forget a \"use\" statement", $args[0]->getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Composer\Autoload\ClassLoader as ComposerClassLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;

Expand Down Expand Up @@ -67,7 +68,7 @@ public function testHandleClassNotFound($error, $translatedMessage, $autoloader
array_map('spl_autoload_register', $autoloaders);
}

$this->assertInstanceOf(\Symfony\Component\Debug\Exception\ClassNotFoundException::class, $exception);
$this->assertInstanceOf(ClassNotFoundException::class, $exception);
$this->assertMatchesRegularExpression($translatedMessage, $exception->getMessage());
$this->assertSame($error['type'], $exception->getSeverity());
$this->assertSame($error['file'], $exception->getFile());
Expand Down Expand Up @@ -218,6 +219,6 @@ public function testCannotRedeclareClass()
$handler = new ClassNotFoundFatalErrorHandler();
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));

$this->assertInstanceOf(\Symfony\Component\Debug\Exception\ClassNotFoundException::class, $exception);
$this->assertInstanceOf(ClassNotFoundException::class, $exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\Debug\Exception\UndefinedFunctionException;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;

/**
Expand All @@ -28,7 +29,7 @@ public function testUndefinedFunction($error, $translatedMessage)
$handler = new UndefinedFunctionFatalErrorHandler();
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));

$this->assertInstanceOf(\Symfony\Component\Debug\Exception\UndefinedFunctionException::class, $exception);
$this->assertInstanceOf(UndefinedFunctionException::class, $exception);
// class names are case insensitive and PHP do not return the same
$this->assertSame(strtolower($translatedMessage), strtolower($exception->getMessage()));
$this->assertSame($error['type'], $exception->getSeverity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\Debug\Exception\UndefinedMethodException;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;

/**
Expand All @@ -28,7 +29,7 @@ public function testUndefinedMethod($error, $translatedMessage)
$handler = new UndefinedMethodFatalErrorHandler();
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));

$this->assertInstanceOf(\Symfony\Component\Debug\Exception\UndefinedMethodException::class, $exception);
$this->assertInstanceOf(UndefinedMethodException::class, $exception);
$this->assertSame($translatedMessage, $exception->getMessage());
$this->assertSame($error['type'], $exception->getSeverity());
$this->assertSame($error['file'], $exception->getFile());
Expand Down

0 comments on commit af4987a

Please sign in to comment.