|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the phpstan-magento package. |
| 5 | + * |
| 6 | + * (c) bitExpert AG |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace bitExpert\PHPStan\Magento\Type; |
| 14 | + |
| 15 | +use PhpParser\Node\Arg; |
| 16 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 17 | +use PhpParser\Node\Expr\MethodCall; |
| 18 | +use PhpParser\Node\Identifier; |
| 19 | +use PHPStan\Analyser\Scope; |
| 20 | +use PHPStan\Reflection\MethodReflection; |
| 21 | +use PHPStan\Testing\PHPStanTestCase; |
| 22 | +use PHPStan\Type\ObjectType; |
| 23 | + |
| 24 | +class ControllerResultFactoryReturnTypeExtensionUnitTest extends PHPStanTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ControllerResultFactoryReturnTypeExtension |
| 28 | + */ |
| 29 | + private $extension; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->extension = new ControllerResultFactoryReturnTypeExtension(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return mixed[] |
| 38 | + */ |
| 39 | + public function returnTypeDataProvider(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + ['TYPE_JSON', 'Magento\Framework\Controller\Result\Json'], |
| 43 | + ['TYPE_PAGE', 'Magento\Framework\View\Result\Page'] |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return mixed[] |
| 49 | + */ |
| 50 | + public function isMethodSupportedDataProvider(): array |
| 51 | + { |
| 52 | + return [ |
| 53 | + ['create', true], |
| 54 | + ['get', false] |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @test |
| 60 | + * @dataProvider isMethodSupportedDataProvider |
| 61 | + * @param string $method |
| 62 | + * @param bool $expectedResult |
| 63 | + */ |
| 64 | + public function checkIfMethodIsSupported(string $method, bool $expectedResult): void |
| 65 | + { |
| 66 | + $methodReflection = $this->createMock(MethodReflection::class); |
| 67 | + $methodReflection->method('getName')->willReturn($method); |
| 68 | + |
| 69 | + self::assertSame($expectedResult, $this->extension->isMethodSupported($methodReflection)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @test |
| 74 | + * @dataProvider returnTypeDataProvider |
| 75 | + * @param string $param |
| 76 | + * @param string $expectedResult |
| 77 | + */ |
| 78 | + public function returnValidResultType(string $param, string $expectedResult): void |
| 79 | + { |
| 80 | + $methodReflection = $this->createMock(MethodReflection::class); |
| 81 | + $scope = $this->createMock(Scope::class); |
| 82 | + |
| 83 | + $identifier = $this->createConfiguredMock(Identifier::class, ['toString' => $param]); |
| 84 | + |
| 85 | + $expr = $this->createMock(ClassConstFetch::class); |
| 86 | + $expr->name = $identifier; |
| 87 | + |
| 88 | + $arg = $this->createMock(Arg::class); |
| 89 | + $arg->value = $expr; |
| 90 | + |
| 91 | + $methodCall = $this->createConfiguredMock(MethodCall::class, ['getArgs' => [$arg]]); |
| 92 | + |
| 93 | + $resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope); |
| 94 | + |
| 95 | + self::assertNotNull($resultType); |
| 96 | + self::assertSame($expectedResult, $resultType->getClassName()); |
| 97 | + } |
| 98 | +} |
0 commit comments