Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed Jan 8, 2022
1 parent aac4a0e commit 4875435
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 25 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ parameters:
-
message: '~is not covered by backward compatibility promise.~'
path: tests/bitExpert/PHPStan/Magento/Autoload/ExtensionInterfaceAutoloaderUnitTest.php
-
message: '~Parameter #1 $argument of class ReflectionClass constructor expects class-string<UncachedExtensionInterface>|UncachedExtensionInterface, string given~'
path: tests/bitExpert/PHPStan/Magento/Autoload/ExtensionInterfaceAutoloaderUnitTest.php
-
message: '~bitExpert\\PHPStan\\Magento\\Rules\\Helper\\SampleModel::__construct\(\) does not call parent constructor~'
path: tests/bitExpert/PHPStan/Magento/Rules/Helper/SampleModel.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,22 @@ protected function locateConstructsInDirectory(string $directory): array

$constructs = [];
foreach ($iterator as $fileInfo) {
/** @var \SplFileInfo $fileInfo */
if (!$fileInfo->isFile()) {
continue;
}

if ($fileInfo->getBasename('.php') === $fileInfo->getBasename()) {
continue;
}
try {
/** @var \SplFileInfo $fileInfo */
if (!$fileInfo->isFile()) {
continue;
}

/** @var string $fileName */
$fileName = $fileInfo->getRealPath();
if ($fileInfo->getBasename('.php') === $fileInfo->getBasename()) {
continue;
}

/** @var string $source */
$source = \file_get_contents($fileName);
/** @var string $fileName */
$fileName = $fileInfo->getRealPath();

try {
$constructsFromFile = Constructs::fromSource($source);
foreach ($constructsFromFile as $construct) {
/** @var string $source */
$source = \file_get_contents($fileName);
foreach (Constructs::fromSource($source) as $construct) {
$constructs[$construct->name()] = 1;
}
} catch (\Throwable $exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use bitExpert\PHPStan\Magento\Autoload\DataProvider\ClassyDataProvider;
use bitExpert\PHPStan\Magento\Autoload\DataProvider\ExtensionAttributeDataProvider;
use Ergebnis\Classy\Construct;
use Laminas\Code\Generator\DocBlock\Tag\ParamTag;
use Laminas\Code\Generator\DocBlock\Tag\ReturnTag;
use Laminas\Code\Generator\DocBlockGenerator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Autoload\DataProvider;

use PHPUnit\Framework\TestCase;

class ClassyDataProviderUnitTest extends TestCase
{
/**
* @test
*/
public function returnsTrueForFoundClassyConstructsInCurrentDirectory(): void
{
$dataprovider = new ClassyDataProvider(__DIR__);

static::assertTrue($dataprovider->exists(ClassyDataProviderUnitTest::class));
static::assertTrue($dataprovider->exists(ExtensionAttributeDataProviderUnitTest::class));
}

/**
* @test
*/
public function returnsFalseWhenDirectoryNotExists(): void
{
$dataprovider = new ClassyDataProvider(__DIR__ . '/not-exists');

static::assertFalse($dataprovider->exists(ClassyDataProviderUnitTest::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace bitExpert\PHPStan\Magento\Autoload;

use bitExpert\PHPStan\Magento\Autoload\Cache\FileCacheStorage;
use bitExpert\PHPStan\Magento\Autoload\DataProvider\ClassyDataProvider;
use bitExpert\PHPStan\Magento\Autoload\DataProvider\ExtensionAttributeDataProvider;
use org\bovigo\vfs\vfsStream;
use PHPStan\Cache\Cache;
use PHPStan\Cache\CacheStorage;
use PHPUnit\Framework\TestCase;

class ExtensionInterfaceAutoloaderUnitTest extends TestCase
{
/**
* @var CacheStorage|\PHPUnit\Framework\MockObject\MockObject
* @var Cache|\PHPUnit\Framework\MockObject\MockObject
*/
private $storage;
private $cache;
/**
* @var ExtensionAttributeDataProvider|\PHPUnit\Framework\MockObject\MockObject
*/
Expand All @@ -29,11 +30,11 @@ class ExtensionInterfaceAutoloaderUnitTest extends TestCase

protected function setUp(): void
{
$this->storage = $this->createMock(CacheStorage::class);
$this->cache = $this->createMock(Cache::class);
$this->extAttrDataProvider = $this->createMock(ExtensionAttributeDataProvider::class);
$this->classyDataProvider = $this->createMock(ClassyDataProvider::class);
$this->autoloader = new ExtensionInterfaceAutoloader(
new Cache($this->storage),
$this->cache,
$this->extAttrDataProvider,
$this->classyDataProvider
);
Expand All @@ -44,7 +45,7 @@ protected function setUp(): void
*/
public function autoloaderIgnoresClassesWithoutExtensionInterfacePostfix(): void
{
$this->storage->expects(self::never())
$this->cache->expects(self::never())
->method('load');

$this->autoloader->autoload('SomeClass');
Expand All @@ -55,12 +56,79 @@ public function autoloaderIgnoresClassesWithoutExtensionInterfacePostfix(): void
*/
public function autoloaderUsesCachedFileWhenFound(): void
{
$this->storage->expects(self::once())
$this->cache->expects(self::once())
->method('load')
->willReturn(__DIR__ . '/HelperExtensionInterface.php');

$this->cache->expects(self::never())
->method('save');

$this->autoloader->autoload(HelperExtensionInterface::class);

self::assertTrue(interface_exists(HelperExtensionInterface::class, false));
}

/**
* @test
*/
public function autoloadDoesNotGenerateInterfaceWhenNoAttributesExist(): void
{
$interfaceName = 'NonExistentExtensionInterface';

$this->cache->expects(self::once())
->method('load')
->willReturn(null);

$this->classyDataProvider->expects(self::once())
->method('exists')
->willReturn(false);

$this->autoloader->autoload($interfaceName);
static::assertFalse(interface_exists($interfaceName));
}

/**
* @test
*/
public function autoloadGeneratesInterfaceWhenNotCached(): void
{
$interfaceName = 'UncachedExtensionInterface';

$root = vfsStream::setup('test');
$cache = new Cache(new FileCacheStorage($root->url() . '/tmp/cache/PHPStan'));
$autoloader = new ExtensionInterfaceAutoloader($cache, $this->extAttrDataProvider, $this->classyDataProvider);

$this->classyDataProvider->expects(self::once())
->method('exists')
->willReturn(true);

$this->extAttrDataProvider->expects(self::once())
->method('getAttributesForInterface')
->willReturn(['attr' => 'string']);

$autoloader->autoload($interfaceName);
static::assertTrue(interface_exists($interfaceName));
$interfaceReflection = new \ReflectionClass($interfaceName);
try {
$getAttrReflection = $interfaceReflection->getMethod('getAttr');
$docComment = $getAttrReflection->getDocComment();
if (!is_string($docComment)) {
throw new \ReflectionException();
}
static::assertStringContainsString('@return string|null', $docComment);
} catch (\ReflectionException $e) {
static::fail('Could not find expected method getAttr on generated interface');
}

try {
$setAttrReflection = $interfaceReflection->getMethod('setAttr');
$docComment = $setAttrReflection->getDocComment();
if (!is_string($docComment)) {
throw new \ReflectionException();
}
static::assertStringContainsString('@param string $attr', $docComment);
} catch (\ReflectionException $e) {
static::fail('Could not find expected generated method setAttr on generated interface');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function provideAutoloaders(): array
[new TestFrameworkAutoloader()],
[new ExtensionInterfaceAutoloader(
$cache,
new ExtensionAttributeDataProvider('.'),
new ClassyDataProvider('.')
new ExtensionAttributeDataProvider(__DIR__),
new ClassyDataProvider(__DIR__)
)]
];
}
Expand Down

0 comments on commit 4875435

Please sign in to comment.