Skip to content

Commit

Permalink
Continued improving tests and testability.
Browse files Browse the repository at this point in the history
  • Loading branch information
navarr committed Jul 11, 2021
1 parent 8077aa3 commit 6bb32bd
Show file tree
Hide file tree
Showing 19 changed files with 411 additions and 22 deletions.
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">src/polyfills</directory>
<directory suffix=".php">src/Factory</directory>
<file>src/Proxy/MimeDeterminer.php</file>
</exclude>
</coverage>
</phpunit>
22 changes: 13 additions & 9 deletions src/Command/WhyBlockCommand/CsvOutputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
namespace Navarr\Depends\Command\WhyBlockCommand;

use Navarr\Depends\Data\DeclaredDependency;
use Navarr\Depends\Proxy\StdOutWriter;
use RuntimeException;

class CsvOutputHandler implements OutputHandlerInterface
{
/** @var bool */
private $includeHeader;

public function __construct(bool $includeHeader = true)
/** @var StdOutWriter */
private $writer;

public function __construct(StdOutWriter $writer, bool $includeHeader = true)
{
$this->includeHeader = $includeHeader;
$this->writer = $writer;
}

/**
Expand All @@ -28,18 +34,16 @@ public function __construct(bool $includeHeader = true)
*/
public function output(array $dependencies, string $packageToSearchFor, string $versionToCompareTo): int
{
$resource = STDIN;
if ($this->includeHeader) {
fputcsv($resource, ['File', 'Line #', 'Constraint Specified', 'Reasoning']);
$this->writer->writeCsv(['File', 'Line #', 'Constraint Specified', 'Reasoning']);
}
foreach ($dependencies as $dependency) {
fputcsv(
$resource,
$this->writer->writeCsv(
[
$dependency->getFile(),
$dependency->getLine(),
$dependency->getConstraint(),
$dependency->getReason(),
$dependency->getFile() ?: '',
$dependency->getLine() ?: '',
$dependency->getConstraint() ?: '',
$dependency->getReason() ?: '',
]
);
}
Expand Down
1 change: 0 additions & 1 deletion src/Parser/AstParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Navarr\Depends\IssueHandler\IssueHandlerInterface;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;

Expand Down
29 changes: 29 additions & 0 deletions src/Proxy/MimeDeterminer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @copyright 2021 Navarr Barnier. All Rights Reserved.
*/

declare(strict_types=1);

namespace Navarr\Depends\Proxy;

use JetBrains\PhpStorm\Pure;
use Navarr\Attribute\Dependency;

class MimeDeterminer
{
/**
* @return false|string
*/
#[Pure]
#[Dependency('ext-fileinfo', required: false, reason: 'Usage of mime_content_type if available')]
public function getMimeType(string $path)
{
if (function_exists('get_mime_type')) {
return mime_content_type($path);
} else {
return false;
}
}
}
45 changes: 45 additions & 0 deletions src/Proxy/StdOutWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright 2021 Navarr Barnier. All Rights Reserved.
*/

declare(strict_types=1);

namespace Navarr\Depends\Proxy;

class StdOutWriter
{
/** @var false|resource */
private $resource;

/**
* @param false|resource $resource
*/
public function __construct($resource = STDIN)
{
$this->resource = $resource;
}

public function canWrite(): bool
{
return $this->resource !== false;
}

public function write(string $data): void
{
if ($this->resource) {
fputs($this->resource, $data);
}
}

/**
* @param string[] $data
*/
public function writeCsv(array $data): void
{
if ($this->resource) {
fputcsv($this->resource, $data);
}
}
}
14 changes: 11 additions & 3 deletions src/ScopeDeterminer/PhpFileDeterminer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Navarr\Depends\ScopeDeterminer;

use JetBrains\PhpStorm\Pure;
use Navarr\Attribute\Dependency;
use Navarr\Depends\Proxy\MimeDeterminer;

class PhpFileDeterminer
{
Expand All @@ -30,14 +30,22 @@ class PhpFileDeterminer
'php8',
];

/** @var MimeDeterminer */
private $mimeDeterminer;

public function __construct(MimeDeterminer $mimeDeterminer)
{
$this->mimeDeterminer = $mimeDeterminer;
}

#[Pure]
#[Dependency('ext-fileinfo', required: false, reason: 'Usage of mime_content_type if available')]
public function isPhp(
string $file
): bool {
// There are so many approaches we could take here, but we're going with this one:

if (function_exists('mime_content_type') && in_array(mime_content_type($file), static::PHP_MIME_TYPES)) {
$mimeType = $this->mimeDeterminer->getMimeType($file);
if ($mimeType && in_array($mimeType, static::PHP_MIME_TYPES)) {
// Mime type is PHP. That's good enough for me
return true;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Controller/Composer/ComposerPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @copyright 2021 Navarr Barnier. All Rights Reserved.
*/

namespace Navarr\Depends\Test\Controller\Composer;

use Composer\Plugin\Capability\CommandProvider;
use Navarr\Depends\Controller\Composer\ComposerCommand;
use Navarr\Depends\Controller\Composer\ComposerPlugin;
use PHPUnit\Framework\TestCase;

class ComposerPluginTest extends TestCase
{

public function testCommandProviderIsPartOfPlugin()
{
$plugin = new ComposerPlugin();
$capabilities = $plugin->getCapabilities();
$this->assertArrayHasKey(CommandProvider::class, $capabilities);
$this->assertEquals(ComposerPlugin::class, $capabilities[CommandProvider::class]);
}

public function testContainsOnlyComposerCommands()
{
$plugin = new ComposerPlugin();
$commands = $plugin->getCommands();

$this->assertIsArray($commands);
$this->assertCount(1, $commands);
$command = end($commands);
$this->assertInstanceOf(ComposerCommand::class, $command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class DeclaredDependencyTest extends TestCase
{
public function testDataTransferObject()
public function testNonDefaultValues()
{
$file = uniqid();
$line = uniqid();
Expand All @@ -27,7 +27,8 @@ public function testDataTransferObject()
$reference,
$package,
$version,
$reason
$reason,
false
);

$this->assertEquals($file, $dependency->getFile());
Expand All @@ -36,9 +37,10 @@ public function testDataTransferObject()
$this->assertEquals($package, $dependency->getPackage());
$this->assertEquals($version, $dependency->getConstraint());
$this->assertEquals($reason, $dependency->getReason());
$this->assertFalse($dependency->isRequired());
}

public function testDataTransferObjectReturnsNull()
public function testDefaultValues()
{
$dependency = new DeclaredDependency();

Expand All @@ -48,5 +50,15 @@ public function testDataTransferObjectReturnsNull()
$this->assertNull($dependency->getPackage());
$this->assertNull($dependency->getConstraint());
$this->assertNull($dependency->getReason());
$this->assertTrue($dependency->isRequired());
}

public function testRequiredAttributeReturnsProvidedValue()
{
$dependency = new DeclaredDependency(required: true);
$this->assertTrue($dependency->isRequired());

$dependency = new DeclaredDependency(required: false);
$this->assertFalse($dependency->isRequired());
}
}
61 changes: 61 additions & 0 deletions tests/Data/ReferenceAdderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @copyright 2021 Navarr Barnier. All Rights Reserved.
*/

declare(strict_types=1);

namespace Navarr\Depends\Test\Data;

use Navarr\Depends\Data\DeclaredDependency;
use Navarr\Depends\Data\ReferenceAdder;
use PHPUnit\Framework\TestCase;

class ReferenceAdderTest extends TestCase
{
public function testAdd()
{
$file = uniqid();
$line = uniqid();
$package = uniqid();
$constraint = uniqid();
$reason = uniqid();

$dependency = new DeclaredDependency(
line: $line,
package: $package,
constraint: $constraint,
reason: $reason
);

$this->assertEquals($package, $dependency->getPackage());
$this->assertEquals($constraint, $dependency->getConstraint());
$this->assertEquals($reason, $dependency->getReason());
$this->assertEquals($line, $dependency->getLine());
$this->assertNull($dependency->getFile());
$this->assertNull($dependency->getReference());

$referenceAdder = new ReferenceAdder();
$dependency2 = $referenceAdder->add(
$dependency,
$file
);

// Ensure original hasn't changed
$this->assertEquals($package, $dependency->getPackage());
$this->assertEquals($constraint, $dependency->getConstraint());
$this->assertEquals($reason, $dependency->getReason());
$this->assertEquals($line, $dependency->getLine());
$this->assertNull($dependency->getFile());
$this->assertNull($dependency->getReference());

// Check new
$this->assertEquals($package, $dependency2->getPackage());
$this->assertEquals($constraint, $dependency2->getConstraint());
$this->assertEquals($reason, $dependency2->getReason());
$this->assertEquals($line, $dependency2->getLine());
$this->assertEquals($file, $dependency2->getFile());
$this->assertEquals("{$file}:{$line}", $dependency2->getReference());
}
}
8 changes: 4 additions & 4 deletions tests/Parser/AstParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

class AstParserTest extends TestCase
{
private const EMPTY_FILE = '../data/emptyFile.php';
private const FILE_ATTRIBUTE_USAGE = '../data/attributeUsage.php';
private const FILE_INVALID = '../data/invalidAttributeUsage.php';
private const EMPTY_FILE = '../_data/emptyFile.php';
private const FILE_ATTRIBUTE_USAGE = '../_data/attributeUsage.php';
private const FILE_INVALID = '../_data/invalidAttributeUsage.php';
private const ATTRIBUTE_USAGE_ATTRIBUTE_COUNT = 10;

private function buildAstParser(array $args = []): AstParser
Expand Down Expand Up @@ -144,7 +144,7 @@ public function testParserReturnsEmptyResultsOnInvalidFile()

public function testParserGracefullyHandlesBadAttributeSyntax()
{
$file = __DIR__ . '/' . '../data/incorrectAttributeUsage.php';
$file = __DIR__ . '/../_data/invalidAttributeUsage.php';
$contents = file_get_contents($file);

$parser = $this->buildAstParser();
Expand Down
4 changes: 2 additions & 2 deletions tests/Parser/LegacyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

class LegacyParserTest extends TestCase
{
private const LEGACY_FILE = '../data/legacyParserUsage.php';
private const EMPTY_FILE = '../data/emptyFile.php';
private const LEGACY_FILE = '../_data/legacyParserUsage.php';
private const EMPTY_FILE = '../_data/emptyFile.php';

private function getStandardResults(): array
{
Expand Down
Loading

0 comments on commit 6bb32bd

Please sign in to comment.