Skip to content

Commit

Permalink
feat(PhpUnit) - add php 8.0 compatibility part 2
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
temafey committed Mar 19, 2021
1 parent cf6fa68 commit ceb2730
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Generator/DataProviderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Generator/Helper/CodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand All @@ -375,6 +376,7 @@ private function parseUseStatements(ReflectionClass $reflection): array
foreach ($tokens as $token) {
if (T_NAMESPACE === $token[0]) {
$buildingNamespace = true;

if ($matchedNamespace) {
break;
}
Expand Down
24 changes: 16 additions & 8 deletions src/Generator/Mock/Mockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Service/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit ceb2730

Please sign in to comment.