Skip to content

Commit

Permalink
feat(PhpUnit) - add php 8.0 compatibility
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 Jan 22, 2021
1 parent 1d6b433 commit 961b6bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Generator/Helper/CodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ public function getNamespacesFromSource(string $sourceName): array
*/
private function parseUseStatements(ReflectionClass $reflection): array
{
if (!defined('T_NAME_QUALIFIED')) {
define('T_NAME_QUALIFIED', 314);
}
$filename = $reflection->getFileName();

if (false === $filename) {
Expand Down Expand Up @@ -385,6 +388,7 @@ private function parseUseStatements(ReflectionClass $reflection): array
}
switch ($token[0]) {
case T_STRING:
case T_NAME_QUALIFIED:
case T_NS_SEPARATOR:
$builtNamespace .= $token[1];

Expand Down Expand Up @@ -427,6 +431,7 @@ private function parseUseStatements(ReflectionClass $reflection): array
if ($record) {
switch ($token[0]) {
case T_STRING:
case T_NAME_QUALIFIED:
case T_NS_SEPARATOR:
$currentUse[$record] .= $token[1];

Expand Down
4 changes: 2 additions & 2 deletions src/Generator/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ protected function processMethodDocComment(ReflectionClass $reflectionClass, Ref
);
}

if (!$excludeConstructor && $param->getClass()) {
$className = $param->getClass() ?: $annotationParams[$i];
if (!$excludeConstructor && $param->getType()) {
$className = (string) $param->getType() ?: $annotationParams[$i];

if (null === $className) {
throw new InvalidClassnameException(sprintf('Class name could not be null.'));
Expand Down
10 changes: 6 additions & 4 deletions src/Service/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ private function getTestPathFromSource(string $sourcePath, string $namespace): a
$dataProviderTestPath = $testPath . DIRECTORY_SEPARATOR . self::DEFAULT_DATA_PROVIDER_NAMESPACE;
$mockTestPath = $testPath . DIRECTORY_SEPARATOR . self::DEFAULT_MOCK_NAMESPACE;
$namespace = explode(DIRECTORY_SEPARATOR, $namespace);
$baseNamespace = implode('\\', array_slice($namespace, $testFolderPosition));
$basePath = implode(DIRECTORY_SEPARATOR, array_slice($namespace, $testFolderPosition));
$projectNamespace = implode('\\', array_slice($namespace, 0, $testFolderPosition));
$projectNamespace = $namespace;
$baseNamespace = array_splice($projectNamespace, -$testFolderPosition+1);
$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, 0, [self::DEFAULT_TEST_NAMESPACE, $this->unitTestFolder]);
array_splice($namespace, -$testFolderPosition+1, 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 961b6bb

Please sign in to comment.