Skip to content

Commit

Permalink
add unit tests and fix related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Feb 17, 2017
1 parent 8d08d7c commit fc677d0
Show file tree
Hide file tree
Showing 33 changed files with 755 additions and 30 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
],
"autoload": {
"psr-4": {
"TheIconic\\Synopsis\\": [
"src/"
]
"TheIconic\\Synopsis\\": ["src/", "tests/"]
}
},
"require": {}
Expand Down
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
verbose="true"
bootstrap="vendor/autoload.php">

<php>
<ini name="memory_limit" value="-1"/>
</php>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<!--Don't want to measure code coverage from test classes.-->
<directory suffix=".php">tests</directory>
</exclude>
</whitelist>
</filter>

<testsuites>
<testsuite name="Synopsis">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
10 changes: 4 additions & 6 deletions src/AbstractSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
abstract class AbstractSynopsis
{

/**
* @var string the type
*/
Expand Down Expand Up @@ -46,15 +45,14 @@ abstract class AbstractSynopsis
*/
public function process($value, $depth)
{
$this->type = strtolower(str_replace('Synopsis', '', str_replace(__NAMESPACE__ . '\\', '', get_class($this))));
$parts = explode('\\', get_class($this));
$this->type = strtolower(str_replace('Synopsis', '', end($parts)));

if (is_scalar($value)) {
$this->value = (string) $value;
$this->length = strlen((string) $value);
} else {
if ($this->value instanceof Countable) {
$this->length = count($value);
}
} else if ($value instanceof Countable) {
$this->length = count($value);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/ArraySynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class ArraySynopsis extends AbstractSynopsis
{

/**
* @see parent::process()
* @param $value
Expand Down
1 change: 0 additions & 1 deletion src/BooleanSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class BooleanSynopsis extends AbstractSynopsis
{

/**
* @see parent::process()
* @param $value
Expand Down
1 change: 0 additions & 1 deletion src/DoubleSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class DoubleSynopsis extends AbstractSynopsis
{

/**
* @see parent::process()
* @param $value
Expand Down
9 changes: 6 additions & 3 deletions src/Exception/TraceSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class TraceSynopsis extends AbstractSynopsis
{

/**
* @var string the file
*/
Expand Down Expand Up @@ -43,9 +42,13 @@ public function process($value, $depth)

$this->value = sprintf('%s()', (!empty($value['class'])) ? ($value['class'] . $value['type'] . $value['function']) : $value['function']);

$this->length = $this->line = $value['line'];
if (isset($value['line'])) {
$this->length = $this->line = $value['line'];
}

$this->type = $this->file = $value['file'];
if (isset($value['file'])) {
$this->type = $this->file = $value['file'];
}

$this->function = $value['function'];

Expand Down
7 changes: 5 additions & 2 deletions src/ExceptionSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TheIconic\Synopsis;

use TheIconic\Synopsis\Exception\TraceSynopsis;
use Exception;

/**
* represents and exception
Expand All @@ -11,7 +12,6 @@
*/
class ExceptionSynopsis extends ObjectSynopsis
{

/**
* @var string the file
*/
Expand Down Expand Up @@ -41,7 +41,10 @@ public function process($value, $depth)

// we omit the $depth check here on purpose
foreach ($value->getTrace() as $k => $trace) {
$this->addChild(new TraceSynopsis($trace, $depth), '#' . $k);
$child = new TraceSynopsis();
$child->setFactory($this->getFactory());
$child->process($trace, $depth);
$this->addChild($child, '#' . $k);
}
}
}
9 changes: 4 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,27 @@
*/
class Factory
{

/**
* @var array mappings from value classes to synopsis classes
*/
protected $objectMap = array(
protected $objectMap = [
'Transfer_AbstractObject' => 'TransferObject',
'Transfer_AbstractCollection' => 'TransferCollection',
'Iterator' => 'Iterator',
'IteratorAggregate' => 'Iterator',
'ArrayAccess' => 'Iterator',
);
];

/**
* @var array mappings from resource types to synopsis classes
*/
protected $resourceMap = array(
protected $resourceMap = [
'bzip2' => 'File',
'cpdf' => 'File',
'fdf' => 'File',
'zlib' => 'File',
'stream' => 'Stream',
);
];

/**
* creates the fitting synopsis instance for a value
Expand Down
1 change: 0 additions & 1 deletion src/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
abstract class AbstractFormatter
{

/**
* @param AbstractSynopsis $synopsis
* @param string|null $key
Expand Down
3 changes: 1 addition & 2 deletions src/Formatter/LogFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class LogFileFormatter
{

/**
* @var int the current level in the tree
*/
Expand Down Expand Up @@ -56,7 +55,7 @@ protected function formatTreeSynopsis(AbstractSynopsis $synopsis, $key)

$this->level++;
foreach ($synopsis->getChildren() as $k => $v) {
$formatted[] = str_repeat(' ', $this->level) . $this->format($v, $k);
$formatted[] = str_repeat(' ', $this->level * 2) . $this->format($v, $k);
}
$this->level--;

Expand Down
1 change: 0 additions & 1 deletion src/Object/IteratorSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class IteratorSynopsis extends ObjectSynopsis
{

/**
* @see parent::process()
* @param $value
Expand Down
1 change: 0 additions & 1 deletion src/Object/TransferObjectSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class TransferObjectSynopsis extends ObjectSynopsis
{

/**
* @see parent::process()
* @param $value
Expand Down
1 change: 0 additions & 1 deletion src/ObjectSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class ObjectSynopsis extends AbstractSynopsis
{

/**
* @var string the namespace
*/
Expand Down
15 changes: 14 additions & 1 deletion src/Resource/FileSynopsis.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TheIconic\Synopsis\Object;
namespace TheIconic\Synopsis\Resource;

use TheIconic\Synopsis\ResourceSynopsis;

Expand All @@ -11,4 +11,17 @@
*/
class FileSynopsis extends ResourceSynopsis
{
/**
* @param $value
* @param $depth
*/
public function process($value, $depth)
{
parent::process($value, $depth);

$meta = stream_get_meta_data($value);

$this->value = $meta['uri'];
$this->length = strlen($this->value);
}
}
15 changes: 14 additions & 1 deletion src/Resource/StreamSynopsis.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TheIconic\Synopsis\Object;
namespace TheIconic\Synopsis\Resource;

use TheIconic\Synopsis\ResourceSynopsis;

Expand All @@ -11,4 +11,17 @@
*/
class StreamSynopsis extends ResourceSynopsis
{
/**
* @param $value
* @param $depth
*/
public function process($value, $depth)
{
parent::process($value, $depth);

$meta = stream_get_meta_data($value);

$this->value = $meta['uri'];
$this->length = strlen($this->value);
}
}
27 changes: 27 additions & 0 deletions src/StandardSynopsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,31 @@
*/
class StandardSynopsis extends AbstractSynopsis
{
/**
* @param $value
* @param $depth
*/
public function process($value, $depth)
{
parent::process($value, $depth);

$this->type = $this->detectType($value);
}

/**
* @param $value
* @return string
*/
protected function detectType($value)
{
if ($value === null) {
$type = 'null';
} elseif ($value instanceof Exception) {
$type = 'exception';
} else {
$type = gettype($value);
}

return $type;
}
}
54 changes: 54 additions & 0 deletions tests/ArraySynopsisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace TheIconic\Synopsis;

use PHPUnit\Framework\TestCase;
use Exception;
use TheIconic\Synopsis\AbstractSynopsis;
use TheIconic\Synopsis\ArraySynopsis;
use TheIconic\Synopsis\Factory;
use TheIconic\Synopsis\StringSynopsis;

class ArraySynopsisTest extends TestCase
{
/**
*
*/
public function testProcess()
{
$synopsis = new ArraySynopsis();
$synopsis->setFactory($this->getMockFactory());
$synopsis->process([
'a' => 'b',
'c' => 'd',
], 3);

$this->assertEquals('array', $synopsis->getType());
$this->assertEquals(2, $synopsis->getLength());
$this->assertEquals('', $synopsis->getValue());
$this->assertTrue($synopsis->hasChildren());

$children = $synopsis->getChildren();
$this->assertCount(2, $children);

$this->assertEquals(1, $children['a']);
$this->assertEquals(2, $children['c']);
}

/**
* @return Factory
*/
protected function getMockFactory()
{
$factory = $this->getMockBuilder(Factory::class)
->setMethods(['synopsize'])
->getMock();

$factory->expects($this->exactly(2))
->method('synopsize')
->withConsecutive(['b'], ['d'])
->willReturnOnConsecutiveCalls(1, 2);

return $factory;
}
}
28 changes: 28 additions & 0 deletions tests/BooleanSynopsisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TheIconic\Synopsis;

use PHPUnit\Framework\TestCase;
use Exception;
use TheIconic\Synopsis\AbstractSynopsis;
use TheIconic\Synopsis\ArraySynopsis;
use TheIconic\Synopsis\Factory;
use TheIconic\Synopsis\StringSynopsis;

class BooleanSynopsisTest extends TestCase
{
/**
*
*/
public function testProcess()
{
$synopsis = new BooleanSynopsis();
$synopsis->process(true, 3);

$this->assertEquals('boolean', $synopsis->getType());
$this->assertEquals(1, $synopsis->getLength());
$this->assertEquals('true', $synopsis->getValue());
$this->assertFalse($synopsis->hasChildren());
$this->assertNull($synopsis->getChildren());
}
}
Loading

0 comments on commit fc677d0

Please sign in to comment.