Skip to content

Commit

Permalink
Merge pull request #7 from phcorp/master
Browse files Browse the repository at this point in the history
increase code coverage
  • Loading branch information
phcorp authored Mar 31, 2017
2 parents 217bc38 + 0cca3f1 commit a69cf39
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 80 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ Render a graphical representation of a Doctrine2 mapped database.

![Example](example.png)

## To do

- [ ] Add phpunit tests
- [ ] Add mongoDB support

## Installation

Require package through composer:
Expand Down
47 changes: 19 additions & 28 deletions src/Command/DoctrinevizCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ public function execute(InputInterface $input, OutputInterface $output)
$tables = [];
foreach ($entities as $entity) {
$metadata = $em->getClassMetadata($entity);
if (!$metadata->getFieldNames()) {
continue;
}
$table = $graph->createVertex($metadata->getTableName());
$table->createAttribute('shape', 'record');
$table->createAttribute('width', '4');
foreach ($metadata->getFieldNames() as $fieldName) {
$fieldMapping = $metadata->getFieldMapping($fieldName);
$table->addRecord(new Record($this->getFieldMappingDisplayName($fieldMapping)));
if ($metadata->getFieldNames()) {
$table = $graph->createVertex($metadata->getTableName());
$table->createAttribute('shape', 'record');
$table->createAttribute('width', '4');
foreach ($metadata->getFieldNames() as $fieldName) {
$fieldMapping = $metadata->getFieldMapping($fieldName);
$table->addRecord(new Record($this->getFieldMappingDisplayName($fieldMapping)));
}
$tables[$entity] = $table;
}
$tables[$entity] = $table;
}
foreach ($entities as $entity) {
$metadata = $em->getClassMetadata($entity);
Expand All @@ -100,11 +99,7 @@ public function execute(InputInterface $input, OutputInterface $output)
if (array_key_exists($sourceEntity, $tables)) {
$name = $this->getFieldMappingDisplayName($joinColumn, 'referencedColumnName');
$nullable = $joinColumn['nullable'];
$to = $graph->getVertex($tables[$sourceEntity]->getId())->getRecord($name);
if (!$to) {
$to = new Record($name);
$tables[$sourceEntity]->addRecord($to);
}
$tables[$sourceEntity]->addRecord($to = $graph->getVertex($tables[$sourceEntity]->getId())->getRecord($name) ?: new Record($name));
}
$edge = $record->addEdgeTo($to);
$edge->createAttribute('headlabel', $nullable ? '0..1' : '1');
Expand All @@ -121,11 +116,7 @@ public function execute(InputInterface $input, OutputInterface $output)
if (array_key_exists($targetEntity, $tables)) {
$name = $this->getFieldMappingDisplayName($inverseJoinColumn, 'referencedColumnName');
$nullable = $inverseJoinColumn['nullable'];
$to = $graph->getVertex($tables[$targetEntity]->getId())->getRecord($name);
if (!$to) {
$to = new Record($name);
$tables[$targetEntity]->addRecord($to);
}
$tables[$targetEntity]->addRecord($to = $graph->getVertex($tables[$targetEntity]->getId())->getRecord($name) ?: new Record($name));
}
$edge = $record->addEdgeTo($to);
$edge->createAttribute('headlabel', $nullable ? '0..1' : '1');
Expand Down Expand Up @@ -168,17 +159,17 @@ public function execute(InputInterface $input, OutputInterface $output)
$binary = $input->getOption('binary', null);
$path = $input->getOption('output-path', null);
$graphviz = new Graphviz($format, $binary);
if (!$path && 'dot' === $format) {
$output->writeln((string) $graph);

return 0;
} elseif (!$path) {
$graphviz->display($graph);
if (!$path) {
if ('dot' === $format) {
$output->writeln((string)$graph);
} else {
$graphviz->display($graph);
}

return 0;
} else {
return (int) !file_put_contents($path, $graphviz->createImageData($graph));
}

return (int) !file_put_contents($path, $graphviz->createImageData($graph));
}

/**
Expand Down
79 changes: 56 additions & 23 deletions src/Graphviz/Graphviz.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,30 @@ public function __construct($format = 'png', $binary = null)
*
* @param Graph $graph to display
*
* @throws \UnexpectedValueException on error
* @throws \RuntimeException on error
*
* @return string filename
*/
public function createImageFile(Graph $graph)
{
// @codeCoverageIgnoreStart
if (false === $tmp = tempnam(sys_get_temp_dir(), 'doctrineviz')) {
throw new \UnexpectedValueException('Unable to get temporary file name for graphviz script');
throw new \RuntimeException('Unable to get temporary file name for graphviz script');
}
if (false === file_put_contents($tmp, (string) $graph, LOCK_EX)) {
throw new \UnexpectedValueException('Unable to write graphviz script to temporary file');
throw new \RuntimeException('Unable to write graphviz script to temporary file');
}
$process = new Process(escapeshellarg($this->binary).' -T '.escapeshellarg($this->format).' '.escapeshellarg($tmp).' -o '.escapeshellarg($tmp.'.'.$this->format));
$process->mustRun();
unlink($tmp);

return $tmp.'.'.$this->format;
// @codeCoverageIgnoreEnd
$path = "$tmp.{$this->format}";
$this->execute(
'%s -T %s %s -o %s',
$this->binary,
$this->format,
$tmp,
$path
);

return $path;
}

/**
Expand All @@ -76,19 +83,24 @@ public function createImageFile(Graph $graph)
*/
public function display(Graph $graph)
{
$path = $this->createImageFile($graph);
if (0 === strpos(strtoupper(PHP_OS), 'WIN')) {
// open image in untitled, temporary background shell
$command = 'start';
} elseif ('DARWIN' === strtoupper(PHP_OS)) {
// open image in background (redirect stdout to /dev/null, sterr to stdout and run in background)
$command = 'open';
} else {
// open image in background (redirect stdout to /dev/null, sterr to stdout and run in background)
$command = 'xdg-open';
// @codeCoverageIgnoreStart
switch (true) {
case 0 === strpos(strtoupper(PHP_OS), 'WIN'):
$binary = 'start';
break;
case 'DARWIN' === strtoupper(PHP_OS):
$binary = 'open';
break;
default:
$binary = 'xdg-open';
}
$process = new Process($command.' '.escapeshellarg($path));
$process->mustRun();
// @codeCoverageIgnoreEnd
$path = $this->createImageFile($graph);
$this->execute(
'%s %s',
$binary,
$path
);
}

/**
Expand All @@ -103,9 +115,9 @@ public function createImageData(Graph $graph)
if ('dot' === $this->format) {
return (string) $graph;
}
$file = $this->createImageFile($graph);
$data = file_get_contents($file);
unlink($file);
$path = $this->createImageFile($graph);
$data = file_get_contents($path);
unlink($path);

return $data;
}
Expand Down Expand Up @@ -140,6 +152,27 @@ public function createImageHtml(Graph $graph)
return '<img src="'.$this->createImageSrc($graph).'" />';
}

/**
* Executes a command.
*
* @param string $format of the command
* @param array ...$args to escape for shell
*
* @return Process
*/
protected function execute($format, ...$args)
{
$process = new Process(sprintf(
$format,
...array_map(function($arg, $index) use ($format) {
return 0 === $index && 0 === strpos('%s', $format) ? escapeshellcmd($arg) : escapeshellarg($arg);
}, $args, array_keys($args))
));
$process->mustRun();

return $process;
}

/**
* Get format.
*
Expand Down
44 changes: 42 additions & 2 deletions test/Command/DoctrinevizCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace Janalis\Doctrineviz\Test\Command;

use Janalis\Doctrineviz\Command\DoctrinevizCommand;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Janalis\Doctrineviz\Test\DoctrinevizTestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

Expand All @@ -25,7 +25,7 @@
*
* @coversDefaultClass \Janalis\Doctrineviz\Command\DoctrinevizCommand
*/
class DoctrinevizCommandTest extends WebTestCase
class DoctrinevizCommandTest extends DoctrinevizTestCase
{
/**
* Test configure.
Expand Down Expand Up @@ -128,4 +128,44 @@ public function testExecuteUpdateExample()
$output = new BufferedOutput();
$command->execute($input, $output);
}

/**
* Test get field mapping display name.
*
* @covers ::getFieldMappingDisplayName
* @group command
*/
public function testGetFieldMappingDisplayName()
{
$name = 'foo';
$command = new DoctrinevizCommand();
// default key is columnName and default doctrine type is integer
$this->assertEquals("$name : integer", $this->callProtectedMethod(
$command,
'getFieldMappingDisplayName',
[
'columnName' => $name,
]
));
// key can be passed as a parameter
$key = 'bar';
$this->assertEquals("$name : integer", $this->callProtectedMethod(
$command,
'getFieldMappingDisplayName',
[
$key => $name,
],
$key
));
// declared doctrine type is returned
$type = 'baz';
$this->assertEquals("$name : $type", $this->callProtectedMethod(
$command,
'getFieldMappingDisplayName',
[
'columnName' => $name,
'type' => $type,
]
));
}
}
15 changes: 13 additions & 2 deletions test/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@
namespace Janalis\Doctrineviz\Test\DependencyInjection;

use Janalis\Doctrineviz\DependencyInjection\Configuration;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Janalis\Doctrineviz\Test\DoctrinevizTestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

/**
* Configuration test.
*
* @coversDefaultClass \Janalis\Doctrineviz\DependencyInjection\Configuration
*/
class ConfigurationTest extends WebTestCase
class ConfigurationTest extends DoctrinevizTestCase
{
/**
* Test constructor.
*
* @covers ::__construct
* @group dependecy_injection
*/
public function testConstructor()
{
new Configuration('foo');
}

/**
* Test get root.
*
Expand Down
4 changes: 2 additions & 2 deletions test/DependencyInjection/DoctrinevizBundleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
namespace Janalis\Doctrineviz\Test\DependencyInjection;

use Janalis\Doctrineviz\DependencyInjection\DoctrinevizBundleExtension;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Janalis\Doctrineviz\Test\DoctrinevizTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Doctrineviz bundle extension test.
*
* @coversDefaultClass \Janalis\Doctrineviz\DependencyInjection\DoctrinevizBundleExtension
*/
class DoctrinevizBundleExtensionTest extends WebTestCase
class DoctrinevizBundleExtensionTest extends DoctrinevizTestCase
{
/**
* Test prepend.
Expand Down
2 changes: 1 addition & 1 deletion test/DoctrinevizBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @coversDefaultClass \Janalis\Doctrineviz\DoctrinevizBundle
*/
class DoctrinevizBundleTest extends \PHPUnit_Framework_TestCase
class DoctrinevizBundleTest extends DoctrinevizTestCase
{
/**
* Test get container extension.
Expand Down
44 changes: 44 additions & 0 deletions test/DoctrinevizTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Janalis\Doctrineviz\Test;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* Doctrineviz test case.
*/
class DoctrinevizTestCase extends WebTestCase
{
/**
* Call protected method.
*
* @param object $object
* @param string $methodName
* @param array ...$arguments
*
* @return mixed
*/
protected function callProtectedMethod($object, $methodName, ...$arguments)
{
$method = $this->setMethodAccessible(get_class($object), $methodName);

return $method->invokeArgs($object, $arguments);
}

/**
* Set method accessible.
*
* @param string $className
* @param string $methodName
*
* @return \ReflectionMethod
*/
protected function setMethodAccessible($className, $methodName)
{
$class = new \ReflectionClass($className);
$method = $class->getMethod($methodName);
$method->setAccessible(true);

return $method;
}
}
4 changes: 2 additions & 2 deletions test/Graphviz/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
namespace Janalis\Doctrineviz\Test\Graphviz;

use Janalis\Doctrineviz\Graphviz\Attribute;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Janalis\Doctrineviz\Test\DoctrinevizTestCase;

/**
* Attribute test.
*
* @coversDefaultClass \Janalis\Doctrineviz\Graphviz\Attribute
*/
class AttributeTest extends WebTestCase
class AttributeTest extends DoctrinevizTestCase
{
/**
* Test accessors.
Expand Down
Loading

0 comments on commit a69cf39

Please sign in to comment.