Skip to content

Commit bd181da

Browse files
committed
[Tests] Move expectException closer to the place of the expectation to avoid false positives
1 parent 73f881e commit bd181da

File tree

8 files changed

+43
-34
lines changed

8 files changed

+43
-34
lines changed

Tests/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPassTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class RegisterEventListenersAndSubscribersPassTest extends TestCase
2727

2828
public function testExceptionOnAbstractTaggedSubscriber()
2929
{
30-
$this->expectException(\InvalidArgumentException::class);
3130
$container = $this->createBuilder();
3231

3332
$abstractDefinition = new Definition('stdClass');
@@ -36,12 +35,13 @@ public function testExceptionOnAbstractTaggedSubscriber()
3635

3736
$container->setDefinition('a', $abstractDefinition);
3837

38+
$this->expectException(\InvalidArgumentException::class);
39+
3940
$this->process($container);
4041
}
4142

4243
public function testExceptionOnAbstractTaggedListener()
4344
{
44-
$this->expectException(\InvalidArgumentException::class);
4545
$container = $this->createBuilder();
4646

4747
$abstractDefinition = new Definition('stdClass');
@@ -50,6 +50,8 @@ public function testExceptionOnAbstractTaggedListener()
5050

5151
$container->setDefinition('a', $abstractDefinition);
5252

53+
$this->expectException(\InvalidArgumentException::class);
54+
5355
$this->process($container);
5456
}
5557

Tests/DependencyInjection/CompilerPass/RegisterMappingsPassTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class RegisterMappingsPassTest extends TestCase
2020
{
2121
public function testNoDriverParmeterException()
2222
{
23+
$container = $this->createBuilder();
24+
2325
$this->expectException(\InvalidArgumentException::class);
2426
$this->expectExceptionMessage('Could not find the manager name parameter in the container. Tried the following parameter names: "manager.param.one", "manager.param.two"');
25-
$container = $this->createBuilder();
27+
2628
$this->process($container, [
2729
'manager.param.one',
2830
'manager.param.two',

Tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ protected function setUp(): void
5858

5959
public function testFixManagersAutoMappingsWithTwoAutomappings()
6060
{
61-
$this->expectException(\LogicException::class);
6261
$emConfigs = [
6362
'em1' => [
6463
'auto_mapping' => true,
@@ -76,6 +75,8 @@ public function testFixManagersAutoMappingsWithTwoAutomappings()
7675
$reflection = new \ReflectionClass($this->extension);
7776
$method = $reflection->getMethod('fixManagersAutoMappings');
7877

78+
$this->expectException(\LogicException::class);
79+
7980
$method->invoke($this->extension, $emConfigs, $bundles);
8081
}
8182

@@ -255,8 +256,6 @@ public function testServiceCacheDriver()
255256

256257
public function testUnrecognizedCacheDriverException()
257258
{
258-
$this->expectException(\InvalidArgumentException::class);
259-
$this->expectExceptionMessage('"unrecognized_type" is an unrecognized Doctrine cache driver.');
260259
$cacheName = 'metadata_cache';
261260
$container = $this->createContainer();
262261
$objectManager = [
@@ -266,6 +265,9 @@ public function testUnrecognizedCacheDriverException()
266265
],
267266
];
268267

268+
$this->expectException(\InvalidArgumentException::class);
269+
$this->expectExceptionMessage('"unrecognized_type" is an unrecognized Doctrine cache driver.');
270+
269271
$this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
270272
}
271273

Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
152152

153153
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
154154
{
155-
$this->expectException(LogicException::class);
156-
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
157-
158155
$loader = new DoctrineChoiceLoader(
159156
$this->om,
160157
$this->class,
@@ -169,7 +166,10 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
169166
->with($this->obj2)
170167
->willReturn('2');
171168

172-
$this->assertSame(['2'], $loader->loadValuesForChoices([$this->obj2]));
169+
$this->expectException(LogicException::class);
170+
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
171+
172+
$loader->loadValuesForChoices([$this->obj2]);
173173
}
174174

175175
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntIdAndValueGiven()
@@ -253,18 +253,13 @@ public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
253253

254254
public function testLegacyLoadChoicesForValuesLoadsOnlyChoicesIfValueUseIdReader()
255255
{
256-
$this->expectException(LogicException::class);
257-
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
258-
259256
$loader = new DoctrineChoiceLoader(
260257
$this->om,
261258
$this->class,
262259
$this->idReader,
263260
$this->objectLoader
264261
);
265262

266-
$choices = [$this->obj2, $this->obj3];
267-
268263
$this->idReader->expects($this->any())
269264
->method('getIdField')
270265
->willReturn('idField');
@@ -275,10 +270,10 @@ public function testLegacyLoadChoicesForValuesLoadsOnlyChoicesIfValueUseIdReader
275270
$this->objectLoader->expects($this->never())
276271
->method('getEntitiesByIds');
277272

278-
$this->assertSame(
279-
[4 => $this->obj3, 7 => $this->obj2],
280-
$loader->loadChoicesForValues([4 => '3', 7 => '2'])
281-
);
273+
$this->expectException(LogicException::class);
274+
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
275+
276+
$loader->loadChoicesForValues([4 => '3', 7 => '2']);
282277
}
283278

284279
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueUseIdReader()
@@ -374,15 +369,15 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
374369

375370
public function testPassingIdReaderWithoutSingleIdEntity()
376371
{
377-
$this->expectException(\InvalidArgumentException::class);
378-
$this->expectExceptionMessage('The "$idReader" argument of "Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\DoctrineChoiceLoader::__construct" must be null when the query cannot be optimized because of composite id fields.');
379-
380372
$idReader = $this->createMock(IdReader::class);
381373
$idReader->expects($this->once())
382374
->method('isSingleId')
383375
->willReturn(false)
384376
;
385377

378+
$this->expectException(\InvalidArgumentException::class);
379+
$this->expectExceptionMessage('The "$idReader" argument of "Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\DoctrineChoiceLoader::__construct" must be null when the query cannot be optimized because of composite id fields.');
380+
386381
new DoctrineChoiceLoader($this->om, $this->class, $idReader, $this->objectLoader);
387382
}
388383
}

Tests/Form/Type/EntityTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
218218
public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
219219
{
220220
$this->expectException(UnexpectedTypeException::class);
221-
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
221+
$this->factory->createNamed('name', static::TESTED_TYPE, null, [
222222
'em' => 'default',
223223
'class' => self::SINGLE_IDENT_CLASS,
224224
'query_builder' => fn () => new \stdClass(),

Tests/Messenger/DoctrineTransactionMiddlewareTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ public function testMiddlewareWrapsInTransactionAndFlushes()
5656

5757
public function testTransactionIsRolledBackOnException()
5858
{
59-
$this->expectException(\RuntimeException::class);
60-
$this->expectExceptionMessage('Thrown from next middleware.');
6159
$this->connection->expects($this->once())
6260
->method('beginTransaction')
6361
;
6462
$this->connection->expects($this->once())
6563
->method('rollBack')
6664
;
6765

66+
$this->expectException(\RuntimeException::class);
67+
$this->expectExceptionMessage('Thrown from next middleware.');
68+
6869
$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
6970
}
7071

Tests/Security/User/EntityUserProviderTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ public function testLoadUserByIdentifierWithUserLoaderRepositoryAndWithoutProper
8989

9090
public function testLoadUserByIdentifierWithNonUserLoaderRepositoryAndWithoutProperty()
9191
{
92-
$this->expectException(\InvalidArgumentException::class);
93-
$this->expectExceptionMessage('You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.');
9492
$em = DoctrineTestHelper::createTestEntityManager();
9593
$this->createSchema($em);
9694

@@ -100,6 +98,10 @@ public function testLoadUserByIdentifierWithNonUserLoaderRepositoryAndWithoutPro
10098
$em->flush();
10199

102100
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
101+
102+
$this->expectException(\InvalidArgumentException::class);
103+
$this->expectExceptionMessage('You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.');
104+
103105
$provider->loadUserByIdentifier('user1');
104106
}
105107

@@ -171,14 +173,15 @@ public function testLoadUserByIdentifierShouldLoadUserWhenProperInterfaceProvide
171173

172174
public function testLoadUserByIdentifierShouldDeclineInvalidInterface()
173175
{
174-
$this->expectException(\InvalidArgumentException::class);
175176
$repository = $this->createMock(ObjectRepository::class);
176177

177178
$provider = new EntityUserProvider(
178179
$this->getManager($this->getObjectManager($repository)),
179180
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
180181
);
181182

183+
$this->expectException(\InvalidArgumentException::class);
184+
182185
$provider->loadUserByIdentifier('name');
183186
}
184187

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ public function testValidateUniquenessWithArrayValue()
617617

618618
public function testDedicatedEntityManagerNullObject()
619619
{
620-
$this->expectException(ConstraintDefinitionException::class);
621-
$this->expectExceptionMessage('Object manager "foo" does not exist.');
622620
$constraint = new UniqueEntity([
623621
'message' => 'myMessage',
624622
'fields' => ['name'],
@@ -632,13 +630,14 @@ public function testDedicatedEntityManagerNullObject()
632630

633631
$entity = new SingleIntIdEntity(1, null);
634632

633+
$this->expectException(ConstraintDefinitionException::class);
634+
$this->expectExceptionMessage('Object manager "foo" does not exist.');
635+
635636
$this->validator->validate($entity, $constraint);
636637
}
637638

638639
public function testEntityManagerNullObject()
639640
{
640-
$this->expectException(ConstraintDefinitionException::class);
641-
$this->expectExceptionMessage('Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"');
642641
$constraint = new UniqueEntity([
643642
'message' => 'myMessage',
644643
'fields' => ['name'],
@@ -652,6 +651,9 @@ public function testEntityManagerNullObject()
652651

653652
$entity = new SingleIntIdEntity(1, null);
654653

654+
$this->expectException(ConstraintDefinitionException::class);
655+
$this->expectExceptionMessage('Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"');
656+
655657
$this->validator->validate($entity, $constraint);
656658
}
657659

@@ -719,8 +721,6 @@ public function testValidateInheritanceUniqueness()
719721

720722
public function testInvalidateRepositoryForInheritance()
721723
{
722-
$this->expectException(ConstraintDefinitionException::class);
723-
$this->expectExceptionMessage('The "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity" entity repository does not support the "Symfony\Bridge\Doctrine\Tests\Fixtures\Person" entity. The entity should be an instance of or extend "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity".');
724724
$constraint = new UniqueEntity([
725725
'message' => 'myMessage',
726726
'fields' => ['name'],
@@ -729,6 +729,10 @@ public function testInvalidateRepositoryForInheritance()
729729
]);
730730

731731
$entity = new Person(1, 'Foo');
732+
733+
$this->expectException(ConstraintDefinitionException::class);
734+
$this->expectExceptionMessage('The "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity" entity repository does not support the "Symfony\Bridge\Doctrine\Tests\Fixtures\Person" entity. The entity should be an instance of or extend "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity".');
735+
732736
$this->validator->validate($entity, $constraint);
733737
}
734738

0 commit comments

Comments
 (0)