Skip to content

Commit

Permalink
add tests to phan paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
d0x2f committed Oct 20, 2018
1 parent e6d475f commit 6ca6426
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 39 deletions.
1 change: 1 addition & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'minimum_severity' => Issue::SEVERITY_LOW,
'directory_list' => [
'src/',
'spec/',
'vendor/kahlan/kahlan/src',
'vendor/vanilla/garden-cli/src',
'vendor/php-ds/php-ds/src'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"test": "./vendor/bin/kahlan",
"phan": "./vendor/bin/phan",
"phan": "env PHAN_DISABLE_XDEBUG_WARN=1 ./vendor/bin/phan",
"sniff": "./vendor/bin/phpcs",
"post-install-cmd": "\\SebastianFeldmann\\CaptainHook\\Composer\\Cmd::install"
},
Expand Down
14 changes: 9 additions & 5 deletions spec/Accumulator.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use d0x2f\CloverMerge\Utilities;
use d0x2f\CloverMerge\Metrics;

/**
* @phan-closure-scope \Kahlan\Scope
*/
describe('Accumulator', function () {
describe('parseAll', function () {
describe('Receives a vector of nice XML documents and merges in "inclusive" mode.', function () {
context('Receives a vector of nice XML documents and merges in "inclusive" mode.', function () {
beforeEach(function () {
$this->accumulator = new Accumulator('inclusive');
$this->accumulator->parseAll(new \Ds\Vector([
Expand Down Expand Up @@ -46,7 +49,7 @@
expect($lines->get(5)->getCount())->toBe(8);
});
});
describe('Receives a vector of nice XML documents and merges in "exclusive" mode.', function () {
context('Receives a vector of nice XML documents and merges in "exclusive" mode.', function () {
beforeEach(function () {
$this->accumulator = new Accumulator('exclusive');
$this->accumulator->parseAll(new \Ds\Vector([
Expand Down Expand Up @@ -77,7 +80,7 @@
expect($lines->get(4)->getCount())->toBe(6);
});
});
describe('Receives a vector of nice XML documents and merges in "additive" mode.', function () {
context('Receives a vector of nice XML documents and merges in "additive" mode.', function () {
beforeEach(function () {
$this->accumulator = new Accumulator('additive');
$this->accumulator->parseAll(new \Ds\Vector([
Expand Down Expand Up @@ -111,7 +114,7 @@
expect($lines->get(5)->getCount())->toBe(4);
});
});
describe('Receives a vector of XML documents with junk included.', function () {
context('Receives a vector of XML documents with junk included.', function () {
beforeEach(function () {
allow(Utilities::class)->toReceive('::logWarning')->andReturn();
$this->accumulator = new Accumulator('inclusive');
Expand All @@ -121,6 +124,7 @@
]));
});

/** @phan-suppress PhanUndeclaredProperty */
it('ignores the junk and returns a map of files parsed from the input.', function () {
$result = $this->accumulator->getFiles();

Expand All @@ -147,7 +151,7 @@
expect($lines->get(5)->getCount())->toBe(4);
});
});
describe('Receives a vector of XML documents with errors.', function () {
context('Receives a vector of XML documents with errors.', function () {
beforeEach(function () {
allow(Utilities::class)->toReceive('::logWarning')->andReturn();
$this->accumulator = new Accumulator('inclusive');
Expand Down
8 changes: 6 additions & 2 deletions spec/ClassT.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use d0x2f\CloverMerge\ClassT;
use d0x2f\CloverMerge\Metrics;

/**
* @phan-closure-scope \Kahlan\Scope
*/
describe('ClassT', function () {
describe('__construct', function () {
describe('Receives a namespace and metrics.', function () {
context('Receives a namespace and metrics.', function () {
beforeEach(function () {
$this->metrics = new Metrics(new \Ds\Map([
'foo' => 'bar',
Expand All @@ -22,14 +25,15 @@
});
});
describe('fromXML', function () {
describe('Receives an XML element.', function () {
context('Receives an XML element.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string(
'<?xml version="1.0" encoding="UTF-8"?>
<class name="Example\Namespace\Class" namespace="Example\Namespace">
<metrics foo="bar" baz="fred" />
</class>'
);
assert($xml_element !== false);
$this->instance = ClassT::fromXML($xml_element);
});

Expand Down
15 changes: 12 additions & 3 deletions spec/File.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
use d0x2f\CloverMerge\File;
use d0x2f\CloverMerge\Utilities;

/**
* @phan-closure-scope \Kahlan\Scope
* @phan-file-suppress PhanParamTooMany
*/
describe('File', function () {
describe('__construct', function () {
describe('Receives a package name.', function () {
context('Receives a package name.', function () {
beforeEach(function () {
$this->instance = new File('package_name');
});
Expand All @@ -18,7 +22,7 @@
});
});
describe('fromXML', function () {
describe('Receives a valid XML element.', function () {
context('Receives a valid XML element.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string('
<file name="/src/Example/Namespace/Class.php">
Expand All @@ -31,6 +35,7 @@
<metrics foo="bar" baz="fred"/>
</file>
');
assert($xml_element !== false);
$this->instance = File::fromXML($xml_element, 'package_name');
});

Expand All @@ -42,6 +47,7 @@
expect($this->instance->getPackageName())->toBe('package_name');
});

/** @phan-suppress PhanUndeclaredProperty */
it('has the correct classes set.', function () {
$classes = $this->instance->getClasses();
expect($classes)->toHaveLength(1);
Expand Down Expand Up @@ -77,6 +83,7 @@
<banana/>
</file>
');
assert($xml_element !== false);
allow(Utilities::class)->toReceive('::logWarning')->andReturn();
$this->closure = function () use ($xml_element) {
return File::fromXML($xml_element, 'package_name');
Expand All @@ -93,7 +100,7 @@
});
});
describe('merge', function () {
describe('Receives a second File instance to merge into this one.', function () {
context('Receives a second File instance to merge into this one.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string('
<file name="/src/Example/Namespace/Class.php">
Expand All @@ -106,6 +113,7 @@
<metrics foo="bar" baz="fred"/>
</file>
');
assert($xml_element !== false);
$this->instance = File::fromXML($xml_element, 'package_name');

$xml_element = simplexml_load_string('
Expand All @@ -117,6 +125,7 @@
<metrics foo="barry" baz="freddy"/>
</file>
');
assert($xml_element !== false);
$this->instance->merge(File::fromXML($xml_element, 'other_package_name'));
});

Expand Down
31 changes: 18 additions & 13 deletions spec/Invocation.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use d0x2f\CloverMerge\Accumulator;
use d0x2f\CloverMerge\Utilities;

/**
* @phan-closure-scope \Kahlan\Scope
* @phan-file-suppress PhanParamTooMany
*/
describe('Invocation', function () {
describe('__construct', function () {
describe('Receives a valid cli argument list.', function () {
context('Receives a valid cli argument list.', function () {
beforeEach(function () {
allow('is_file')->toBeCalled()->andReturn(true);
allow('simplexml_load_file')->toBeCalled()->andReturn(
Expand All @@ -21,7 +25,7 @@
expect($this->invocation)->toBeAnInstanceOf('d0x2f\CloverMerge\Invocation');
});
});
describe('Receives an empty cli argument list.', function () {
context('Receives an empty cli argument list.', function () {
beforeEach(function () {
$this->closure = function () {
new Invocation([]);
Expand All @@ -32,7 +36,7 @@
expect($this->closure)->toThrow();
});
});
describe('Receives a cli argument list missing the output option.', function () {
context('Receives a cli argument list missing the output option.', function () {
beforeEach(function () {
$this->closure = function () {
new Invocation(['prog', 'file']);
Expand All @@ -43,7 +47,7 @@
expect($this->closure)->toThrow('Missing required option: output');
});
});
describe('Receives a cli argument list with an invalid mode option.', function () {
context('Receives a cli argument list with an invalid mode option.', function () {
beforeEach(function () {
$this->closure = function () {
new Invocation(['prog', '-o', 'test', '-m', 'bogus', 'file']);
Expand All @@ -54,7 +58,7 @@
expect($this->closure)->toThrow('Merge option must be one of: additive, exclusive or inclusive.');
});
});
describe('Receives a cli argument list without any filenames given.', function () {
context('Receives a cli argument list without any filenames given.', function () {
beforeEach(function () {
$this->closure = function () {
new Invocation(['prog', '-o', 'test']);
Expand All @@ -65,8 +69,8 @@
expect($this->closure)->toThrow("At least one input path is required (preferably two).");
});
});
describe('Receives a cli argument list containing a list of files to merge.', function () {
describe('Where one doesn\'t exist', function () {
context('Receives a cli argument list containing a list of files to merge.', function () {
context('Where one doesn\'t exist', function () {
beforeEach(function () {
allow('is_file')->toBeCalled()->andReturn(false);
$this->closure = function () {
Expand All @@ -79,7 +83,7 @@
});
});

describe('Where one refers to an invalid XML document.', function () {
context('Where one refers to an invalid XML document.', function () {
beforeEach(function () {
allow('is_file')->toBeCalled()->andReturn(true);
allow('simplexml_load_file')->toBeCalled()->andReturn(false);
Expand All @@ -95,10 +99,11 @@
});
});
describe('execute', function () {
describe('With fixtures.', function () {
describe('Executes on all available fixtures.', function () {
context('With fixtures.', function () {
context('Executes on all available fixtures.', function () {
beforeEach(function () {
$fixtures = glob(__DIR__.'/fixtures/*.xml');
assert(is_array($fixtures));
$invocation = new Invocation(array_merge(
[
'prog',
Expand All @@ -121,7 +126,7 @@
});
});
});
describe('With mocked dependencies.', function () {
context('With mocked dependencies.', function () {
beforeEach(function () {
allow('is_file')->toBeCalled()->andReturn(true);
allow('simplexml_load_file')->toBeCalled()->andReturn(
Expand All @@ -134,7 +139,7 @@
$this->invocation->execute();
};
});
describe('Executes an invocation instance where the output file is readable.', function () {
context('Executes an invocation instance where the output file is readable.', function () {
beforeEach(function () {
allow('file_put_contents')->toBeCalled()->andReturn(100);
});
Expand All @@ -153,7 +158,7 @@
expect($this->closure)->toEcho("Files Discovered: 0\nFinal Coverage: 0/0 (0.00%)\n");
});
});
describe('Executes an invocation instance where the output file in unreadable.', function () {
context('Executes an invocation instance where the output file in unreadable.', function () {
beforeEach(function () {
allow('file_put_contents')->toBeCalled()->andReturn(false);
});
Expand Down
12 changes: 8 additions & 4 deletions spec/Line.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace d0x2f\CloverMerge\Spec;

use d0x2f\CloverMerge\Line;
use d0x2f\CloverMerge\Utilities;

/**
* @phan-closure-scope \Kahlan\Scope
*/
describe('Line', function () {
describe('__construct', function () {
describe('Receives a map of properties.', function () {
context('Receives a map of properties.', function () {
beforeEach(function () {
$this->instance = new Line(new \Ds\Map([
'foo' => 'bar',
Expand All @@ -21,12 +23,13 @@
});
});
describe('fromXML', function () {
describe('Receives a valid XML element.', function () {
context('Receives a valid XML element.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string(
'<?xml version="1.0" encoding="UTF-8"?>
<line foo="bar" baz="fred" count="2"/>'
);
assert($xml_element !== false);
$this->instance = Line::fromXML($xml_element);
});

Expand All @@ -45,12 +48,13 @@
expect($this->instance->getCount())->toBe(2);
});
});
describe('Receives a XML element with errors.', function () {
context('Receives a XML element with errors.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string(
'<?xml version="1.0" encoding="UTF-8"?>
<line foo="bar" baz="fred"/>'
);
assert($xml_element !== false);
$this->closure = function () use ($xml_element) {
return Line::fromXML($xml_element);
};
Expand Down
8 changes: 6 additions & 2 deletions spec/Metrics.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use d0x2f\CloverMerge\Metrics;

/**
* @phan-closure-scope \Kahlan\Scope
*/
describe('Metrics', function () {
describe('__construct', function () {
describe('Receives a map of properties.', function () {
context('Receives a map of properties.', function () {
beforeEach(function () {
$this->instance = new Metrics(new \Ds\Map([
'foo' => 'bar',
Expand All @@ -20,12 +23,13 @@
});
});
describe('fromXML', function () {
describe('Receives an XML element.', function () {
context('Receives an XML element.', function () {
beforeEach(function () {
$xml_element = simplexml_load_string(
'<?xml version="1.0" encoding="UTF-8"?>
<metrics foo="bar" baz="fred" />'
);
assert($xml_element !== false);
$this->instance = Metrics::fromXML($xml_element);
});

Expand Down
Loading

0 comments on commit 6ca6426

Please sign in to comment.