diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index d1ab0a1ec2..3260f6ea01 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -499,7 +499,7 @@ protected function setUp() { } // Ensure that the test is not marked as risky because of no assertions. In - // PHPIUnit 6 tests that only make assertions using $this->assertSession() + // PHPUnit 6 tests that only make assertions using $this->assertSession() // can be marked as risky. $this->addToAssertionCount(1); } diff --git a/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php index 8be9064ac2..1b5b23f347 100644 --- a/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php +++ b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php @@ -70,7 +70,12 @@ protected function setUp() { public function testConstruct() { $container_definition = $this->getMockContainerDefinition(); $container_definition['machine_format'] = !$this->machineFormat; - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } $container = new $this->containerClass($container_definition); } @@ -93,7 +98,12 @@ public function testGetParameter() { * @covers ::getAlternatives */ public function testGetParameterIfNotFound() { - $this->setExpectedException(ParameterNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ParameterNotFoundException::class); + } + else { + $this->setExpectedException(ParameterNotFoundException::class); + } $this->container->getParameter('parameter_that_does_not_exist'); } @@ -103,7 +113,12 @@ public function testGetParameterIfNotFound() { * @covers ::getParameter */ public function testGetParameterIfNotFoundBecauseNull() { - $this->setExpectedException(ParameterNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ParameterNotFoundException::class); + } + else { + $this->setExpectedException(ParameterNotFoundException::class); + } $this->container->getParameter(NULL); } @@ -137,7 +152,12 @@ public function testSetParameterWithUnfrozenContainer() { */ public function testSetParameterWithFrozenContainer() { $this->container = new $this->containerClass($this->containerDefinition); - $this->setExpectedException(LogicException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(LogicException::class); + } + else { + $this->setExpectedException(LogicException::class); + } $this->container->setParameter('some_config', 'new_value'); } @@ -242,7 +262,12 @@ public function testHasForAliasedService() { * @covers ::createService */ public function testGetForCircularServices() { - $this->setExpectedException(ServiceCircularReferenceException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ServiceCircularReferenceException::class); + } + else { + $this->setExpectedException(ServiceCircularReferenceException::class); + } $this->container->get('circular_dependency'); } @@ -255,7 +280,12 @@ public function testGetForCircularServices() { * @covers ::getServiceAlternatives */ public function testGetForNonExistantService() { - $this->setExpectedException(ServiceNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ServiceNotFoundException::class); + } + else { + $this->setExpectedException(ServiceNotFoundException::class); + } $this->container->get('service_not_exists'); } @@ -304,7 +334,12 @@ public function testGetForParameterDependencyWithExceptionOnSecondCall() { // Reset the service. $this->container->set('service_parameter_not_exists', NULL); - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } $this->container->get('service_parameter_not_exists'); } @@ -316,7 +351,12 @@ public function testGetForParameterDependencyWithExceptionOnSecondCall() { * @covers ::resolveServicesAndParameters */ public function testGetForNonExistantParameterDependencyWithException() { - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } $this->container->get('service_parameter_not_exists'); } @@ -341,7 +381,12 @@ public function testGetForNonExistantServiceDependency() { * @covers ::getAlternatives */ public function testGetForNonExistantServiceDependencyWithException() { - $this->setExpectedException(ServiceNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ServiceNotFoundException::class); + } + else { + $this->setExpectedException(ServiceNotFoundException::class); + } $this->container->get('service_dependency_not_exists'); } @@ -361,7 +406,12 @@ public function testGetForNonExistantServiceWhenUsingNull() { * @covers ::createService */ public function testGetForNonExistantNULLService() { - $this->setExpectedException(ServiceNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ServiceNotFoundException::class); + } + else { + $this->setExpectedException(ServiceNotFoundException::class); + } $this->container->get(NULL); } @@ -387,7 +437,12 @@ public function testGetForNonExistantServiceMultipleTimes() { */ public function testGetForNonExistantServiceWithExceptionOnSecondCall() { $this->assertNull($this->container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does nto throw exception.'); - $this->setExpectedException(ServiceNotFoundException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(ServiceNotFoundException::class); + } + else { + $this->setExpectedException(ServiceNotFoundException::class); + } $this->container->get('service_not_exists'); } @@ -423,7 +478,12 @@ public function testGetForSyntheticService() { * @covers ::createService */ public function testGetForSyntheticServiceWithException() { - $this->setExpectedException(RuntimeException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(RuntimeException::class); + } + else { + $this->setExpectedException(RuntimeException::class); + } $this->container->get('synthetic'); } @@ -462,7 +522,12 @@ public function testGetForInstantiationWithVariousArgumentLengths() { * @covers ::createService */ public function testGetForWrongFactory() { - $this->setExpectedException(RuntimeException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(RuntimeException::class); + } + else { + $this->setExpectedException(RuntimeException::class); + } $this->container->get('wrong_factory'); } @@ -500,7 +565,12 @@ public function testGetForFactoryClass() { * @covers ::createService */ public function testGetForConfiguratorWithException() { - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } $this->container->get('configurable_service_exception'); } @@ -598,7 +668,12 @@ public function testResolveServicesAndParametersForOptionalServiceDependencies() * @covers ::resolveServicesAndParameters */ public function testResolveServicesAndParametersForInvalidArgument() { - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } $this->container->get('invalid_argument_service'); } @@ -612,7 +687,12 @@ public function testResolveServicesAndParametersForInvalidArgument() { public function testResolveServicesAndParametersForInvalidArguments() { // In case the machine-optimized format is not used, we need to simulate the // test failure. - $this->setExpectedException(InvalidArgumentException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(InvalidArgumentException::class); + } + else { + $this->setExpectedException(InvalidArgumentException::class); + } if (!$this->machineFormat) { throw new InvalidArgumentException('Simulating the test failure.'); } @@ -983,31 +1063,6 @@ protected function getCollection($collection, $resolve = TRUE) { ]; } - /** - * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. - * - * @param mixed $class - * The expected exception class. - * @param string $message - * The expected exception message. - * @param int $exception_code - * The expected exception code. - */ - public function setExpectedException($class, $message = '', $exception_code = NULL) { - if (method_exists($this, 'expectException')) { - $this->expectException($class); - if (!empty($message)) { - $this->expectExceptionMessage($message); - } - if ($exception_code !== NULL) { - $this->expectExceptionCode($exception_code); - } - } - else { - parent::setExpectedException($class, $message, $exception_code); - } - } - } /** diff --git a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php index 639053623f..de24172904 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php @@ -50,7 +50,13 @@ public function testGetPluginClassWithValidObjectPluginDefinition() { * @covers ::getPluginClass */ public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() { - $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.'); + if (method_exists($this, 'expectException')) { + $this->expectException(PluginException::class); + $this->expectExceptionMessage('The plugin (cherry) did not specify an instance class.'); + } + else { + $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.'); + } DefaultFactory::getPluginClass('cherry', []); } @@ -61,7 +67,13 @@ public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() { */ public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() { $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock(); - $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.'); + if (method_exists($this, 'expectException')) { + $this->expectException(PluginException::class); + $this->expectExceptionMessage('The plugin (cherry) did not specify an instance class.'); + } + else { + $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.'); + } DefaultFactory::getPluginClass('cherry', $plugin_definition); } @@ -71,7 +83,13 @@ public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() { * @covers ::getPluginClass */ public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() { - $this->setExpectedException(PluginException::class, 'Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.'); + if (method_exists($this, 'expectException')) { + $this->expectException(PluginException::class); + $this->expectExceptionMessage('Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.'); + } + else { + $this->setExpectedException(PluginException::class, 'Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.'); + } DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']); } @@ -86,7 +104,12 @@ public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition $plugin_definition->expects($this->atLeastOnce()) ->method('getClass') ->willReturn($plugin_class); - $this->setExpectedException(PluginException::class); + if (method_exists($this, 'expectException')) { + $this->expectException(PluginException::class); + } + else { + $this->setExpectedException(PluginException::class); + } DefaultFactory::getPluginClass('kiwifruit', $plugin_definition); } @@ -125,7 +148,13 @@ public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() { */ public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() { $plugin_class = Kale::class; - $this->setExpectedException(PluginException::class, 'Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.'); + if (method_exists($this, 'expectException')) { + $this->expectException(PluginException::class); + $this->expectExceptionMessage('Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.'); + } + else { + $this->setExpectedException(PluginException::class, 'Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.'); + } DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], FruitInterface::class); } @@ -140,33 +169,13 @@ public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDe $plugin_definition->expects($this->atLeastOnce()) ->method('getClass') ->willReturn($plugin_class); - $this->setExpectedException(PluginException::class); - DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class); - } - - /** - * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. - * - * @param mixed $class - * The expected exception class. - * @param string $message - * The expected exception message. - * @param int $exception_code - * The expected exception code. - */ - public function setExpectedException($class, $message = '', $exception_code = NULL) { if (method_exists($this, 'expectException')) { - $this->expectException($class); - if (!empty($message)) { - $this->expectExceptionMessage($message); - } - if ($exception_code !== NULL) { - $this->expectExceptionCode($exception_code); - } + $this->expectException(PluginException::class); } else { - parent::setExpectedException($class, $message, $exception_code); + $this->setExpectedException(PluginException::class); } + DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class); } }