Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Added Scaffolding Mysql adapter tests; Removed BeforeException class;…
Browse files Browse the repository at this point in the history
… Updated config.sample.php; Updated travis.yml
  • Loading branch information
szytko committed Dec 17, 2014
1 parent 8c0ee75 commit fa8872d
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ before_script:
- php composer.phar install --dev

script:
- cp tests/config.sample.php tests/config.php
- mkdir -p tests/fixtures/cache
- mkdir -p tests/fixtures/tmp
- mkdir -p build/logs
- php vendor/bin/phpunit -c travis/phpunit.xml.dist

Expand Down
22 changes: 21 additions & 1 deletion src/DI/Scaffolding/Adapter/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Vegas\Db\AdapterInterface;
use Vegas\Db\Exception\NoRequiredServiceException;
use Vegas\DI\Scaffolding\AdapterInterface as ScaffoldingAdapterInterface;
use Vegas\DI\Scaffolding\Exception\MissingScaffoldingException;
use Vegas\DI\Scaffolding\Exception\RecordNotFoundException;
use Vegas\DI\Scaffolding;

Expand Down Expand Up @@ -51,6 +52,8 @@ public function __construct()
*/
public function retrieveOne($id)
{
$this->ensureScaffolding();

$record = call_user_func(array($this->scaffolding->getRecord(),'findById'),$id);

if (!$record) {
Expand All @@ -65,8 +68,10 @@ public function retrieveOne($id)
*/
public function getPaginator($page = 1, $limit = 10)
{
$this->ensureScaffolding();

return new PaginatorAdapterModel(array(
'data' => call_user_func(array($this->scaffolding->getRecord(),'find')),
'data' => (object) call_user_func(array($this->scaffolding->getRecord(), 'find')),
'limit' => $limit,
'page' => $page
));
Expand All @@ -90,4 +95,19 @@ public function verifyRequiredServices(DiInterface $di)
throw new NoRequiredServiceException();
}
}

/**
* Determines if scaffolding has been set
*
* @return bool
* @throws \Vegas\DI\Scaffolding\Exception\MissingScaffoldingException
*/
protected function ensureScaffolding()
{
if (!$this->scaffolding instanceof Scaffolding) {
throw new MissingScaffoldingException();
}

return true;
}
}
23 changes: 23 additions & 0 deletions src/DI/Scaffolding/Exception/MissingScaffoldingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* This file is part of Vegas package
*
* @author Slawomir Zytko <slawomir.zytko@gmail.com>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\DI\Scaffolding\Exception;

/**
* Class MissingScaffoldingException
* @package Vegas\DI\Scaffolding\Exception
*/
class MissingScaffoldingException extends \Vegas\DI\Scaffolding\Exception
{
protected $message = 'Scaffolding has not been set';
}

39 changes: 0 additions & 39 deletions src/Mvc/Dispatcher/Events/BeforeException.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Mvc/ModuleAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Vegas\Mvc;

use Phalcon\DI\InjectionAwareInterface;
use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\ModuleDefinitionInterface;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected function registerPlugins($di)
$className = $plugin['class'];
$reflectionClass = new \ReflectionClass($className);
$dispatcherPlugin = $reflectionClass->newInstance();
if ($reflectionClass->hasMethod('setDI')) {
if ($dispatcherPlugin instanceof InjectionAwareInterface) {
$reflectionClass->getMethod('setDI')->invoke($dispatcherPlugin, $di);
}
$eventsManager->attach($plugin['attach'], $dispatcherPlugin);
Expand Down
108 changes: 108 additions & 0 deletions tests/DI/Scaffolding/Adapter/MysqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* This file is part of Vegas package
*
* @author Slawomir Zytko <slawek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Tests\DI\Scaffolding\Adapter;

use Phalcon\DI;
use Vegas\DI\Scaffolding;

class MysqlTest extends \Vegas\Test\TestCase
{
public static function setUpBeforeClass()
{
$di = DI::getDefault();
$di->get('db')->execute('DROP TABLE IF EXISTS fake ');
$di->get('db')->execute(
'CREATE TABLE fake(
id int not null primary key auto_increment,
fake_field varchar(250) null,
created_at int null
)'
);
}

public static function tearDownAfterClass()
{
$di = DI::getDefault();
$di->get('db')->execute('DROP TABLE IF EXISTS fake ');
}

public function tearDown()
{
foreach (\Test\Models\Fake::find() as $r) {
$r->delete();
}
}

public function testShouldThrowExceptionAboutMissingRequiredService()
{
$db = $this->getDI()->get('db');

$this->getDI()->remove('db');

$exception = null;
try {
new \Vegas\DI\Scaffolding\Adapter\Mysql();
} catch (\Exception $e) {
$exception = $e;
}
$this->assertInstanceOf('\Vegas\Db\Exception\NoRequiredServiceException', $exception);

$this->getDI()->set('db', $db);
}

public function testShouldThrowExceptionAboutMissingScaffolding()
{
$exception = null;
try {
$mysql = new \Vegas\DI\Scaffolding\Adapter\Mysql();
$mysql->retrieveOne(1);
} catch (\Exception $e) {
$exception = $e;
}
$this->assertInstanceOf('\Vegas\DI\Scaffolding\Exception\MissingScaffoldingException', $exception);
}

public function testShouldRetrieveRecordByItsId()
{
$mysql = new \Vegas\DI\Scaffolding\Adapter\Mysql();
$scaffolding = new Scaffolding($mysql);

$scaffolding->setModelName('\Test\Models\Fake');
$scaffolding->setFormName('\Test\Forms\Fake');
$created = $scaffolding->doCreate([
'fake_field' => 'fake'
]);
$this->assertTrue($created);

$this->assertInstanceOf('\Test\Models\Fake', $mysql->retrieveOne($scaffolding->getRecord()->getId()));
}

public function testShouldReturnValidPagination()
{
$mysql = new \Vegas\DI\Scaffolding\Adapter\Mysql();
$scaffolding = new Scaffolding($mysql);

$scaffolding->setModelName('\Test\Models\Fake');
$scaffolding->setFormName('\Test\Forms\Fake');
$scaffolding->doCreate([
'fake_field' => 'fake'
]);
$scaffolding->doCreate([
'fake_field' => 'fake2'
]);

$pagination = $mysql->getPaginator();
$this->assertInstanceOf('\Phalcon\Paginator\Adapter\Model', $pagination);
$this->assertInstanceOf('\stdClass', $pagination->getPaginate());
}
}
3 changes: 1 addition & 2 deletions tests/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'serviceDir' => APP_ROOT . '/app/services/',
'configDir' => APP_ROOT . '/app/config/',
'libraryDir' => dirname(APP_ROOT) . DIRECTORY_SEPARATOR . '/lib/',
'libraryDir' => APP_ROOT. '/lib/',
'pluginDir' => APP_ROOT . '/app/plugins/',
'moduleDir' => APP_ROOT . '/app/modules/',
'taskDir' => APP_ROOT . '/app/tasks/',
Expand Down Expand Up @@ -37,7 +37,6 @@
"dbname" => "vegas_test",
"port" => 3306,
"username" => "root",
'password'=> 'root',
"options" => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]
Expand Down

0 comments on commit fa8872d

Please sign in to comment.