|
| 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\Rules; |
| 14 | + |
| 15 | +use bitExpert\PHPStan\Magento\Rules\Helper\SampleBlock; |
| 16 | +use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel; |
| 17 | +use PhpParser\Node\Expr\MethodCall; |
| 18 | +use PhpParser\Node\Expr\Variable; |
| 19 | +use PHPStan\Analyser\Scope; |
| 20 | +use PHPStan\Rules\Rule; |
| 21 | +use PHPStan\ShouldNotHappenException; |
| 22 | +use PHPStan\Testing\RuleTestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * @extends \PHPStan\Testing\RuleTestCase<SetTemplateDisallowedForBlockRule> |
| 26 | + */ |
| 27 | +class SetTemplateDisallowedForBlockRuleUnitTest extends RuleTestCase |
| 28 | +{ |
| 29 | + protected function getRule(): Rule |
| 30 | + { |
| 31 | + return new SetTemplateDisallowedForBlockRule(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @test |
| 36 | + */ |
| 37 | + public function checkCaughtExceptions(): void |
| 38 | + { |
| 39 | + $this->analyse([__DIR__ . '/Helper/block_settemplate.php'], [ |
| 40 | + [ |
| 41 | + 'Setter methods like '.SampleBlock::class.'::setTemplate() are deprecated in Block classes, '. |
| 42 | + 'use constructor arguments instead', |
| 43 | + 4, |
| 44 | + ], |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @test |
| 50 | + */ |
| 51 | + public function getNodeTypeMethodReturnsMethodCall(): void |
| 52 | + { |
| 53 | + $rule = new SetTemplateDisallowedForBlockRule(); |
| 54 | + |
| 55 | + self::assertSame(MethodCall::class, $rule->getNodeType()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @test |
| 60 | + */ |
| 61 | + public function processNodeThrowsExceptionForNonMethodCallNodes(): void |
| 62 | + { |
| 63 | + $this->expectException(ShouldNotHappenException::class); |
| 64 | + |
| 65 | + $node = new Variable('var'); |
| 66 | + $scope = $this->createMock(Scope::class); |
| 67 | + |
| 68 | + $rule = new SetTemplateDisallowedForBlockRule(); |
| 69 | + $rule->processNode($node, $scope); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @test |
| 74 | + */ |
| 75 | + public function processNodeReturnsEarlyWhenNodeNameIsWrongType(): void |
| 76 | + { |
| 77 | + $node = new MethodCall(new Variable('var'), new Variable('wrong_node')); |
| 78 | + $scope = $this->createMock(Scope::class); |
| 79 | + |
| 80 | + $rule = new SetTemplateDisallowedForBlockRule(); |
| 81 | + $return = $rule->processNode($node, $scope); |
| 82 | + |
| 83 | + self::assertCount(0, $return); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @test |
| 88 | + */ |
| 89 | + public function processNodeReturnsEarlyWhenNodeNameIsNotSaveOrLoadOrDelete(): void |
| 90 | + { |
| 91 | + $node = new MethodCall(new Variable('var'), 'wrong_node_name'); |
| 92 | + $scope = $this->createMock(Scope::class); |
| 93 | + |
| 94 | + $rule = new SetTemplateDisallowedForBlockRule(); |
| 95 | + $return = $rule->processNode($node, $scope); |
| 96 | + |
| 97 | + self::assertCount(0, $return); |
| 98 | + } |
| 99 | +} |
0 commit comments