[wip] add tests

This commit is contained in:
Joop Schilder 2021-04-08 15:37:18 +02:00
parent afff03166e
commit 78b596a342
9 changed files with 2255 additions and 22 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/vendor/
/.idea/
/.phpunit.cache/

View File

@ -2,7 +2,14 @@
"name": "joopschilder/pdf-finder",
"type": "project",
"license": "MIT",
"keywords": ["pdf", "documents", "search", "metadata", "info", "portable document format"],
"keywords": [
"pdf",
"documents",
"search",
"metadata",
"info",
"portable document format"
],
"description": "Utility to locate PDF files based on their metadata",
"autoload": {
"psr-0": {
@ -11,9 +18,20 @@
]
}
},
"autoload-dev": {
"psr-0": {
"": [
"src",
"tests"
]
}
},
"require": {
"symfony/console": "^5.2",
"cocur/slugify": "^4.0",
"illuminate/collections": "^8.33"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}

2055
composer.lock generated

File diff suppressed because it is too large Load Diff

25
phpunit.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>

View File

@ -0,0 +1,29 @@
<?php
use IO\Filesystem\File;
use PDF\Document;
use PDF\Metadata;
trait ExampleDocumentAccess
{
protected function exampleDocument(?Metadata $metadata = null): Document
{
return new Document(
$this->exampleFile(),
$metadata ?? Metadata::empty()
);
}
protected function exampleDocumentWithMetadata(array $data): Document
{
return new Document(
$this->exampleFile(),
Metadata::fill($data)
);
}
protected function exampleFile(): File
{
return File::fromString(__DIR__ . '/resources/example.pdf');
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace Filter;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* @covers \Filter\FilterParser
*/
class FilterParserTest extends TestCase
{
private FilterParser $parser;
protected function setUp(): void
{
$this->parser = new FilterParser();
}
public function testCreatesSpecificFilters(): void
{
self::assertContainsOnlyInstancesOf(SpecificFilter::class, [
$this->parser->parse('title=poo'),
$this->parser->parse('1='),
$this->parser->parse('filepath=some_thing_else'),
]);
self::assertNotInstanceOf(SpecificFilter::class, $this->parser->parse('='));
}
public function testCreatesGenericFilters(): void
{
// Basically, anything that does not match the pattern .+=.*
self::assertContainsOnlyInstancesOf(GenericFilter::class, [
$this->parser->parse('scoobypoo'),
$this->parser->parse('324234'),
$this->parser->parse(324234),
$this->parser->parse('baz'),
]);
}
public function testDoesNotTakeNull(): void
{
$this->expectException(TypeError::class);
$this->parser->parse(null);
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace Filter;
use ExampleDocumentAccess;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* @covers \Filter\GenericFilter
*/
class GenericFilterTest extends TestCase
{
use ExampleDocumentAccess;
public function testAllowsWhenMatchBasenameOfFile(): void
{
$filter = new GenericFilter('example');
$document = $this->exampleDocument();
self::assertTrue($filter->allows($document));
}
public function testAllowsWhenMatchesPathToFile(): void
{
$filter = new GenericFilter('resources');
$document = $this->exampleDocument();
self::assertTrue($filter->allows($document));
}
public function testAllowsWhenMatchesAnyMetadataField(): void
{
$filter = new GenericFilter('John Snow');
$document = $this->exampleDocumentWithMetadata([
'title' => 'some title',
'author' => 'John Snow',
]);
self::assertTrue($filter->allows($document));
}
public function testDisallowsWhenNotMatchingBasenameOfFile(): void
{
$filter = new GenericFilter('not-an-example');
$document = $this->exampleDocument();
self::assertFalse($filter->allows($document));
}
public function testDisallowsWhenNotMatchingPathToFile(): void
{
$filter = new GenericFilter('this-is-definitely-not-in-the-path' . md5(time()));
$document = $this->exampleDocument();
self::assertFalse($filter->allows($document));
}
public function testDisallowsWhenNotMatchingAnyMetadataField(): void
{
$filter = new GenericFilter('peepee');
$document = $this->exampleDocumentWithMetadata([
'title' => 'some title',
'author' => 'John Snow',
]);
self::assertFalse($filter->allows($document));
}
public function testStringRepresentation(): void
{
$filter = new GenericFilter('i-am-a-field');
self::assertEquals(
'[*] contains \'i-am-a-field\'',
(string)$filter
);
}
public function testDoesNotAcceptNull(): void
{
$this->expectException(TypeError::class);
new GenericFilter(null);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Filter;
use PHPUnit\Framework\TestCase;
/**
* @covers \Filter\GenericFilter
*/
class SpecificFilterTest extends TestCase
{
protected function setUp(): void
{
self::markTestIncomplete('Not implemented');
}
}

View File