Skip to content

Commit

Permalink
add basic property generator unit test (#4)
Browse files Browse the repository at this point in the history
* add basic property generator unit test

* add basic property generator unit test

* fixing coding standard raise phpstan to level 2
  • Loading branch information
dakorpar authored Mar 1, 2019
1 parent a4fc1c7 commit 142a92c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"composer require -d temp/phpstan phpstan/phpstan-strict-rules:^0.11"
],
"phpstan": [
"temp/phpstan/vendor/bin/phpstan analyse -l 1 -c phpstan.neon src --memory-limit 1024M"
"temp/phpstan/vendor/bin/phpstan analyse -l 2 -c phpstan.neon src --memory-limit 1024M"
]
}
}
8 changes: 4 additions & 4 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function generate(?string $table = null, ?string $query = null): void
}
$this->repository->createViewFromQuery($table, $query);
$this->generateEntity($table);
$this->repository->dropView($table, $query);
$this->repository->dropView($table);
return;
}
if ($table !== null) {
Expand Down Expand Up @@ -103,7 +103,7 @@ protected function generateColumn(ClassType $entity, Column $column): void
{
$type = $this->getColumnType($column);

if($this->config->generateProperties) {
if ($this->config->generateProperties) {
$entity->addProperty($column->getField())
->setVisibility($this->config->propertyVisibility)
->addComment('@var ' . $type);
Expand All @@ -114,8 +114,8 @@ protected function generateColumn(ClassType $entity, Column $column): void
$entity->addConstant($columnConstant, $column->getField());
}

if($this->config->generatePhpDocProperties) {
$entity->addComment($this->config->phpDocProperty . ' ' . $type . ' $' .$column->getField());
if ($this->config->generatePhpDocProperties) {
$entity->addComment($this->config->phpDocProperty . ' ' . $type . ' $' . $column->getField());
}

if ($this->config->generateColumnConstant) {
Expand Down
97 changes: 97 additions & 0 deletions tests/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare (strict_types = 1);

namespace Generator;

use DodoIt\EntityGenerator\Entity\Column;
use DodoIt\EntityGenerator\Generator\Config;
use DodoIt\EntityGenerator\Generator\Generator;
use DodoIt\EntityGenerator\Repository\IRepository;
use Nette\PhpGenerator\ClassType;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class GeneratorTest extends TestCase
{

/**
* @var Config
*/
private $config;

/**
* @var IRepository|MockObject
*/
private $repository;

/**
* @var Column[]
*/
private $tableColumns = [];

/**
* @var Generator
*/
private $generator;

protected function setUp(): void
{
$this->tableColumns[] = new Column([
'Field' => 'id',
'Type' => 'int(11)',
'Null' => 'NO',
'Key' => 'PRI',
'Default' => null,
'Extra' => 'auto_increment',
]);
$this->tableColumns[] = new Column([
'Field' => 'title',
'Type' => 'varchar(25)',
'Null' => 'YES',
'Key' => null,
'Default' => null,
'Extra' => null,
]);
$this->tableColumns[] = new Column([
'Field' => 'published',
'Type' => 'tinyint(1)',
'Null' => 'NO',
'Key' => null,
'Default' => 1,
'Extra' => null,
]);
$this->tableColumns[] = new Column([
'Field' => 'created_at',
'Type' => 'datetime',
'Null' => 'YES',
'Key' => null,
'Default' => 1,
'Extra' => null,
]);
$this->config = new Config();
$this->repository = $this->getMockForAbstractClass(IRepository::class);
$this->generator = new Generator($this->repository, $this->config);
}

public function testGenerate_WithPropertiesOnly_ShouldGenerateOnlyProperties()
{
$this->config->generateProperties = true;
$this->config->generateGetters = false;
$this->config->generateSetters = false;
$this->config->generateColumnConstant = false;
$this->config->generatePhpDocProperties = false;
$this->repository->expects($this->once())->method('getTableColumns')->with('articles')->willReturn($this->tableColumns);
$this->config->path = __DIR__;
$this->generator->generateEntity('articles');
$entityFile = __DIR__ . '/ArticleEntity.php';
$this->assertFileExists($entityFile);
include $entityFile;
$entityClass = ClassType::from($this->config->namespace . '\ArticleEntity');
$properties = $entityClass->getProperties();
$this->assertCount(count($this->tableColumns), $properties);
$this->assertEquals($properties['id']->getComment(), '@var int');
$this->assertEquals($properties['title']->getComment(), '@var string');
$this->assertEquals($properties['created_at']->getComment(), '@var \DateTime');
unlink($entityFile);
}

}

0 comments on commit 142a92c

Please sign in to comment.