Skip to content

Commit

Permalink
Form generator en ModelAbstractor
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoralesweb committed Nov 15, 2015
1 parent d2953b2 commit a2a37f1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/bin
composer.phar
composer.lock
.DS_Store
.DS_Store
.idea
5 changes: 4 additions & 1 deletion src/Abstractor/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer;
use Illuminate\Database\Eloquent\Model as LaravelModel;
use App;
use ANavallaSuiza\Crudoado\Contracts\Form\Generator as FormGenerator;

class Model implements ModelAbstractorContract
{
use ConfigurationReader;

protected $dbal;
protected $relationFactory;
protected $generator;

protected $model;
protected $config;
Expand All @@ -22,7 +24,7 @@ class Model implements ModelAbstractorContract
protected $name;
protected $instance;

public function __construct($config, AbstractionLayer $dbal, RelationFactory $relationFactory)
public function __construct($config, AbstractionLayer $dbal, RelationFactory $relationFactory, FormGenerator $generator)
{
if (is_array($config)) {
$this->model = $config['model'];
Expand All @@ -34,6 +36,7 @@ public function __construct($config, AbstractionLayer $dbal, RelationFactory $re

$this->dbal = $dbal;
$this->relationFactory = $relationFactory;
$this->generator = $generator;
}

public function setSlug($slug)
Expand Down
17 changes: 15 additions & 2 deletions src/Abstractor/Eloquent/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory as RelationAbstractorFactoryContract;
use ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager;
use EasySlugger\Slugger;
use ANavallaSuiza\Crudoado\Contracts\Form\Generator as FormGenerator;

class ModelFactory implements ModelAbstractorFactoryContract
{
Expand All @@ -13,13 +14,18 @@ class ModelFactory implements ModelAbstractorFactoryContract
protected $slugger;

protected $allConfiguredModels;
/**
* @var FormGenerator
*/
private $generator;

public function __construct(array $allConfiguredModels, ModelManager $modelManager, RelationAbstractorFactoryContract $relationFactory)
public function __construct(array $allConfiguredModels, ModelManager $modelManager, RelationAbstractorFactoryContract $relationFactory, FormGenerator $generator)
{
$this->allConfiguredModels = $allConfiguredModels;
$this->modelManager = $modelManager;
$this->relationFactory = $relationFactory;
$this->slugger = new Slugger();
$this->generator = $generator;
}

/**
Expand All @@ -42,7 +48,7 @@ public function getBySlug($slug, $id = null)
$modelNamespace = $config;
}

$model = new Model($config, $this->modelManager->getAbstractionLayer($modelNamespace), $this->relationFactory);
$model = new Model($config, $this->modelManager->getAbstractionLayer($modelNamespace), $this->relationFactory, $this->generator);

$model->setSlug($modelSlug)
->setName($modelName);
Expand All @@ -69,4 +75,11 @@ public function getByName($name, $id = null)
{
return $this->getBySlug($this->slugger->slugify($name));
}

public function getByClassName($classname, $id = null)
{
$model = new Model(['model' => $classname], $this->modelManager->getAbstractionLayer($classname), $this->relationFactory, $this->generator);

return $model;
}
}
10 changes: 5 additions & 5 deletions src/Abstractor/Eloquent/Relation/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ANavallaSuiza\Crudoado\Abstractor\Eloquent\Relation;

use ANavallaSuiza\Crudoado\Abstractor\Eloquent\Model;
use App;

class Translation extends Relation
{
Expand All @@ -25,11 +26,10 @@ public function checkEloquentRelationCompatibility()
*/
public function getEditFields()
{
$modelAbstractor = new Model(
get_class($this->eloquentRelation->getRelated()),
$this->modelManager->getAbstractionLayer(get_class($this->eloquentRelation->getRelated())),
\App::make('ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory', array($this->modelManager))
);
/** @var \ANavallaSuiza\Crudoado\Contracts\Abstractor\ModelFactory $modelFactory */
$modelFactory = App::make('ANavallaSuiza\Crudoado\Contracts\Abstractor\ModelFactory');

$modelAbstractor = $modelFactory->getByClassName(get_class($this->eloquentRelation->getRelated()));

$fields = $modelAbstractor->getEditFields();

Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Abstractor/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public function getBySlug($slug, $id = null);
* @return Model
*/
public function getByName($name, $id = null);

public function getByClassName($classname, $id = null);
}
14 changes: 13 additions & 1 deletion tests/Abstractor/Eloquent/ModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ModelFactoryTest extends TestBase
protected $modelManagerMock;
/** @var Mock */
protected $relationMock;
/** @var Mock */
protected $generatorMock;

public function setUp()
{
Expand All @@ -24,8 +26,9 @@ public function setUp()

$this->modelManagerMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager');
$this->relationMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory');
$this->generatorMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Form\Generator');

$this->sut = new ModelFactory($config, $this->modelManagerMock, $this->relationMock);
$this->sut = new ModelFactory($config, $this->modelManagerMock, $this->relationMock, $this->generatorMock);
}

public function test_implements_model__factory_interface()
Expand Down Expand Up @@ -63,4 +66,13 @@ public function test_gets_model_by_slug_and_id()

$this->assertInstanceOf('ANavallaSuiza\Crudoado\Contracts\Abstractor\Model', $model);
}

public function test_gets_model_by_classname()
{
$this->modelManagerMock->shouldReceive('getAbstractionLayer')->once()->andReturn($this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer'));

$model = $this->sut->getByClassName('Crudoado\Tests\Models\User');

$this->assertInstanceOf('ANavallaSuiza\Crudoado\Contracts\Abstractor\Model', $model);
}
}
5 changes: 4 additions & 1 deletion tests/Abstractor/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ModelTest extends TestBase
protected $columnMock;
/** @var Mock */
protected $relationMock;
/** @var Mock */
protected $generatorMock;

public function setUp()
{
Expand All @@ -28,8 +30,9 @@ public function setUp()
$this->dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer');
$this->relationMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory');
$this->columnMock = $this->mock('Doctrine\DBAL\Schema\Column');
$this->generatorMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Form\Generator');

$this->sut = new Model($config['Users'], $this->dbalMock, $this->relationMock);
$this->sut = new Model($config['Users'], $this->dbalMock, $this->relationMock, $this->generatorMock);
}

public function test_implements_model_interface()
Expand Down
24 changes: 7 additions & 17 deletions tests/Abstractor/Eloquent/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,21 @@ public function test_implements_relation_interface()
public function test_get_edit_fields_returns_array_of_fields_with_proper_key()
{
$relationFactoryMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory');
$modelFactoryMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Abstractor\ModelFactory');

$columnMock = $this->mock('Doctrine\DBAL\Schema\Column');

\App::instance('ANavallaSuiza\Crudoado\Contracts\Abstractor\RelationFactory', $relationFactoryMock);
\App::instance('ANavallaSuiza\Crudoado\Contracts\Abstractor\ModelFactory', $modelFactoryMock);

$this->modelManagerMock->shouldReceive('getAbstractionLayer')
->andReturn($dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer'));
$modelFactoryMock->shouldReceive('getByClassName')
->andReturn($this->modelManagerMock);

$dbalMock->shouldReceive('getTableColumns')
->once()
->andReturn([
'id' => $columnMock,
'username' => $columnMock,
'password' => $columnMock,
]);
$dbalMock->shouldReceive('getModel')
->andReturn($dbalMock);

$dbalMock->shouldReceive('getKeyName')
->andReturn(LaravelModel::CREATED_AT, LaravelModel::UPDATED_AT);

$dbalMock->shouldReceive('getEditFields')->atLeast()->once()
$this->modelManagerMock->shouldReceive('getEditFields')->atLeast()->once()
->andReturn([$fieldMock = $this->mock('ANavallaSuiza\Crudoado\Contracts\Abstractor\Field')]);

$fieldMock->shouldReceive('getName')->atLeast()->once()->andReturn($fieldName = 'chompy');
$fieldMock->shouldReceive('setName')->atLeast()->once()->with("translations[][{$fieldName}]");
$fieldMock->shouldReceive('setName')->atLeast()->once()->with(matchesPattern("/^translations\[[\d]\]\[chompy\]/"));

$fields = $this->sut->getEditFields();

Expand Down

0 comments on commit a2a37f1

Please sign in to comment.