Skip to content

Commit

Permalink
[Tests] Replace setMethods() by onlyMethods() and addMethods()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Mar 6, 2023
1 parent d0ec59b commit decc480
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void

$this->extension = $this
->getMockBuilder(AbstractDoctrineExtension::class)
->setMethods([
->onlyMethods([
'getMappingResourceConfigDirectory',
'getObjectManagerElementName',
'getMappingObjectDefaultName',
Expand Down
55 changes: 39 additions & 16 deletions Tests/Form/ChoiceList/ORMQueryBuilderLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Version;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
Expand Down Expand Up @@ -46,8 +48,8 @@ protected function checkIdentifierType($classname, $expectedType)
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(\QueryMock::class)
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
Expand All @@ -61,7 +63,7 @@ protected function checkIdentifierType($classname, $expectedType)

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();

$qb->expects($this->once())
Expand All @@ -79,8 +81,8 @@ public function testFilterNonIntegerValues()
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(\QueryMock::class)
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
Expand All @@ -94,7 +96,7 @@ public function testFilterNonIntegerValues()

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();

$qb->expects($this->once())
Expand All @@ -115,8 +117,8 @@ public function testFilterEmptyUuids($entityClass)
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(\QueryMock::class)
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
Expand All @@ -130,7 +132,7 @@ public function testFilterEmptyUuids($entityClass)

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();

$qb->expects($this->once())
Expand Down Expand Up @@ -160,8 +162,8 @@ public function testFilterUid($entityClass)

$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(\QueryMock::class)
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
Expand All @@ -175,7 +177,7 @@ public function testFilterUid($entityClass)

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();

$qb->expects($this->once())
Expand Down Expand Up @@ -207,7 +209,7 @@ public function testUidThrowProperException($entityClass)

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();

$qb->expects($this->never())
Expand All @@ -234,8 +236,8 @@ public function testEmbeddedIdentifierName()

$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(\QueryMock::class)
->setMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();

$query
Expand All @@ -249,7 +251,7 @@ public function testEmbeddedIdentifierName()

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
->setConstructorArgs([$em])
->setMethods(['getQuery'])
->onlyMethods(['getQuery'])
->getMock();
$qb->expects($this->once())
->method('getQuery')
Expand Down Expand Up @@ -278,3 +280,24 @@ public static function provideUidEntityClasses()
];
}
}

class QueryMock extends AbstractQuery
{
public function __construct()
{
}

/**
* @return array|string
*/
public function getSQL()
{
}

/**
* @return Result|int
*/
protected function _doExecute()
{
}
}
10 changes: 5 additions & 5 deletions Tests/Logger/DbalLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testLog($sql, $params, $logParams)
$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
->setConstructorArgs([$logger, null])
->setMethods(['log'])
->onlyMethods(['log'])
->getMock()
;

Expand Down Expand Up @@ -62,7 +62,7 @@ public function testLogNonUtf8()
$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
->setConstructorArgs([$logger, null])
->setMethods(['log'])
->onlyMethods(['log'])
->getMock()
;

Expand All @@ -85,7 +85,7 @@ public function testLogNonUtf8Array()
$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
->setConstructorArgs([$logger, null])
->setMethods(['log'])
->onlyMethods(['log'])
->getMock()
;

Expand Down Expand Up @@ -116,7 +116,7 @@ public function testLogLongString()
$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
->setConstructorArgs([$logger, null])
->setMethods(['log'])
->onlyMethods(['log'])
->getMock()
;

Expand Down Expand Up @@ -144,7 +144,7 @@ public function testLogUTF8LongString()
$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
->setConstructorArgs([$logger, null])
->setMethods(['log'])
->onlyMethods(['log'])
->getMock()
;

Expand Down
2 changes: 1 addition & 1 deletion Tests/Security/User/EntityUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private function getManager($em, $name = null)
private function getObjectManager($repository)
{
$em = $this->getMockBuilder(ObjectManager::class)
->setMethods(['getClassMetadata', 'getRepository'])
->onlyMethods(['getClassMetadata', 'getRepository'])
->getMockForAbstractClass();
$em->expects($this->any())
->method('getRepository')
Expand Down
3 changes: 2 additions & 1 deletion Tests/Validator/Constraints/UniqueEntityValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ protected function createRegistryMock($em = null)
protected function createRepositoryMock()
{
$repository = $this->getMockBuilder(ObjectRepository::class)
->setMethods(['findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'])
->onlyMethods(['find', 'findAll', 'findOneBy', 'findBy', 'getClassName'])
->addMethods(['findByCustom'])
->getMock()
;

Expand Down

0 comments on commit decc480

Please sign in to comment.