Skip to content

Commit

Permalink
Merge pull request #119 from shochdoerfer/feature/more_tests
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
shochdoerfer committed Jun 19, 2021
2 parents 26c1319 + 66b721b commit d0aa105
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,33 @@ public function returnMagicMethodReflectionForGetMethod(): void
self::assertCount(1, $params);
self::assertInstanceOf(MixedType::class, $params[0]->getType());
}

/**
* @test
* @dataProvider isMethodSupportedDataprovider
* @param string $method
* @param bool $expectedResult
*/
public function hasMethodDetectSessionManager(string $method, bool $expectedResult): void
{
$this->classReflection->expects(self::once())
->method('isSubclassOf')
->willReturn(true);

self::assertSame($expectedResult, $this->extension->hasMethod($this->classReflection, $method));
}

/**
* @return mixed[]
*/
public function isMethodSupportedDataprovider(): array
{
return [
['getTest', true],
['setTest', true],
['unsetTest', true],
['hasText', true],
['someOtherMethod', false],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the phpstan-magento package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace bitExpert\PHPStan\Magento\Reflection;

use PHPStan\Reflection\ClassReflection;
use PHPUnit\Framework\TestCase;

class MagicMethodReflectionUnitTest extends TestCase
{
/**
* @test
*/
public function magicMethodReflectionCreation(): void
{
$classReflection = $this->createMock(ClassReflection::class);
$methodName = 'myTestMethod';
$variants = [];

$reflection = new MagicMethodReflection($methodName, $classReflection, $variants);

self::assertSame($classReflection, $reflection->getDeclaringClass());
self::assertFalse($reflection->isStatic());
self::assertFalse($reflection->isPrivate());
self::assertTrue($reflection->isPublic());
self::assertSame($methodName, $reflection->getName());
self::assertSame($reflection, $reflection->getPrototype());
self::assertSame($variants, $reflection->getVariants());
self::assertNull($reflection->getDocComment());
self::assertSame(\PHPStan\TrinaryLogic::createNo(), $reflection->isDeprecated());
self::assertSame('', $reflection->getDeprecatedDescription());
self::assertSame(\PHPStan\TrinaryLogic::createNo(), $reflection->isFinal());
self::assertSame(\PHPStan\TrinaryLogic::createNo(), $reflection->isInternal());
self::assertNull($reflection->getThrowType());
}

/**
* @test
* @dataProvider sideeffectsDataprovider
* @param string $methodName
* @param \PHPStan\TrinaryLogic $expectedResult
*/
public function magicMethodReflectionCreationSideeffects(
string $methodName,
\PHPStan\TrinaryLogic $expectedResult
): void {
$classReflection = $this->createMock(ClassReflection::class);
$variants = [];

$reflection = new MagicMethodReflection($methodName, $classReflection, $variants);
self::assertSame($expectedResult, $reflection->hasSideEffects());
}

/**
* @return mixed[]
*/
public function sideeffectsDataprovider(): array
{
return [
['getTest', \PHPStan\TrinaryLogic::createNo()],
['setTest', \PHPStan\TrinaryLogic::createYes()],
['unsetTest', \PHPStan\TrinaryLogic::createYes()],
['hasText', \PHPStan\TrinaryLogic::createNo()],
['someOtherMethod', \PHPStan\TrinaryLogic::createNo()],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
namespace bitExpert\PHPStan\Magento\Rules;

use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;

/**
Expand All @@ -39,4 +43,56 @@ public function checkCaughtException(): void
]
]);
}

/**
* @test
*/
public function getNodeTypeMethodReturnsMethodCall(): void
{
$rule = new AbstractModelRetrieveCollectionViaFactoryRule();

self::assertSame(MethodCall::class, $rule->getNodeType());
}

/**
* @test
*/
public function processNodeThrowsExceptionForNonMethodCallNodes(): void
{
$this->expectException(ShouldNotHappenException::class);

$node = new Variable('var');
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelRetrieveCollectionViaFactoryRule();
$rule->processNode($node, $scope);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsWrongType(): void
{
$node = new MethodCall(new Variable('var'), new Variable('wrong_node'));
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelRetrieveCollectionViaFactoryRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsNotGetCollection(): void
{
$node = new MethodCall(new Variable('var'), 'wrong_node_name');
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelRetrieveCollectionViaFactoryRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
namespace bitExpert\PHPStan\Magento\Rules;

use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;

/**
Expand Down Expand Up @@ -46,4 +50,56 @@ public function checkCaughtExceptions(): void
],
]);
}

/**
* @test
*/
public function getNodeTypeMethodReturnsMethodCall(): void
{
$rule = new AbstractModelUseServiceContractRule();

self::assertSame(MethodCall::class, $rule->getNodeType());
}

/**
* @test
*/
public function processNodeThrowsExceptionForNonMethodCallNodes(): void
{
$this->expectException(ShouldNotHappenException::class);

$node = new Variable('var');
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelUseServiceContractRule();
$rule->processNode($node, $scope);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsWrongType(): void
{
$node = new MethodCall(new Variable('var'), new Variable('wrong_node'));
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelUseServiceContractRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsNotSaveOrLoadOrDelete(): void
{
$node = new MethodCall(new Variable('var'), 'wrong_node_name');
$scope = $this->createMock(Scope::class);

$rule = new AbstractModelUseServiceContractRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel;
use bitExpert\PHPStan\Magento\Type\TestFrameworkObjectManagerDynamicReturnTypeExtension;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;

/**
Expand Down Expand Up @@ -49,4 +53,55 @@ public function checkCaughtExceptions(): void
],
]);
}

/**
* @test
*/
public function getNodeTypeMethodReturnsMethodCall(): void
{
$rule = new GetCollectionMockMethodNeedsCollectionSubclassRule();

self::assertSame(MethodCall::class, $rule->getNodeType());
}
/**
* @test
*/
public function processNodeThrowsExceptionForNonMethodCallNodes(): void
{
$this->expectException(ShouldNotHappenException::class);

$node = new Variable('var');
$scope = $this->createMock(Scope::class);

$rule = new GetCollectionMockMethodNeedsCollectionSubclassRule();
$rule->processNode($node, $scope);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsWrongType(): void
{
$node = new MethodCall(new Variable('var'), new Variable('wrong_node'));
$scope = $this->createMock(Scope::class);

$rule = new GetCollectionMockMethodNeedsCollectionSubclassRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}

/**
* @test
*/
public function processNodeReturnsEarlyWhenNodeNameIsNotGetCollectionMock(): void
{
$node = new MethodCall(new Variable('var'), 'wrong_node_name');
$scope = $this->createMock(Scope::class);

$rule = new GetCollectionMockMethodNeedsCollectionSubclassRule();
$return = $rule->processNode($node, $scope);

self::assertCount(0, $return);
}
}

0 comments on commit d0aa105

Please sign in to comment.