Skip to content

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 8248745 commit 8888741
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Tests/File/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests\File;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\File;

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ public function testGuessExtensionIsBasedOnMimeType()

public function testConstructWhenFileNotExists()
{
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException::class);
$this->expectException(FileNotFoundException::class);

new File(__DIR__.'/Fixtures/not_here');
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/File/MimeType/MimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\HttpFoundation\Tests\File\MimeType;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;

Expand Down Expand Up @@ -39,7 +41,7 @@ public function testGuessImageWithoutExtension()

public function testGuessImageWithDirectory()
{
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException::class);
$this->expectException(FileNotFoundException::class);

MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/directory');
}
Expand Down Expand Up @@ -68,7 +70,7 @@ public function testGuessWithDuplicatedFileType()

public function testGuessWithIncorrectPath()
{
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException::class);
$this->expectException(FileNotFoundException::class);
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
}

Expand All @@ -87,7 +89,7 @@ public function testGuessWithNonReadablePath()
@chmod($path, 0333);

if ('0333' == substr(sprintf('%o', fileperms($path)), -4)) {
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException::class);
$this->expectException(AccessDeniedException::class);
MimeTypeGuesser::getInstance()->guess($path);
} else {
$this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
Expand Down
3 changes: 2 additions & 1 deletion Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
Expand Down Expand Up @@ -1087,7 +1088,7 @@ public function getClientIpsProvider()
*/
public function testGetClientIpsWithConflictingHeaders($httpForwarded, $httpXForwardedFor)
{
$this->expectException(\Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException::class);
$this->expectException(ConflictingHeadersException::class);
$request = new Request();

$server = [
Expand Down
2 changes: 1 addition & 1 deletion Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public function testSettersAreChainable()
public function testNoDeprecationsAreTriggered()
{
new DefaultResponse();
$this->getMockBuilder(Response::class)->getMock();
$this->createMock(Response::class);

// we just need to ensure that subclasses of Response can be created without any deprecations
// being triggered if the subclass does not override any final methods
Expand Down
6 changes: 4 additions & 2 deletions Tests/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionBagProxy;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

/**
Expand Down Expand Up @@ -212,7 +214,7 @@ public function testGetId()

public function testGetFlashBag()
{
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface::class, $this->session->getFlashBag());
$this->assertInstanceOf(FlashBagInterface::class, $this->session->getFlashBag());
}

public function testGetIterator()
Expand Down Expand Up @@ -241,7 +243,7 @@ public function testGetCount()

public function testGetMeta()
{
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\Session\Storage\MetadataBag::class, $this->session->getMetadataBag());
$this->assertInstanceOf(MetadataBag::class, $this->session->getMetadataBag());
}

public function testIsEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function setUp(): void
$this->markTestSkipped('Tests can only be run with memcached extension 2.1.0 or lower, or 3.0.0b1 or higher');
}

$this->memcached = $this->getMockBuilder(\Memcached::class)->getMock();
$this->memcached = $this->createMock(\Memcached::class);
$this->storage = new MemcachedSessionHandler(
$this->memcached,
['prefix' => self::PREFIX, 'expiretime' => self::TTL]
Expand Down
6 changes: 3 additions & 3 deletions Tests/Session/Storage/Handler/PdoSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testReadWriteReadWithNullByte()
public function testReadConvertsStreamToString()
{
$pdo = new MockPdo('pgsql');
$pdo->prepareResult = $this->getMockBuilder(\PDOStatement::class)->getMock();
$pdo->prepareResult = $this->createMock(\PDOStatement::class);

$content = 'foobar';
$stream = $this->createStream($content);
Expand All @@ -152,8 +152,8 @@ public function testReadLockedConvertsStreamToString()
}

$pdo = new MockPdo('pgsql');
$selectStmt = $this->getMockBuilder(\PDOStatement::class)->getMock();
$insertStmt = $this->getMockBuilder(\PDOStatement::class)->getMock();
$selectStmt = $this->createMock(\PDOStatement::class);
$insertStmt = $this->createMock(\PDOStatement::class);

$pdo->prepareResult = function ($statement) use ($selectStmt, $insertStmt) {
return 0 === strpos($statement, 'INSERT') ? $insertStmt : $selectStmt;
Expand Down
30 changes: 15 additions & 15 deletions Tests/Session/Storage/Handler/StrictSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class StrictSessionHandlerTest extends TestCase
{
public function testOpen()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('open')
->with('path', 'name')->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand All @@ -31,7 +31,7 @@ public function testOpen()

public function testCloseSession()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('close')
->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand All @@ -41,7 +41,7 @@ public function testCloseSession()

public function testValidateIdOK()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('data');
$proxy = new StrictSessionHandler($handler);
Expand All @@ -51,7 +51,7 @@ public function testValidateIdOK()

public function testValidateIdKO()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('');
$proxy = new StrictSessionHandler($handler);
Expand All @@ -61,7 +61,7 @@ public function testValidateIdKO()

public function testRead()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('data');
$proxy = new StrictSessionHandler($handler);
Expand All @@ -71,7 +71,7 @@ public function testRead()

public function testReadWithValidateIdOK()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('data');
$proxy = new StrictSessionHandler($handler);
Expand All @@ -82,7 +82,7 @@ public function testReadWithValidateIdOK()

public function testReadWithValidateIdMismatch()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->exactly(2))->method('read')
->withConsecutive(['id1'], ['id2'])
->will($this->onConsecutiveCalls('data1', 'data2'));
Expand All @@ -94,7 +94,7 @@ public function testReadWithValidateIdMismatch()

public function testUpdateTimestamp()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('write')
->with('id', 'data')->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand All @@ -104,7 +104,7 @@ public function testUpdateTimestamp()

public function testWrite()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('write')
->with('id', 'data')->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand All @@ -114,7 +114,7 @@ public function testWrite()

public function testWriteEmptyNewSession()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('');
$handler->expects($this->never())->method('write');
Expand All @@ -128,7 +128,7 @@ public function testWriteEmptyNewSession()

public function testWriteEmptyExistingSession()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('data');
$handler->expects($this->never())->method('write');
Expand All @@ -141,7 +141,7 @@ public function testWriteEmptyExistingSession()

public function testDestroy()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('destroy')
->with('id')->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand All @@ -151,7 +151,7 @@ public function testDestroy()

public function testDestroyNewSession()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('');
$handler->expects($this->once())->method('destroy')->willReturn(true);
Expand All @@ -163,7 +163,7 @@ public function testDestroyNewSession()

public function testDestroyNonEmptyNewSession()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('read')
->with('id')->willReturn('');
$handler->expects($this->once())->method('write')
Expand All @@ -179,7 +179,7 @@ public function testDestroyNonEmptyNewSession()

public function testGc()
{
$handler = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$handler = $this->createMock(\SessionHandlerInterface::class);
$handler->expects($this->once())->method('gc')
->with(123)->willReturn(true);
$proxy = new StrictSessionHandler($handler);
Expand Down
6 changes: 3 additions & 3 deletions Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SessionHandlerProxyTest extends TestCase

protected function setUp(): void
{
$this->mock = $this->getMockBuilder(\SessionHandlerInterface::class)->getMock();
$this->mock = $this->createMock(\SessionHandlerInterface::class);
$this->proxy = new SessionHandlerProxy($this->mock);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public function testGc()
*/
public function testValidateId()
{
$mock = $this->getMockBuilder(TestSessionHandler::class)->getMock();
$mock = $this->createMock(TestSessionHandler::class);
$mock->expects($this->once())
->method('validateId');

Expand All @@ -142,7 +142,7 @@ public function testValidateId()
*/
public function testUpdateTimestamp()
{
$mock = $this->getMockBuilder(TestSessionHandler::class)->getMock();
$mock = $this->createMock(TestSessionHandler::class);
$mock->expects($this->once())
->method('updateTimestamp')
->willReturn(false);
Expand Down

0 comments on commit 8888741

Please sign in to comment.