Skip to content

Commit

Permalink
added unit test, updated documentation, code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 1, 2018
1 parent 27a8a5e commit f03818d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ This bundle offers various utility functionality for the Contao CMS.

## Utils

### Models `huh.utils.model`

Method | Description
----------------------|------------
findModelInstanceByPk | Returns a model instance if for a given table and id or null if not exist.

### Routing `huh.utils.routing`

Method | Description
Expand Down
5 changes: 4 additions & 1 deletion src/Model/ModelUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct(ContaoFrameworkInterface $framework)
}

/**
* Returns a model instance if for a given table and id(primary key).
* Return null, if model type or model instance with given id not exist.
*
* @param string $table
* @param mixed $pk
* @param array $options
Expand All @@ -30,7 +33,7 @@ public function __construct(ContaoFrameworkInterface $framework)
*/
public function findModelInstanceByPk(string $table, $pk, array $options = [])
{
if (!($modelClass = Model::getClassFromTable($table))) {
if (!($modelClass = $this->framework->getAdapter(Model::class)->getClassFromTable($table))) {
return null;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/ModelUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Tests\Model;

use Contao\ContentModel;
use Contao\Model;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;

class ModelUtilTest extends ContaoTestCase
{
public function testInstantiation()
{
$util = new ModelUtil($this->mockContaoFramework());
$this->assertInstanceOf(ModelUtil::class, $util);
}

public function testFindModelInstanceByPk()
{
$util = new ModelUtil($this->prepareFramework());
$this->assertNull($util->findModelInstanceByPk('tl_null', 5));
$this->assertNull($util->findModelInstanceByPk('tl_null_class', 5));
$this->assertNull($util->findModelInstanceByPk('tl_content', 4));
$this->assertNull($util->findModelInstanceByPk('tl_content', 'content_null'));
$this->assertSame(5, $util->findModelInstanceByPk('tl_content', 5)->id);
$this->assertSame('alias', $util->findModelInstanceByPk('tl_content', 'alias')->alias);
}

public function prepareFramework()
{
$modelAdapter = $this->mockAdapter([
'getClassFromTable',
]);
$modelAdapter
->method('getClassFromTable')
->with($this->anything())->willReturnCallback(function ($table) {
switch ($table) {
case 'tl_content':
return ContentModel::class;
case 'tl_null_class':
return 'Huh\Null\Class\Nullclass';
default:
return null;
}
})
;

$newsModel = $this->mockClassWithProperties(ContentModel::class, [
'id' => 5,
'alias' => 'alias',
]);

$contentModelAdapter = $this->mockAdapter(['findByPk']);
$contentModelAdapter
->method('findByPk')
->with($this->anything(), $this->anything())
->willReturnCallback(function ($pk, $option) use ($newsModel) {
switch ($pk) {
case 'alias':
return $newsModel;
case 5:
return $newsModel;
default:
return null;
}
})
;

$framework = $this->mockContaoFramework([
Model::class => $modelAdapter,
ContentModel::class => $contentModelAdapter,
]);

return $framework;
}
}

0 comments on commit f03818d

Please sign in to comment.