Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use createMock() and use import instead of FQCN #39941

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

Merged
merged 1 commit into from
Jan 27, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Tests\DataCollector;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Version;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -236,7 +237,7 @@ private function createCollector($queries)
->method('getDatabasePlatform')
->willReturn(new MySqlPlatform());

$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$registry = $this->createMock(ManagerRegistry::class);
$registry
->expects($this->any())
->method('getConnectionNames')
Expand All @@ -249,7 +250,7 @@ private function createCollector($queries)
->method('getConnection')
->willReturn($connection);

$logger = $this->getMockBuilder(\Doctrine\DBAL\Logging\DebugStack::class)->getMock();
$logger = $this->createMock(DebugStack::class);
$logger->queries = $queries;

$collector = new DoctrineDataCollector($registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
use Symfony\Bridge\Doctrine\Tests\Fixtures\ContainerAwareFixture;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ContainerAwareLoaderTest extends TestCase
{
public function testShouldSetContainerOnContainerAwareFixture()
{
$container = $this->getMockBuilder(\Symfony\Component\DependencyInjection\ContainerInterface::class)->getMock();
$container = $this->createMock(ContainerInterface::class);
$loader = new ContainerAwareLoader($container);
$fixture = new ContainerAwareFixture();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand All @@ -22,7 +23,7 @@
class DoctrineExtensionTest extends TestCase
{
/**
* @var \Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension
* @var AbstractDoctrineExtension
*/
private $extension;

Expand All @@ -31,7 +32,7 @@ protected function setUp(): void
parent::setUp();

$this->extension = $this
->getMockBuilder(\Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension::class)
->getMockBuilder(AbstractDoctrineExtension::class)
->setMethods([
'getMappingResourceConfigDirectory',
'getObjectManagerElementName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,17 @@ class DoctrineChoiceLoaderTest extends TestCase

protected function setUp(): void
{
$this->factory = $this->getMockBuilder(ChoiceListFactoryInterface::class)->getMock();
$this->om = $this->getMockBuilder(ObjectManager::class)->getMock();
$this->repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
$this->factory = $this->createMock(ChoiceListFactoryInterface::class);
$this->om = $this->createMock(ObjectManager::class);
$this->repository = $this->createMock(ObjectRepository::class);
$this->class = 'stdClass';
$this->idReader = $this->getMockBuilder(IdReader::class)
->disableOriginalConstructor()
->getMock();
$this->idReader = $this->createMock(IdReader::class);
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true)
;

$this->objectLoader = $this->getMockBuilder(EntityLoaderInterface::class)->getMock();
$this->objectLoader = $this->createMock(EntityLoaderInterface::class);
$this->obj1 = (object) ['name' => 'A'];
$this->obj2 = (object) ['name' => 'B'];
$this->obj3 = (object) ['name' => 'C'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* @author Bernhard Schussek <[email protected]>
Expand Down Expand Up @@ -64,7 +65,7 @@ public function testTransformNull()

public function testTransformExpectsArrayOrCollection()
{
$this->expectException(\Symfony\Component\Form\Exception\TransformationFailedException::class);
$this->expectException(TransformationFailedException::class);
$this->transformer->transform('Foo');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public function requiredProvider()
$return = [];

// Simple field, not nullable
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(false);

$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];

// Simple field, nullable
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(true);

$return[] = [$classMetadata, new ValueGuess(false, Guess::MEDIUM_CONFIDENCE)];

// One-to-one, nullable (by default)
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [[]]];
Expand All @@ -57,7 +57,7 @@ public function requiredProvider()
$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];

// One-to-one, nullable (explicit)
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => true]]];
Expand All @@ -66,7 +66,7 @@ public function requiredProvider()
$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];

// One-to-one, not nullable
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => false]]];
Expand All @@ -75,7 +75,7 @@ public function requiredProvider()
$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];

// One-to-many, no clue
$classMetadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(false);

$return[] = [$classMetadata, null];
Expand All @@ -85,10 +85,10 @@ public function requiredProvider()

private function getGuesser(ClassMetadata $classMetadata)
{
$em = $this->getMockBuilder(ObjectManager::class)->getMock();
$em = $this->createMock(ObjectManager::class);
$em->expects($this->once())->method('getClassMetaData')->with('TestEntity')->willReturn($classMetadata);

$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->once())->method('getManagers')->willReturn([$em]);

return new DoctrineOrmTypeGuesser($registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;

class MergeDoctrineCollectionListenerTest extends TestCase
{
Expand All @@ -32,7 +33,7 @@ protected function setUp(): void
{
$this->collection = new ArrayCollection(['test']);
$this->dispatcher = new EventDispatcher();
$this->factory = $this->getMockBuilder(\Symfony\Component\Form\FormFactoryInterface::class)->getMock();
$this->factory = $this->createMock(FormFactoryInterface::class);
$this->form = $this->getBuilder()
->getForm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase

protected function getExtensions()
{
$manager = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$manager = $this->createMock(ManagerRegistry::class);

$manager->expects($this->any())
->method('getManager')
Expand Down
19 changes: 12 additions & 7 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringCastableIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Tests\Extension\Core\Type\BaseTypeTest;
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;

class EntityTypeTest extends BaseTypeTest
{
Expand Down Expand Up @@ -118,13 +123,13 @@ protected function persist(array $entities)

public function testClassOptionIsRequired()
{
$this->expectException(\Symfony\Component\OptionsResolver\Exception\MissingOptionsException::class);
$this->expectException(MissingOptionsException::class);
$this->factory->createNamed('name', static::TESTED_TYPE);
}

public function testInvalidClassOption()
{
$this->expectException(\Symfony\Component\Form\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->factory->createNamed('name', static::TESTED_TYPE, null, [
'class' => 'foo',
]);
Expand Down Expand Up @@ -219,7 +224,7 @@ public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()

public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
{
$this->expectException(\Symfony\Component\OptionsResolver\Exception\InvalidOptionsException::class);
$this->expectException(InvalidOptionsException::class);
$this->factory->createNamed('name', static::TESTED_TYPE, null, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
Expand All @@ -229,7 +234,7 @@ public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()

public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
{
$this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
$this->expectException(UnexpectedTypeException::class);
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
Expand Down Expand Up @@ -1242,7 +1247,7 @@ public function testLoaderCaching()
$choiceLoader2 = $form->get('property2')->getConfig()->getOption('choice_loader');
$choiceLoader3 = $form->get('property3')->getConfig()->getOption('choice_loader');

$this->assertInstanceOf(\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::class, $choiceLoader1);
$this->assertInstanceOf(ChoiceLoaderInterface::class, $choiceLoader1);
$this->assertSame($choiceLoader1, $choiceLoader2);
$this->assertSame($choiceLoader1, $choiceLoader3);
}
Expand Down Expand Up @@ -1302,14 +1307,14 @@ public function testLoaderCachingWithParameters()
$choiceLoader2 = $form->get('property2')->getConfig()->getOption('choice_loader');
$choiceLoader3 = $form->get('property3')->getConfig()->getOption('choice_loader');

$this->assertInstanceOf(\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::class, $choiceLoader1);
$this->assertInstanceOf(ChoiceLoaderInterface::class, $choiceLoader1);
$this->assertSame($choiceLoader1, $choiceLoader2);
$this->assertSame($choiceLoader1, $choiceLoader3);
}

protected function createRegistryMock($name, $em)
{
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
Expand Down
11 changes: 6 additions & 5 deletions src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Tests\Logger;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Logger\DbalLogger;

class DbalLoggerTest extends TestCase
Expand All @@ -21,7 +22,7 @@ class DbalLoggerTest extends TestCase
*/
public function testLog($sql, $params, $logParams)
{
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);

$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
Expand Down Expand Up @@ -53,7 +54,7 @@ public function getLogFixtures()

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

$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
Expand All @@ -76,7 +77,7 @@ public function testLogNonUtf8()

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

$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testLogNonUtf8Array()

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

$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
Expand Down Expand Up @@ -135,7 +136,7 @@ public function testLogLongString()

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

$dbalLogger = $this
->getMockBuilder(DbalLogger::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Tests\Security\User;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
Expand All @@ -20,6 +21,7 @@
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\User;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

Expand Down Expand Up @@ -71,9 +73,7 @@ public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty
->with('user1')
->willReturn($user);

$em = $this->getMockBuilder(\Doctrine\ORM\EntityManager::class)
->disableOriginalConstructor()
->getMock();
$em = $this->createMock(EntityManager::class);
$em
->expects($this->once())
->method('getRepository')
Expand Down Expand Up @@ -125,7 +125,7 @@ public function testRefreshInvalidUser()
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');

$user2 = new User(1, 2, 'user2');
$this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
$this->expectException(UsernameNotFoundException::class);
$this->expectExceptionMessage('User with id {"id1":1,"id2":2} not found');

$provider->refreshUser($user2);
Expand Down Expand Up @@ -155,7 +155,7 @@ public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided(
->method('loadUserByUsername')
->with('name')
->willReturn(
$this->getMockBuilder(UserInterface::class)->getMock()
$this->createMock(UserInterface::class)
);

$provider = new EntityUserProvider(
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testPasswordUpgrades()

private function getManager($em, $name = null)
{
$manager = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$manager = $this->createMock(ManagerRegistry::class);
$manager->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
Expand Down
Loading