From fc677d029f2b853a89ffdcc9168ccf8721b44185 Mon Sep 17 00:00:00 2001 From: Andre Wyrwa Date: Fri, 17 Feb 2017 23:16:04 +1100 Subject: [PATCH] add unit tests and fix related issues --- composer.json | 4 +- phpunit.xml.dist | 29 ++++++++++ src/AbstractSynopsis.php | 10 ++-- src/ArraySynopsis.php | 1 - src/BooleanSynopsis.php | 1 - src/DoubleSynopsis.php | 1 - src/Exception/TraceSynopsis.php | 9 ++-- src/ExceptionSynopsis.php | 7 ++- src/Factory.php | 9 ++-- src/Formatter/AbstractFormatter.php | 1 - src/Formatter/LogFileFormatter.php | 3 +- src/Object/IteratorSynopsis.php | 1 - src/Object/TransferObjectSynopsis.php | 1 - src/ObjectSynopsis.php | 1 - src/Resource/FileSynopsis.php | 15 +++++- src/Resource/StreamSynopsis.php | 15 +++++- src/StandardSynopsis.php | 27 ++++++++++ tests/ArraySynopsisTest.php | 54 +++++++++++++++++++ tests/BooleanSynopsisTest.php | 28 ++++++++++ tests/DoubleSynopsisTest.php | 28 ++++++++++ tests/Exception/TraceSynopsisTest.php | 51 ++++++++++++++++++ tests/ExceptionSynopsisTest.php | 50 ++++++++++++++++++ tests/Formatter/ArrayFormatterTest.php | 59 +++++++++++++++++++++ tests/Formatter/LogFileFormatterTest.php | 39 ++++++++++++++ tests/IntegerSynopsisTest.php | 28 ++++++++++ tests/NullSynopsisTest.php | 28 ++++++++++ tests/Object/IteratorSynopsisTest.php | 51 ++++++++++++++++++ tests/ObjectSynopsisTest.php | 67 ++++++++++++++++++++++++ tests/Resource/FileSynopsisTest.php | 28 ++++++++++ tests/Resource/StreamSynopsisTest.php | 28 ++++++++++ tests/ResourceSynopsisTest.php | 28 ++++++++++ tests/StandardSynopsisTest.php | 55 +++++++++++++++++++ tests/StringSynopsisTest.php | 28 ++++++++++ 33 files changed, 755 insertions(+), 30 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 tests/ArraySynopsisTest.php create mode 100644 tests/BooleanSynopsisTest.php create mode 100644 tests/DoubleSynopsisTest.php create mode 100644 tests/Exception/TraceSynopsisTest.php create mode 100644 tests/ExceptionSynopsisTest.php create mode 100644 tests/Formatter/ArrayFormatterTest.php create mode 100644 tests/Formatter/LogFileFormatterTest.php create mode 100644 tests/IntegerSynopsisTest.php create mode 100644 tests/NullSynopsisTest.php create mode 100644 tests/Object/IteratorSynopsisTest.php create mode 100644 tests/ObjectSynopsisTest.php create mode 100644 tests/Resource/FileSynopsisTest.php create mode 100644 tests/Resource/StreamSynopsisTest.php create mode 100644 tests/ResourceSynopsisTest.php create mode 100644 tests/StandardSynopsisTest.php create mode 100644 tests/StringSynopsisTest.php diff --git a/composer.json b/composer.json index 25f24ad..846a3b3 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,7 @@ ], "autoload": { "psr-4": { - "TheIconic\\Synopsis\\": [ - "src/" - ] + "TheIconic\\Synopsis\\": ["src/", "tests/"] } }, "require": {} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..9743b39 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + + + + + + src + + + tests + + + + + + + tests + + + diff --git a/src/AbstractSynopsis.php b/src/AbstractSynopsis.php index d0e4e60..f612413 100644 --- a/src/AbstractSynopsis.php +++ b/src/AbstractSynopsis.php @@ -12,7 +12,6 @@ */ abstract class AbstractSynopsis { - /** * @var string the type */ @@ -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); } } diff --git a/src/ArraySynopsis.php b/src/ArraySynopsis.php index ba53223..16a8bfb 100644 --- a/src/ArraySynopsis.php +++ b/src/ArraySynopsis.php @@ -9,7 +9,6 @@ */ class ArraySynopsis extends AbstractSynopsis { - /** * @see parent::process() * @param $value diff --git a/src/BooleanSynopsis.php b/src/BooleanSynopsis.php index 591a7e2..fa952db 100644 --- a/src/BooleanSynopsis.php +++ b/src/BooleanSynopsis.php @@ -9,7 +9,6 @@ */ class BooleanSynopsis extends AbstractSynopsis { - /** * @see parent::process() * @param $value diff --git a/src/DoubleSynopsis.php b/src/DoubleSynopsis.php index 7ff2a38..7e75be8 100644 --- a/src/DoubleSynopsis.php +++ b/src/DoubleSynopsis.php @@ -9,7 +9,6 @@ */ class DoubleSynopsis extends AbstractSynopsis { - /** * @see parent::process() * @param $value diff --git a/src/Exception/TraceSynopsis.php b/src/Exception/TraceSynopsis.php index 9972663..7bfa20a 100644 --- a/src/Exception/TraceSynopsis.php +++ b/src/Exception/TraceSynopsis.php @@ -11,7 +11,6 @@ */ class TraceSynopsis extends AbstractSynopsis { - /** * @var string the file */ @@ -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']; diff --git a/src/ExceptionSynopsis.php b/src/ExceptionSynopsis.php index f0baad4..37156f1 100644 --- a/src/ExceptionSynopsis.php +++ b/src/ExceptionSynopsis.php @@ -3,6 +3,7 @@ namespace TheIconic\Synopsis; use TheIconic\Synopsis\Exception\TraceSynopsis; +use Exception; /** * represents and exception @@ -11,7 +12,6 @@ */ class ExceptionSynopsis extends ObjectSynopsis { - /** * @var string the file */ @@ -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); } } } diff --git a/src/Factory.php b/src/Factory.php index 567a0d5..87ab6da 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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 diff --git a/src/Formatter/AbstractFormatter.php b/src/Formatter/AbstractFormatter.php index 615d0b8..9278ad4 100644 --- a/src/Formatter/AbstractFormatter.php +++ b/src/Formatter/AbstractFormatter.php @@ -10,7 +10,6 @@ */ abstract class AbstractFormatter { - /** * @param AbstractSynopsis $synopsis * @param string|null $key diff --git a/src/Formatter/LogFileFormatter.php b/src/Formatter/LogFileFormatter.php index 78cf719..5ebf759 100644 --- a/src/Formatter/LogFileFormatter.php +++ b/src/Formatter/LogFileFormatter.php @@ -11,7 +11,6 @@ */ class LogFileFormatter { - /** * @var int the current level in the tree */ @@ -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--; diff --git a/src/Object/IteratorSynopsis.php b/src/Object/IteratorSynopsis.php index d85b442..1bffc16 100644 --- a/src/Object/IteratorSynopsis.php +++ b/src/Object/IteratorSynopsis.php @@ -11,7 +11,6 @@ */ class IteratorSynopsis extends ObjectSynopsis { - /** * @see parent::process() * @param $value diff --git a/src/Object/TransferObjectSynopsis.php b/src/Object/TransferObjectSynopsis.php index c1dfdda..5fda068 100644 --- a/src/Object/TransferObjectSynopsis.php +++ b/src/Object/TransferObjectSynopsis.php @@ -11,7 +11,6 @@ */ class TransferObjectSynopsis extends ObjectSynopsis { - /** * @see parent::process() * @param $value diff --git a/src/ObjectSynopsis.php b/src/ObjectSynopsis.php index 38db070..f1a41e4 100644 --- a/src/ObjectSynopsis.php +++ b/src/ObjectSynopsis.php @@ -12,7 +12,6 @@ */ class ObjectSynopsis extends AbstractSynopsis { - /** * @var string the namespace */ diff --git a/src/Resource/FileSynopsis.php b/src/Resource/FileSynopsis.php index 33f1924..e1cac11 100644 --- a/src/Resource/FileSynopsis.php +++ b/src/Resource/FileSynopsis.php @@ -1,6 +1,6 @@ value = $meta['uri']; + $this->length = strlen($this->value); + } } diff --git a/src/Resource/StreamSynopsis.php b/src/Resource/StreamSynopsis.php index 14ebeea..4809732 100644 --- a/src/Resource/StreamSynopsis.php +++ b/src/Resource/StreamSynopsis.php @@ -1,6 +1,6 @@ value = $meta['uri']; + $this->length = strlen($this->value); + } } diff --git a/src/StandardSynopsis.php b/src/StandardSynopsis.php index 6eb4714..9d8bf00 100644 --- a/src/StandardSynopsis.php +++ b/src/StandardSynopsis.php @@ -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; + } } diff --git a/tests/ArraySynopsisTest.php b/tests/ArraySynopsisTest.php new file mode 100644 index 0000000..eca1f72 --- /dev/null +++ b/tests/ArraySynopsisTest.php @@ -0,0 +1,54 @@ +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; + } +} \ No newline at end of file diff --git a/tests/BooleanSynopsisTest.php b/tests/BooleanSynopsisTest.php new file mode 100644 index 0000000..791d174 --- /dev/null +++ b/tests/BooleanSynopsisTest.php @@ -0,0 +1,28 @@ +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()); + } +} \ No newline at end of file diff --git a/tests/DoubleSynopsisTest.php b/tests/DoubleSynopsisTest.php new file mode 100644 index 0000000..029a371 --- /dev/null +++ b/tests/DoubleSynopsisTest.php @@ -0,0 +1,28 @@ +process(3.4, 3); + + $this->assertEquals('double', $synopsis->getType()); + $this->assertEquals(3, $synopsis->getLength()); + $this->assertEquals(3.4, $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/Exception/TraceSynopsisTest.php b/tests/Exception/TraceSynopsisTest.php new file mode 100644 index 0000000..40ac168 --- /dev/null +++ b/tests/Exception/TraceSynopsisTest.php @@ -0,0 +1,51 @@ +getSynopsis(0); + + $this->assertEquals(__LINE__ - 2, $synopsis->getLine()); + $this->assertEquals(__FILE__, $synopsis->getFile()); + $this->assertEquals('getSynopsis', $synopsis->getFunction()); + $this->assertEquals(__CLASS__, $synopsis->getClass()); + + /** @var AbstractSynopsis $firstChild */ + $firstChild = $synopsis->getChildren()[0]; + + $this->assertInstanceOf(AbstractSynopsis::class, $firstChild); + + $this->assertEquals('integer', $firstChild->getType()); + $this->assertEquals(0, $firstChild->getValue()); + $this->assertEquals(1, $firstChild->getLength()); + } + + /** + * @param int $index + * @return TraceSynopsis + */ + protected function getSynopsis(int $index) + { + $synopsis = new TraceSynopsis(); + + try { + throw new Exception(); + } catch (Exception $e) { + $trace = $e->getTrace(); + + $synopsis->process($trace[0], 3); + } + + return $synopsis; + } +} \ No newline at end of file diff --git a/tests/ExceptionSynopsisTest.php b/tests/ExceptionSynopsisTest.php new file mode 100644 index 0000000..6f3dea4 --- /dev/null +++ b/tests/ExceptionSynopsisTest.php @@ -0,0 +1,50 @@ +getSynopsis(); + + $this->assertEquals('Exception', $synopsis->getType()); + $this->assertEquals(12, $synopsis->getLength()); + $this->assertEquals('Test Exception', $synopsis->getValue()); + $this->assertTrue($synopsis->hasChildren()); + + $children = $synopsis->getChildren(); + $this->assertCount(12, $children); + + $this->assertInstanceOf(TraceSynopsis::class, $children['#0']); + $this->assertInstanceOf(TraceSynopsis::class, $children['#11']); + } + + /** + * @param int $index + * @return ExceptionSynopsis + */ + protected function getSynopsis() + { + $synopsis = new ExceptionSynopsis(); + + try { + throw new Exception('Test Exception'); + } catch (Exception $e) { + $synopsis->process($e, 3); + } + + return $synopsis; + } +} \ No newline at end of file diff --git a/tests/Formatter/ArrayFormatterTest.php b/tests/Formatter/ArrayFormatterTest.php new file mode 100644 index 0000000..b4744b6 --- /dev/null +++ b/tests/Formatter/ArrayFormatterTest.php @@ -0,0 +1,59 @@ + 'c', + 'b' => 1, + 'c' => null + ]; + + /** + * + */ + public function testFormat() + { + $synopsis = new ObjectSynopsis(); + $synopsis->process(new self(), 3); + + $formatter = new ArrayFormatter(); + + $this->assertEquals([ + 'type' => __CLASS__, + 'length' => 1, + 'value' => '', + 'children' => [ + 'child' => [ + 'type' => 'array', + 'length' => 3, + 'value' => '', + 'children' => [ + 'a' => [ + 'type' => 'string', + 'length' => 1, + 'value' => 'c', + ], + 'b' => [ + 'type' => 'integer', + 'length' => 1, + 'value' => 1, + ], + 'c' => [ + 'type' => 'NULL', + 'length' => 0, + 'value' => '' + ], + ], + ], + ], + ], $formatter->format($synopsis)); + } +} \ No newline at end of file diff --git a/tests/Formatter/LogFileFormatterTest.php b/tests/Formatter/LogFileFormatterTest.php new file mode 100644 index 0000000..2df60d6 --- /dev/null +++ b/tests/Formatter/LogFileFormatterTest.php @@ -0,0 +1,39 @@ + 'c', + 'b' => 1, + 'c' => null + ]; + + /** + * + */ + public function testFormat() + { + $synopsis = new ObjectSynopsis(); + $synopsis->process(new self(), 3); + + $formatter = new LogFileFormatter(); + + $expectation = <<assertEquals($expectation, $formatter->format($synopsis, 'Test')); + } +} \ No newline at end of file diff --git a/tests/IntegerSynopsisTest.php b/tests/IntegerSynopsisTest.php new file mode 100644 index 0000000..03b9703 --- /dev/null +++ b/tests/IntegerSynopsisTest.php @@ -0,0 +1,28 @@ +process(3, 3); + + $this->assertEquals('integer', $synopsis->getType()); + $this->assertEquals(1, $synopsis->getLength()); + $this->assertEquals(3, $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/NullSynopsisTest.php b/tests/NullSynopsisTest.php new file mode 100644 index 0000000..88f7bea --- /dev/null +++ b/tests/NullSynopsisTest.php @@ -0,0 +1,28 @@ +process(null, 3); + + $this->assertEquals('NULL', $synopsis->getType()); + $this->assertEquals(0, $synopsis->getLength()); + $this->assertEquals(null, $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/Object/IteratorSynopsisTest.php b/tests/Object/IteratorSynopsisTest.php new file mode 100644 index 0000000..6f0087b --- /dev/null +++ b/tests/Object/IteratorSynopsisTest.php @@ -0,0 +1,51 @@ +setFactory($this->getMockFactory()); + $synopsis->process(new ArrayIterator([ + 'a' => 'b', + 'c' => 'd', + ]), 3); + + $this->assertEquals('ArrayIterator', $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; + } +} \ No newline at end of file diff --git a/tests/ObjectSynopsisTest.php b/tests/ObjectSynopsisTest.php new file mode 100644 index 0000000..6c77799 --- /dev/null +++ b/tests/ObjectSynopsisTest.php @@ -0,0 +1,67 @@ +setFactory($this->getMockFactory()); + $synopsis->process($object, 3); + + $this->assertEquals(__CLASS__, $synopsis->getType()); + $this->assertEquals(1, $synopsis->getLength()); + $this->assertEquals('', $synopsis->getValue()); + $this->assertTrue($synopsis->hasChildren()); + + $children = $synopsis->getChildren(); + $this->assertCount(1, $children); + + $this->assertEquals(1, $children['publicProp']); + } + + /** + * @return Factory + */ + protected function getMockFactory() + { + $factory = $this->getMockBuilder(Factory::class) + ->setMethods(['synopsize']) + ->getMock(); + + $factory->expects($this->exactly(1)) + ->method('synopsize') + ->with('a', 3) + ->willReturn(1); + + return $factory; + } +} \ No newline at end of file diff --git a/tests/Resource/FileSynopsisTest.php b/tests/Resource/FileSynopsisTest.php new file mode 100644 index 0000000..9d40718 --- /dev/null +++ b/tests/Resource/FileSynopsisTest.php @@ -0,0 +1,28 @@ +process(STDIN, 3); + + $this->assertEquals('file', $synopsis->getType()); + $this->assertEquals(11, $synopsis->getLength()); + $this->assertEquals('php://stdin', $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/Resource/StreamSynopsisTest.php b/tests/Resource/StreamSynopsisTest.php new file mode 100644 index 0000000..da625ce --- /dev/null +++ b/tests/Resource/StreamSynopsisTest.php @@ -0,0 +1,28 @@ +process(STDIN, 3); + + $this->assertEquals('stream', $synopsis->getType()); + $this->assertEquals(11, $synopsis->getLength()); + $this->assertEquals('php://stdin', $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/ResourceSynopsisTest.php b/tests/ResourceSynopsisTest.php new file mode 100644 index 0000000..88b68c6 --- /dev/null +++ b/tests/ResourceSynopsisTest.php @@ -0,0 +1,28 @@ +process(STDIN, 3); + + $this->assertEquals('resource', $synopsis->getType()); + $this->assertEquals(0, $synopsis->getLength()); + $this->assertEquals('', $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/StandardSynopsisTest.php b/tests/StandardSynopsisTest.php new file mode 100644 index 0000000..76444d2 --- /dev/null +++ b/tests/StandardSynopsisTest.php @@ -0,0 +1,55 @@ +process($original, 3); + + $this->assertEquals($type, $synopsis->getType()); + $this->assertEquals($length, $synopsis->getLength()); + $this->assertEquals($value, $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file diff --git a/tests/StringSynopsisTest.php b/tests/StringSynopsisTest.php new file mode 100644 index 0000000..611cc10 --- /dev/null +++ b/tests/StringSynopsisTest.php @@ -0,0 +1,28 @@ +process('Hello World!', 3); + + $this->assertEquals('string', $synopsis->getType()); + $this->assertEquals(12, $synopsis->getLength()); + $this->assertEquals('Hello World!', $synopsis->getValue()); + $this->assertFalse($synopsis->hasChildren()); + $this->assertNull($synopsis->getChildren()); + } +} \ No newline at end of file