From ceb27305abcf9f2e3b2e04254e62f220bbf86697 Mon Sep 17 00:00:00 2001 From: Artem Onyshchenko Date: Fri, 19 Mar 2021 13:13:45 +0200 Subject: [PATCH] feat(PhpUnit) - add php 8.0 compatibility part 2 - Update CodeHelper add support for T_NAME_QUALIFIED constant - Update TestGenerator, remove using deprecated method getClass() from ReflectionParameter - Update TestClass, fix logic in generating class namespaces --- src/Generator/DataProviderGenerator.php | 2 +- src/Generator/Helper/CodeHelper.php | 6 ++++-- src/Generator/Mock/Mockery.php | 24 ++++++++++++++++-------- src/Service/TestClass.php | 8 ++++---- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Generator/DataProviderGenerator.php b/src/Generator/DataProviderGenerator.php index 1e9d155..a1543c3 100644 --- a/src/Generator/DataProviderGenerator.php +++ b/src/Generator/DataProviderGenerator.php @@ -395,7 +395,7 @@ public function generate(): ?string } $defaultArgs = null; - if (isset($dataProvider['dataProviderMethods'][self::DATA_PROVIDER_DEFAULT_ARGS])) { + if (!empty($dataProvider['dataProviderMethods'][self::DATA_PROVIDER_DEFAULT_ARGS])) { $defaultArgs = $dataProvider['dataProviderMethods'][self::DATA_PROVIDER_DEFAULT_ARGS]; unset($dataProvider['dataProviderMethods'][self::DATA_PROVIDER_DEFAULT_ARGS]); } diff --git a/src/Generator/Helper/CodeHelper.php b/src/Generator/Helper/CodeHelper.php index f79e05e..64464e9 100644 --- a/src/Generator/Helper/CodeHelper.php +++ b/src/Generator/Helper/CodeHelper.php @@ -292,8 +292,9 @@ public function getNamespacesFromSource(string $sourceName): array continue; } - if (is_array($useToken) && T_STRING === $useToken[0]) { + if (is_array($useToken) && T_NAME_QUALIFIED === $useToken[0]) { $useToken[1] = trim($useToken[1]); + if (!$namespaceFinish) { $namespace[] = $useToken[1]; } else { @@ -349,7 +350,7 @@ public function getNamespacesFromSource(string $sourceName): array private function parseUseStatements(ReflectionClass $reflection): array { if (!defined('T_NAME_QUALIFIED')) { - define('T_NAME_QUALIFIED', 314); + define('T_NAME_QUALIFIED', 311); } $filename = $reflection->getFileName(); @@ -375,6 +376,7 @@ private function parseUseStatements(ReflectionClass $reflection): array foreach ($tokens as $token) { if (T_NAMESPACE === $token[0]) { $buildingNamespace = true; + if ($matchedNamespace) { break; } diff --git a/src/Generator/Mock/Mockery.php b/src/Generator/Mock/Mockery.php index 6a736c2..f1bde5f 100644 --- a/src/Generator/Mock/Mockery.php +++ b/src/Generator/Mock/Mockery.php @@ -177,12 +177,16 @@ public function generateMockCode( } if (class_exists($returnType) || interface_exists($returnType)) { - $filename = $reflection->getFileName(); + $refNamespaces = []; - if (false === $filename) { - throw new FileNotExistsException(sprintf('File \'%s\' does not exists.', $reflection->getName())); + if (!$reflection->isInternal()) { + $filename = $reflection->getFileName(); + + if (false === $filename) { + throw new FileNotExistsException(sprintf('File \'%s\' does not exist.', $reflection->getName())); + } + $refNamespaces = $this->getNamespacesFromSource($filename); } - $refNamespaces = $this->getNamespacesFromSource($filename); $namespacesFromParentClassesAndTraits = $this->getNamespacesFromParentClassesAndTraits($reflection); $namespaces = array_merge($parentNamespaces, $refNamespaces, $namespacesFromParentClassesAndTraits); $refMockName = $mockGenerator->addMock($returnType, $reflection, $refMethod, $namespaces, $level, $parentClass); @@ -230,12 +234,16 @@ public function generateMockCode( break; } - $filename = $reflection->getFileName(); + $refNamespaces = []; - if (false === $filename) { - throw new FileNotExistsException(sprintf('File \'%s\' does not exists.', $reflection->getName())); + if (!$reflection->isInternal()) { + $filename = $reflection->getFileName(); + + if (false === $filename) { + throw new FileNotExistsException(sprintf('File \'%s\' does not exist.', $reflection->getName())); + } + $refNamespaces = $this->getNamespacesFromSource($filename); } - $refNamespaces = $this->getNamespacesFromSource($filename); $namespacesFromParentClassesAndTraits = $this->getNamespacesFromParentClassesAndTraits($reflection); $namespaces = array_merge($parentNamespaces, $refNamespaces, $namespacesFromParentClassesAndTraits); $refMockName = $mockGenerator->addMock($returnType, $reflection, $refMethod, $namespaces, $level, $parentClass); diff --git a/src/Service/TestClass.php b/src/Service/TestClass.php index 6ef37c4..a471730 100644 --- a/src/Service/TestClass.php +++ b/src/Service/TestClass.php @@ -251,11 +251,11 @@ private function getTestPathFromSource(string $sourcePath, string $namespace): a $treePath = array_reverse(explode(DIRECTORY_SEPARATOR, $sourcePath)); $treeNamespace = array_reverse(explode(DIRECTORY_SEPARATOR, $namespace)); $sourcePath = []; - $testFolderPosition = count($treePath) + 1; + $testFolderPosition = 0; foreach ($treePath as $i => $part) { if ($part === $treeNamespace[$i]) { - --$testFolderPosition; + ++$testFolderPosition; continue; } @@ -275,14 +275,14 @@ private function getTestPathFromSource(string $sourcePath, string $namespace): a $mockTestPath = $testPath . DIRECTORY_SEPARATOR . self::DEFAULT_MOCK_NAMESPACE; $namespace = explode(DIRECTORY_SEPARATOR, $namespace); $projectNamespace = $namespace; - $baseNamespace = array_splice($projectNamespace, -$testFolderPosition+1); + $baseNamespace = array_splice($projectNamespace, -$testFolderPosition); $basePath = implode(DIRECTORY_SEPARATOR, $baseNamespace); $baseNamespace = implode('\\', $baseNamespace); $projectNamespace = implode('\\', $projectNamespace); $dataProviderNamespace = $projectNamespace . '\\' . self::DEFAULT_TEST_NAMESPACE . '\\' . $this->unitTestFolder . '\\' . self::DEFAULT_DATA_PROVIDER_NAMESPACE; $mockNamespace = $projectNamespace . '\\' . self::DEFAULT_TEST_NAMESPACE . '\\' . $this->unitTestFolder . '\\' . self::DEFAULT_MOCK_NAMESPACE; $baseTestNamespace = $projectNamespace . '\\' . self::DEFAULT_TEST_NAMESPACE . '\\' . $this->unitTestFolder; - array_splice($namespace, -$testFolderPosition+1, 0, [self::DEFAULT_TEST_NAMESPACE, $this->unitTestFolder]); + array_splice($namespace, -$testFolderPosition, 0, [self::DEFAULT_TEST_NAMESPACE, $this->unitTestFolder]); $testNamespace = implode('\\', $namespace); $localTestPath .= self::DEFAULT_TEST_NAMESPACE . DIRECTORY_SEPARATOR . $this->unitTestFolder; $testPath .= DIRECTORY_SEPARATOR . $basePath;