From 4dd3a4e37c0c5052a23902ee9e2866d218ac754d Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Wed, 7 Feb 2024 15:32:39 +0100 Subject: [PATCH] Closes #5248 --- composer.json | 3 - .../SoapExtensionNotAvailableException.php | 23 -- .../MockObject/Generator/Generator.php | 108 ---------- .../Generator/templates/wsdl_class.tpl | 9 - src/Framework/TestCase.php | 64 ------ tests/_files/3530.wsdl | 33 --- tests/_files/GoogleSearch.wsdl | 198 ------------------ .../mock-objects/generator/3530.phpt | 29 --- .../mock-objects/generator/wsdl_class.phpt | 37 ---- .../generator/wsdl_class_namespace.phpt | 39 ---- .../generator/wsdl_class_partial.phpt | 30 --- .../Creation/GetMockFromWsdlTest.php | 38 ---- 12 files changed, 611 deletions(-) delete mode 100644 src/Framework/MockObject/Generator/Exception/SoapExtensionNotAvailableException.php delete mode 100644 src/Framework/MockObject/Generator/templates/wsdl_class.tpl delete mode 100644 tests/_files/3530.wsdl delete mode 100644 tests/_files/GoogleSearch.wsdl delete mode 100644 tests/end-to-end/mock-objects/generator/3530.phpt delete mode 100644 tests/end-to-end/mock-objects/generator/wsdl_class.phpt delete mode 100644 tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt delete mode 100644 tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt delete mode 100644 tests/unit/Framework/MockObject/Creation/GetMockFromWsdlTest.php diff --git a/composer.json b/composer.json index a81ec54f0cc..72e30d202cb 100644 --- a/composer.json +++ b/composer.json @@ -55,9 +55,6 @@ "optimize-autoloader": true, "sort-packages": true }, - "suggest": { - "ext-soap": "To be able to generate mocks based on WSDL files" - }, "bin": [ "phpunit" ], diff --git a/src/Framework/MockObject/Generator/Exception/SoapExtensionNotAvailableException.php b/src/Framework/MockObject/Generator/Exception/SoapExtensionNotAvailableException.php deleted file mode 100644 index 37af4913145..00000000000 --- a/src/Framework/MockObject/Generator/Exception/SoapExtensionNotAvailableException.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject\Generator; - -/** - * @internal This class is not covered by the backward compatibility promise for PHPUnit - */ -final class SoapExtensionNotAvailableException extends \PHPUnit\Framework\Exception implements Exception -{ - public function __construct() - { - parent::__construct( - 'The SOAP extension is required to generate a test double from WSDL', - ); - } -} diff --git a/src/Framework/MockObject/Generator/Generator.php b/src/Framework/MockObject/Generator/Generator.php index 822f9cf827f..e8e1164c5f0 100644 --- a/src/Framework/MockObject/Generator/Generator.php +++ b/src/Framework/MockObject/Generator/Generator.php @@ -10,8 +10,6 @@ namespace PHPUnit\Framework\MockObject\Generator; use const PHP_EOL; -use const PREG_OFFSET_CAPTURE; -use const WSDL_CACHE_NONE; use function array_merge; use function array_pop; use function array_unique; @@ -19,7 +17,6 @@ use function class_exists; use function count; use function explode; -use function extension_loaded; use function implode; use function in_array; use function interface_exists; @@ -29,15 +26,9 @@ use function method_exists; use function mt_rand; use function preg_match; -use function preg_match_all; -use function range; use function serialize; use function sort; use function sprintf; -use function str_contains; -use function str_replace; -use function strlen; -use function strpos; use function substr; use function trait_exists; use Exception; @@ -61,8 +52,6 @@ use PHPUnit\Framework\MockObject\StubInternal; use ReflectionClass; use ReflectionMethod; -use SoapClient; -use SoapFault; use Throwable; use Traversable; @@ -433,103 +422,6 @@ public function generate(string $type, bool $mockObject, bool $markAsMockObject, return self::$cache[$key]; } - /** - * @throws RuntimeException - * @throws SoapExtensionNotAvailableException - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/5242 - */ - public function generateClassFromWsdl(string $wsdlFile, string $className, array $methods = [], array $options = []): string - { - if (!extension_loaded('soap')) { - throw new SoapExtensionNotAvailableException; - } - - $options['cache_wsdl'] = WSDL_CACHE_NONE; - - try { - $client = new SoapClient($wsdlFile, $options); - $_methods = array_unique($client->__getFunctions()); - - unset($client); - } catch (SoapFault $e) { - throw new RuntimeException( - $e->getMessage(), - $e->getCode(), - $e, - ); - } - - sort($_methods); - - $methodTemplate = $this->loadTemplate('wsdl_method.tpl'); - $methodsBuffer = ''; - - foreach ($_methods as $method) { - preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\(/', $method, $matches, PREG_OFFSET_CAPTURE); - - $lastFunction = array_pop($matches[0]); - $nameStart = $lastFunction[1]; - $nameEnd = $nameStart + strlen($lastFunction[0]) - 1; - $name = str_replace('(', '', $lastFunction[0]); - - if (empty($methods) || in_array($name, $methods, true)) { - $arguments = explode( - ',', - str_replace(')', '', substr($method, $nameEnd + 1)), - ); - - foreach (range(0, count($arguments) - 1) as $i) { - $parameterStart = strpos($arguments[$i], '$'); - - if (!$parameterStart) { - continue; - } - - $arguments[$i] = substr($arguments[$i], $parameterStart); - } - - $methodTemplate->setVar( - [ - 'method_name' => $name, - 'arguments' => implode(', ', $arguments), - ], - ); - - $methodsBuffer .= $methodTemplate->render(); - } - } - - $optionsBuffer = '['; - - foreach ($options as $key => $value) { - $optionsBuffer .= $key . ' => ' . $value; - } - - $optionsBuffer .= ']'; - - $classTemplate = $this->loadTemplate('wsdl_class.tpl'); - $namespace = ''; - - if (str_contains($className, '\\')) { - $parts = explode('\\', $className); - $className = array_pop($parts); - $namespace = 'namespace ' . implode('\\', $parts) . ';' . "\n\n"; - } - - $classTemplate->setVar( - [ - 'namespace' => $namespace, - 'class_name' => $className, - 'wsdl' => $wsdlFile, - 'options' => $optionsBuffer, - 'methods' => $methodsBuffer, - ], - ); - - return $classTemplate->render(); - } - /** * @throws ReflectionException * diff --git a/src/Framework/MockObject/Generator/templates/wsdl_class.tpl b/src/Framework/MockObject/Generator/templates/wsdl_class.tpl deleted file mode 100644 index b3100b41417..00000000000 --- a/src/Framework/MockObject/Generator/templates/wsdl_class.tpl +++ /dev/null @@ -1,9 +0,0 @@ -declare(strict_types=1); - -{namespace}class {class_name} extends \SoapClient -{ - public function __construct($wsdl, array $options) - { - parent::__construct('{wsdl}', $options); - } -{methods}} diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 8e487ee1449..f8a978150a6 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -15,15 +15,12 @@ use const LC_MONETARY; use const LC_NUMERIC; use const LC_TIME; -use const PATHINFO_FILENAME; use const PHP_EOL; -use const PHP_URL_PATH; use function array_keys; use function array_merge; use function array_reverse; use function array_values; use function assert; -use function basename; use function chdir; use function class_exists; use function clearstatcache; @@ -47,10 +44,7 @@ use function ob_get_contents; use function ob_get_level; use function ob_start; -use function parse_url; -use function pathinfo; use function preg_match; -use function preg_replace; use function restore_error_handler; use function restore_exception_handler; use function set_error_handler; @@ -1421,64 +1415,6 @@ final protected function createTestProxy(string $originalClassName, array $const return $testProxy; } - /** - * Creates a mock object based on the given WSDL file. - * - * @throws MockObjectException - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/5242 - */ - final protected function getMockFromWsdl(string $wsdlFile, string $originalClassName = '', string $mockClassName = '', array $methods = [], bool $callOriginalConstructor = true, array $options = []): MockObject - { - Event\Facade::emitter()->testTriggeredPhpunitDeprecation( - $this->valueObjectForEvents(), - 'getMockFromWsdl() is deprecated and will be removed in PHPUnit 12 without replacement.', - ); - - if ($originalClassName === '') { - $fileName = pathinfo(basename(parse_url($wsdlFile, PHP_URL_PATH)), PATHINFO_FILENAME); - $originalClassName = preg_replace('/\W/', '', $fileName); - } - - if (!class_exists($originalClassName)) { - eval( - (new MockGenerator)->generateClassFromWsdl( - $wsdlFile, - $originalClassName, - $methods, - $options, - ) - ); - } - - $mockObject = (new MockGenerator)->testDouble( - $originalClassName, - true, - true, - $methods, - ['', $options], - $mockClassName, - $callOriginalConstructor, - false, - false, - ); - - Event\Facade::emitter()->testCreatedMockObjectFromWsdl( - $wsdlFile, - $originalClassName, - $mockClassName, - $methods, - $callOriginalConstructor, - $options, - ); - - assert($mockObject instanceof MockObject); - - $this->registerMockObject($mockObject); - - return $mockObject; - } - protected function transformException(Throwable $t): Throwable { return $t; diff --git a/tests/_files/3530.wsdl b/tests/_files/3530.wsdl deleted file mode 100644 index b94a1e0a061..00000000000 --- a/tests/_files/3530.wsdl +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/_files/GoogleSearch.wsdl b/tests/_files/GoogleSearch.wsdl deleted file mode 100644 index e448501dd1b..00000000000 --- a/tests/_files/GoogleSearch.wsdl +++ /dev/null @@ -1,198 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/end-to-end/mock-objects/generator/3530.phpt b/tests/end-to-end/mock-objects/generator/3530.phpt deleted file mode 100644 index 686a0f7eb85..00000000000 --- a/tests/end-to-end/mock-objects/generator/3530.phpt +++ /dev/null @@ -1,29 +0,0 @@ ---TEST-- -\PHPUnit\Framework\MockObject\Generator\Generator::generateClassFromWsdl('3530.wsdl', 'Test') ---SKIPIF-- -generateClassFromWsdl( - __DIR__ . '/../../../_files/3530.wsdl', - 'Test' -); ---EXPECTF-- -declare(strict_types=1); - -class Test extends \SoapClient -{ - public function __construct($wsdl, array $options) - { - parent::__construct('%s/3530.wsdl', $options); - } - - public function Contact_Information($Contact_Id) - { - } -} diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class.phpt deleted file mode 100644 index e199b70debd..00000000000 --- a/tests/end-to-end/mock-objects/generator/wsdl_class.phpt +++ /dev/null @@ -1,37 +0,0 @@ ---TEST-- -\PHPUnit\Framework\MockObject\Generator\Generator::generateClassFromWsdl('GoogleSearch.wsdl', 'GoogleSearch') ---SKIPIF-- -generateClassFromWsdl( - __DIR__ . '/../../../_files/GoogleSearch.wsdl', - 'GoogleSearch' -); ---EXPECTF-- -declare(strict_types=1); - -class GoogleSearch extends \SoapClient -{ - public function __construct($wsdl, array $options) - { - parent::__construct('%s/GoogleSearch.wsdl', $options); - } - - public function doGoogleSearch($key, $q, $start, $maxResults, $filter, $restrict, $safeSearch, $lr, $ie, $oe) - { - } - - public function doGetCachedPage($key, $url) - { - } - - public function doSpellingSuggestion($key, $phrase) - { - } -} diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt deleted file mode 100644 index 1b9ab946949..00000000000 --- a/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt +++ /dev/null @@ -1,39 +0,0 @@ ---TEST-- -\PHPUnit\Framework\MockObject\Generator\Generator::generateClassFromWsdl('GoogleSearch.wsdl', 'GoogleSearch') ---SKIPIF-- -generateClassFromWsdl( - __DIR__ . '/../../../_files/GoogleSearch.wsdl', - 'My\\Space\\GoogleSearch' -); ---EXPECTF-- -declare(strict_types=1); - -namespace My\Space; - -class GoogleSearch extends \SoapClient -{ - public function __construct($wsdl, array $options) - { - parent::__construct('%s/GoogleSearch.wsdl', $options); - } - - public function doGoogleSearch($key, $q, $start, $maxResults, $filter, $restrict, $safeSearch, $lr, $ie, $oe) - { - } - - public function doGetCachedPage($key, $url) - { - } - - public function doSpellingSuggestion($key, $phrase) - { - } -} diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt deleted file mode 100644 index fbb3996f8f5..00000000000 --- a/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt +++ /dev/null @@ -1,30 +0,0 @@ ---TEST-- -\PHPUnit\Framework\MockObject\Generator\Generator::generateClassFromWsdl('GoogleSearch.wsdl', 'GoogleSearch', ['doGoogleSearch']) ---SKIPIF-- -generateClassFromWsdl( - __DIR__ . '/../../../_files/GoogleSearch.wsdl', - 'GoogleSearch', - ['doGoogleSearch'] -); ---EXPECTF-- -declare(strict_types=1); - -class GoogleSearch extends \SoapClient -{ - public function __construct($wsdl, array $options) - { - parent::__construct('%s/GoogleSearch.wsdl', $options); - } - - public function doGoogleSearch($key, $q, $start, $maxResults, $filter, $restrict, $safeSearch, $lr, $ie, $oe) - { - } -} diff --git a/tests/unit/Framework/MockObject/Creation/GetMockFromWsdlTest.php b/tests/unit/Framework/MockObject/Creation/GetMockFromWsdlTest.php deleted file mode 100644 index 6f517217481..00000000000 --- a/tests/unit/Framework/MockObject/Creation/GetMockFromWsdlTest.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations; -use PHPUnit\Framework\Attributes\Medium; -use PHPUnit\Framework\Attributes\RequiresPhpExtension; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -#[Group('test-doubles')] -#[Group('test-doubles/creation')] -#[Group('test-doubles/mock-object')] -#[Medium] -#[RequiresPhpExtension('soap')] -#[TestDox('getMockFromWsdl()')] -#[IgnorePhpunitDeprecations] -final class GetMockFromWsdlTest extends TestCase -{ - #[TestDox('Creates mock object from WSDL file')] - public function test_CreatesMockObjectFromWsdlFileWithNonNamespacedClassName(): void - { - $mock = $this->getMockFromWsdl(TEST_FILES_PATH . 'GoogleSearch.wsdl'); - - $this->assertStringStartsWith( - 'MockObject_GoogleSearch_', - $mock::class, - ); - } -}