From 26ded46f9cb8c52f9d5844665db0aaa3d14ced22 Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 15:05:45 +0100 Subject: [PATCH 01/35] crud autotemplating sample for testing purposes --- src/DI/Scaffolding.php | 12 +- src/Mvc/Controller/Crud.php | 351 ------------------ src/Mvc/Controller/Crud/Events.php | 12 +- src/Mvc/Controller/Crud/HooksTrait.php | 142 +++++++ src/Mvc/Controller/Crud/views/_form.volt | 23 ++ src/Mvc/Controller/Crud/views/edit.volt | 26 ++ src/Mvc/Controller/Crud/views/index.volt | 46 +++ src/Mvc/Controller/Crud/views/new.volt | 26 ++ src/Mvc/Controller/Crud/views/show.volt | 19 + src/Mvc/Controller/CrudAbstract.php | 292 +++++++++++++++ .../{Controller => }/ControllerAbstract.php | 3 +- src/Mvc/View.php | 23 ++ tests/DI/ScaffoldingTest.php | 2 +- tests/Mvc/Controller/CrudTest.php | 27 +- .../ControllerAbstractTest.php | 2 +- .../controllers/backend/FakeController.php | 3 +- .../controllers/backend/FakeController.php | 3 +- .../controllers/backend/CrudController.php | 20 +- .../controllers/backend/FakeController.php | 2 +- .../Foo/controllers/custom/FakeController.php | 3 +- .../controllers/frontend/FakeController.php | 3 +- .../app/modules/Test/config/routes.php | 10 + .../Test/controllers/FakeController.php | 2 +- .../backend/BrokenCrudController.php | 35 ++ .../controllers/backend/CrudController.php | 29 +- .../controllers/backend/FakeController.php | 2 +- .../controllers/custom/FakeController.php | 2 +- .../controllers/frontend/FakeController.php | 2 +- .../controllers/frontend/FooController.php | 2 +- .../modules/Test/views/backend/crud/new.volt | 1 - 30 files changed, 703 insertions(+), 422 deletions(-) delete mode 100644 src/Mvc/Controller/Crud.php create mode 100644 src/Mvc/Controller/Crud/HooksTrait.php create mode 100755 src/Mvc/Controller/Crud/views/_form.volt create mode 100755 src/Mvc/Controller/Crud/views/edit.volt create mode 100644 src/Mvc/Controller/Crud/views/index.volt create mode 100755 src/Mvc/Controller/Crud/views/new.volt create mode 100644 src/Mvc/Controller/Crud/views/show.volt create mode 100644 src/Mvc/Controller/CrudAbstract.php rename src/Mvc/{Controller => }/ControllerAbstract.php (98%) rename tests/Mvc/{Controller => }/ControllerAbstractTest.php (97%) create mode 100644 tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php delete mode 100644 tests/fixtures/app/modules/Test/views/backend/crud/new.volt diff --git a/src/DI/Scaffolding.php b/src/DI/Scaffolding.php index 95afc40..9496f21 100644 --- a/src/DI/Scaffolding.php +++ b/src/DI/Scaffolding.php @@ -169,7 +169,7 @@ public function doCreate(array $values) */ public function doUpdate($id, array $values) { - $this->record = $this->adapter->retrieveOne($id); + $this->record = $this->doRead($id); return $this->processForm($values); } @@ -178,8 +178,8 @@ public function doUpdate($id, array $values) */ public function doDelete($id) { - $this->record = $this->adapter->retrieveOne($id); - + $this->record = $this->doRead($id); + try { return $this->record->delete(); } catch (\Exception $e) { @@ -199,11 +199,11 @@ private function processForm($values) { $form = $this->getForm(); $form->bind($values, $this->record); - + if ($form->isValid()) { return $this->record->save(); - } - + } + $this->form = $form; throw new InvalidFormException(); } diff --git a/src/Mvc/Controller/Crud.php b/src/Mvc/Controller/Crud.php deleted file mode 100644 index 7826089..0000000 --- a/src/Mvc/Controller/Crud.php +++ /dev/null @@ -1,351 +0,0 @@ - - * class MyController extends Controller\Crud { - * protected $formName = 'My\Forms\My'; // default form used by CRUD - * protected $modelName = 'My\Models\My'; // default model used by CRUD - * - * public function initialize() - * { - * parent::initialize(); - * - * // we can also add this event in the Module.php to the dispatcher - * $this->dispatcher->getEventsManager()->attach( - * Controller\Crud\Events::AFTER_CREATE, function() { - * $this->response->redirect('user-admin/index'); - * } - * ); - * - * // attach more events - * } - * - * // instead using eventManager you can use method like - * protected function afterCreate() - * { - * parent::afterCreate(); - * $this->response->redirect('user-admin/index'); - * } - * - * // other actions - * } - * - * - * The list of fired events is located in \Vegas\Mvc\Controller\Crud\Events class. - * - * @author Arkadiusz Ostrycharz - * @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\Mvc\Controller; - -use Vegas\Exception; - -/** - * Class Crud - * @package Vegas\Mvc\Controller - */ -class Crud extends ControllerAbstract -{ - /** - * Default success message - * - * @var string - */ - protected $successMessage = 'Action has been successful.'; - - /** - * Form class name - * - * @var string - */ - protected $formName; - - /** - * Model class name - * - * @var string - */ - protected $modelName; - - /** - * Initializes scaffolding - * - * @throws Crud\Exception\NotConfiguredException - */ - public function initialize() - { - parent::initialize(); - - if (!$this->isConfigured()) { - throw new Crud\Exception\NotConfiguredException(); - } - - $this->scaffolding->setModelName($this->modelName); - $this->scaffolding->setFormName($this->formName); - } - - /** - * @return bool - * @internal - */ - private function isConfigured() - { - return ($this->di->has('scaffolding') && !empty($this->modelName) && !empty($this->formName)); - } - - /** - * Displays form for new record - * - * @ACL(name="new", description="Create a new record") - */ - public function newAction() - { - $this->beforeNew(); - - $this->view->form = $this->scaffolding->getForm(); - - $this->afterNew(); - } - - /** - * Creates new record - * - * @ACL(name="create", inherit='new') - */ - public function createAction() - { - $this->checkRequest(); - $this->beforeCreate(); - - try { - $this->beforeSave(); - - $this->scaffolding->doCreate($this->request->getPost()); - - $this->afterSave(); - - $this->view->disable(); - $this->flash->success($this->successMessage); - - $this->afterCreate(); - } catch (Exception $e) { - $this->flash->error($e->getMessage()); - - $this->afterCreateException(); - } - - $this->dispatcher->forward(array('action' => 'new')); - } - - /** - * Displays form for existing record - * - * @ACL(name="edit", description="Record edit") - * @param $id - */ - public function editAction($id) - { - $this->beforeEdit(); - - $this->view->record = $this->scaffolding->doRead($id); - - $this->afterRead(); - - $this->view->form = $this->scaffolding->getForm($this->view->record); - - $this->afterEdit(); - } - - /** - * Updates existing record indicated by its ID - * - * @ACL(name="update", inherit='edit') - * @param $id - */ - public function updateAction($id) - { - $this->checkRequest(); - $this->beforeUpdate(); - - try { - $this->beforeSave(); - - $this->scaffolding->doUpdate($id, $this->request->getPost()); - - $this->afterSave(); - - $this->view->disable(); - $this->flash->success($this->successMessage); - - $this->afterUpdate(); - } catch (Exception $e) { - $this->flash->error($e->getMessage()); - - $this->afterUpdateException(); - } - - $this->dispatcher->forward(array('action' => 'edit')); - } - - /** - * Checks if request was send using POST method - * - * @throws Crud\Exception\PostRequiredException - */ - protected function checkRequest() - { - if (!$this->request->isPost()) { - throw new Crud\Exception\PostRequiredException(); - } - } - - /** - * Deletes existing record by its ID - * - * @ACL(name="delete", description="Delete a record") - * @param $id - */ - public function deleteAction($id) - { - $this->view->disable(); - - $this->beforeDelete(); - - try { - $this->scaffolding->doDelete($id); - $this->flash->success($this->successMessage); - } catch (Exception $e) { - $this->flash->error($e->getMessage()); - } - - $this->afterDelete(); - } - - /** - * Method invoked on the beginning of the newAction. - */ - protected function beforeNew() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_NEW, $this); - } - - /** - * Method invoked on the end of the newAction. - */ - protected function afterNew() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_NEW, $this); - } - - /** - * Method invoked on the beginning of the createAction after checking request validity. - */ - protected function beforeCreate() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_CREATE, $this); - } - - /** - * Method invoked on the end of the successful createAction before dispatcher forward. - */ - protected function afterCreate() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_CREATE, $this); - } - - /** - * Method invoked on the end of the createAction failure before dispatcher forward. - */ - protected function afterCreateException() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_CREATE_EXCEPTION, $this); - } - - /** - * Method invoked on the beginning of the editAction. - */ - protected function beforeEdit() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_EDIT, $this); - } - - /** - * Method invoked after reading and setting $this->view->record variable but before creating $this->view->form. - */ - protected function afterRead() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_READ, $this); - } - - /** - * Method invoked on the end of the editAction. - */ - protected function afterEdit() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_EDIT, $this); - } - - /** - * Method invoked on the beginning of the updateAction after checking request validity. - */ - protected function beforeUpdate() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_UPDATE, $this); - } - - /** - * Method invoked on the end of the successful updateAction before dispatcher forward. - */ - protected function afterUpdate() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_UPDATE, $this); - } - - /** - * Method invoked on the end of the updateAction failure before dispatcher forward. - */ - protected function afterUpdateException() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_UPDATE_EXCEPTION, $this); - } - - /** - * Method invoked on the beginning of the deleteAction. - */ - protected function beforeDelete() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_DELETE, $this); - } - - /** - * Method invoked on the end of the deleteAction. - */ - protected function afterDelete() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_DELETE, $this); - } - - /** - * Method invoked just before doUpdate/doCreate method in createAction and updateAction after calling - * boforeCreate/beforeUpdate. - */ - protected function beforeSave() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::BEFORE_SAVE, $this); - } - - /** - * Method invoked just after doUpdate/doCreate method in createAction and updateAction before success or failure - * information. - */ - protected function afterSave() - { - $this->dispatcher->getEventsManager()->fire(Crud\Events::AFTER_SAVE, $this); - } -} \ No newline at end of file diff --git a/src/Mvc/Controller/Crud/Events.php b/src/Mvc/Controller/Crud/Events.php index 4bdb216..4c4b57e 100644 --- a/src/Mvc/Controller/Crud/Events.php +++ b/src/Mvc/Controller/Crud/Events.php @@ -19,6 +19,7 @@ */ class Events { + const BEFORE_READ = 'crud:beforeRead'; const AFTER_READ = 'crud:afterRead'; const BEFORE_NEW = 'crud:beforeNew'; @@ -27,17 +28,18 @@ class Events const BEFORE_CREATE = 'crud:beforeCreate'; const AFTER_CREATE = 'crud:afterCreate'; const AFTER_CREATE_EXCEPTION = 'crud:afterCreateException'; - + + const BEFORE_EDIT = 'crud:beforeEdit'; + const AFTER_EDIT = 'crud:afterEdit'; + const BEFORE_UPDATE = 'crud:beforeUpdate'; const AFTER_UPDATE = 'crud:afterUpdate'; const AFTER_UPDATE_EXCEPTION = 'crud:afterUpdateException'; const BEFORE_DELETE = 'crud:beforeDelete'; const AFTER_DELETE = 'crud:afterDelete'; - - const BEFORE_EDIT = 'crud:beforeEdit'; - const AFTER_EDIT = 'crud:afterEdit'; - + const AFTER_DELETE_EXCEPTION = 'crud:afterDeleteException'; + const BEFORE_UPLOAD = 'crud:beforeUpload'; const AFTER_UPLOAD = 'crud:afterUpload'; diff --git a/src/Mvc/Controller/Crud/HooksTrait.php b/src/Mvc/Controller/Crud/HooksTrait.php new file mode 100644 index 0000000..9ed9b07 --- /dev/null +++ b/src/Mvc/Controller/Crud/HooksTrait.php @@ -0,0 +1,142 @@ + + * @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\Mvc\Controller\Crud; + +trait HooksTrait +{ + /** + * Method invoked on the beginning of the newAction. + */ + protected function beforeNew() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_NEW, $this); + } + /** + * Method invoked on the end of the newAction. + */ + protected function afterNew() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_NEW, $this); + } + /** + * Method invoked on the beginning of the createAction after checking request validity. + */ + protected function beforeCreate() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_CREATE, $this); + $this->beforeSave(); + } + /** + * Method invoked on the end of the successful createAction before picking view. + */ + protected function afterCreate() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_CREATE, $this); + $this->afterSave(); + } + /** + * Method invoked on the end of the createAction failure before picking view. + */ + protected function afterCreateException() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_CREATE_EXCEPTION, $this); + } + /** + * Method invoked on the beginning of the editAction, after setting $this->view->record. + */ + protected function beforeEdit() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_EDIT, $this); + } + + /** + * Method invoked before reading and setting $this->view->record variable. + */ + protected function beforeRead() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_READ, $this); + } + + /** + * Method invoked after reading and setting $this->view->record variable but before creating $this->view->form. + */ + protected function afterRead() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_READ, $this); + } + /** + * Method invoked on the end of the editAction. + */ + protected function afterEdit() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_EDIT, $this); + } + /** + * Method invoked on the beginning of the updateAction after checking request validity. + */ + protected function beforeUpdate() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_UPDATE, $this); + $this->beforeSave(); + } + /** + * Method invoked on the end of the successful updateAction before picking view. + */ + protected function afterUpdate() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_UPDATE, $this); + $this->afterSave(); + } + /** + * Method invoked on the end of the updateAction failure before picking view. + */ + protected function afterUpdateException() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_UPDATE_EXCEPTION, $this); + } + /** + * Method invoked on the beginning of the deleteAction. + */ + protected function beforeDelete() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_DELETE, $this); + } + /** + * Method invoked on the end of the deleteAction. + */ + protected function afterDelete() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_DELETE, $this); + } + /** + * Method invoked on the end of the deleteAction failure before picking view. + */ + protected function afterDeleteException() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_DELETE_EXCEPTION, $this); + } + /** + * Method invoked just before doUpdate/doCreate method in createAction and updateAction after calling + * boforeCreate/beforeUpdate. + */ + protected function beforeSave() + { + $this->dispatcher->getEventsManager()->fire(Events::BEFORE_SAVE, $this); + } + /** + * Method invoked just after doUpdate/doCreate method in createAction and updateAction before success information. + */ + protected function afterSave() + { + $this->dispatcher->getEventsManager()->fire(Events::AFTER_SAVE, $this); + } +} \ No newline at end of file diff --git a/src/Mvc/Controller/Crud/views/_form.volt b/src/Mvc/Controller/Crud/views/_form.volt new file mode 100755 index 0000000..56f3115 --- /dev/null +++ b/src/Mvc/Controller/Crud/views/_form.volt @@ -0,0 +1,23 @@ +{% for element in form %} + {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} + {% set hasErrors = form.hasMessagesFor(element.getName()) %} + +
+ {% if element.getLabel() %} + + {% endif %} + {% if hasErrors %} + + {% for error in form.getMessagesFor(element.getName()) %} + {{ error }} + {% endfor %} + + {% endif %} + {{ element }} +
+{% endfor %} + +
+ + {{ i18n._('Cancel') }} +
\ No newline at end of file diff --git a/src/Mvc/Controller/Crud/views/edit.volt b/src/Mvc/Controller/Crud/views/edit.volt new file mode 100755 index 0000000..dc85276 --- /dev/null +++ b/src/Mvc/Controller/Crud/views/edit.volt @@ -0,0 +1,26 @@ +
+
+
+
+
+
+

{{ i18n._('Update record') }}

+
+
+
+
+
+
+
+
+
+
+ {{ partial('./_form', ['form': form]) }} +
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt new file mode 100644 index 0000000..c54a257 --- /dev/null +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -0,0 +1,46 @@ +
+
+
+

{{ i18n._('Records list') }}

+ +
+
+
+
+ + + + {% for field in fields %} + + {% endfor %} + + + + + {% if(page.items) %} + {% for item in page.items %} + + {% for key, field in fields %} + + {% endfor %} + + + {% endfor %} + {% else %} + + + + {% endif %} + +
{{ field }} 
{{ item.{key} }} + {{ i18n._("Update")}} + {{ i18n._("Delete")}} +
{{ i18n._("No records found.")}}
+ {{ pagination(page) }} +
+
+
diff --git a/src/Mvc/Controller/Crud/views/new.volt b/src/Mvc/Controller/Crud/views/new.volt new file mode 100755 index 0000000..819d946 --- /dev/null +++ b/src/Mvc/Controller/Crud/views/new.volt @@ -0,0 +1,26 @@ +
+
+
+
+
+
+

{{ i18n._('Add record') }}

+
+
+
+
+
+
+
+
+
+
+ {{ partial('./_form', ['form': form]) }} +
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/src/Mvc/Controller/Crud/views/show.volt b/src/Mvc/Controller/Crud/views/show.volt new file mode 100644 index 0000000..b3d252a --- /dev/null +++ b/src/Mvc/Controller/Crud/views/show.volt @@ -0,0 +1,19 @@ +
+
+
+

{{ i18n._('Record details') }}

+
+
+
+
+ + + {% for key, field in fields %} + + + {% endfor %} + +
{{ field }}{{ item.{key} }}
+
+
+
diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php new file mode 100644 index 0000000..6ca0588 --- /dev/null +++ b/src/Mvc/Controller/CrudAbstract.php @@ -0,0 +1,292 @@ + + * class MyController extends Controller\Crud { + * protected $formName = 'My\Forms\My'; // default form used by CRUD + * protected $modelName = 'My\Models\My'; // default model used by CRUD + * } + * + * + * @author Arkadiusz Ostrycharz + * @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\Mvc\Controller; + +use Vegas\Exception; +use Vegas\Mvc\Controller\Crud\HooksTrait; +use Vegas\Mvc\ControllerAbstract; +use Vegas\Mvc\View; + +/** + * Class Crud + * @package Vegas\Mvc\Controller + */ +abstract class CrudAbstract extends ControllerAbstract +{ + use HooksTrait; + + /** + * Default success message + * + * @var string + */ + protected $successMessage = 'Action has been successful.'; + + /** + * Form class name + * + * @var string + */ + protected $formName; + + /** + * Model class name + * + * @var string + */ + protected $modelName; + + /** + * Initializes scaffolding + * + * @throws Crud\Exception\NotConfiguredException + */ + private function initializeScaffolding() + { + if (!$this->isConfigured()) { + throw new Crud\Exception\NotConfiguredException(); + } + + $this->scaffolding->setModelName($this->modelName); + $this->scaffolding->setFormName($this->formName); + } + + /** + * @return bool + * @internal + */ + private function isConfigured() + { + return ($this->di->has('scaffolding') && !empty($this->modelName) && !empty($this->formName)); + } + + /** + * If user view did not exists, render default one from Crud/views dir. + * + * @return mixed + */ + private function checkForView() + { + var_dump($this->view->existsForControllerAndAction($this->view->exists($this->view->getControllerName(), $this->view->getActionName()))); + var_dump('kwiatek'); + die; + /*$view = $this->view; + + $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud', 'views', '']); + $view->setViewsDir($templatePath); + + die(var_dump('test', $view->getRender('', $this->view->getActionName())));*/ + +/* + var_dump($this->view->getViewsDir()); + + var_dump($this->view->getRender('', $this->router->getActionName(),$this->view->getParams(), function($view) { + $view->setRenderLevel(View::LEVEL_ACTION_VIEW); + }));*/ + + /*if (!$this->view->exists($this->router->getControllerName(), $this->router->getActionName())) { + $this->view->setViewsDir(__DIR__.DIRECTORY_SEPARATOR.'Crud'.DIRECTORY_SEPARATOR.'views'); + die(var_dump($this->view->getRender('', $this->router->getActionName(),$this->view->getParams()))); + }*/ + } + + /** + * Display records list. + * + * @ACL(name="new", description="Create a new record") + */ + public function indexAction() + { + $this->initializeScaffolding(); + } + + /** + * Display record details. + * + * @ACL(name="new", description="Create a new record") + */ + public function showAction($id) + { + $this->initializeScaffolding(); + + $this->beforeRead(); + $this->view->record = $this->scaffolding->doRead($id); + $this->afterRead(); + } + + /** + * Displays form for new record + * + * @ACL(name="new", description="Create a new record") + */ + final public function newAction() + { + $this->initializeScaffolding(); + + $this->beforeNew(); + $this->view->form = $this->scaffolding->getForm(); + $this->afterNew(); + + if (!$this->view->existsForCurrentAction()) { + $templatePath = realpath(implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud', 'views',''])); + + $view = $this->view; + $view->setViewsDir($templatePath); + + $content = $view->getRender( + '', + $this->router->getActionName(), + $view->getParams() + ); + + echo $content; + } + } + + /** + * Creates new record + * + * @ACL(name="create", inherit='new') + * @return mixed + */ + final public function createAction() + { + $this->initializeScaffolding(); + $this->checkRequest(); + + try { + $this->beforeCreate(); + $this->scaffolding->doCreate($this->request->getPost()); + $this->afterCreate(); + + $this->flash->success($this->successMessage); + return $this->redirectAfterSave(); + } catch (Exception $e) { + $this->afterCreateException(); + $this->flash->error($e->getMessage()); + } + + return $this->dispatcher->forward(['action' => 'new']); + } + + /** + * Displays form for existing record + * + * @ACL(name="edit", description="Record edit") + * @param $id + */ + final public function editAction($id) + { + $this->initializeScaffolding(); + + $this->beforeRead(); + $this->view->record = $this->scaffolding->doRead($id); + $this->afterRead(); + + $this->beforeEdit(); + $this->view->form = $this->scaffolding->getForm($this->view->record); + $this->afterEdit(); + } + + /** + * Updates existing record indicated by its ID + * + * @ACL(name="update", inherit='edit') + * @param $id + * @return mixed + */ + final public function updateAction($id) + { + $this->initializeScaffolding(); + $this->checkRequest(); + + try { + $this->beforeRead(); + $this->view->record = $this->scaffolding->doRead($id); + $this->afterRead(); + + $this->beforeUpdate(); + $this->scaffolding->doUpdate($id, $this->request->getPost()); + $this->afterUpdate(); + + $this->flash->success($this->successMessage); + return $this->redirectAfterSave(); + } catch (Exception $e) { + $this->afterUpdateException(); + $this->flash->error($e->getMessage()); + } + + return $this->dispatcher->forward(['action' => 'edit']); + } + + /** + * Checks if request was send using POST method + * + * @throws Crud\Exception\PostRequiredException + */ + protected function checkRequest() + { + if (!$this->request->isPost()) { + throw new Crud\Exception\PostRequiredException(); + } + } + + /** + * Deletes existing record by its ID + * + * @ACL(name="delete", description="Delete a record") + * @param $id + * @return mixed + */ + final public function deleteAction($id) + { + $this->initializeScaffolding(); + + try { + $this->beforeRead(); + $this->view->record = $this->scaffolding->doRead($id); + $this->afterRead(); + + $this->beforeDelete(); + $this->scaffolding->doDelete($id); + $this->afterDelete(); + + $this->flash->success($this->successMessage); + } catch (Exception $e) { + $this->flash->error($e->getMessage()); + } + + return $this->redirectAfterDelete(); + } + + /** + * Method called after successful update or create. + * + * @return mixed + */ + abstract protected function redirectAfterSave(); + + /** + * Method called after delete. + * + * @return mixed + */ + abstract protected function redirectAfterDelete(); +} \ No newline at end of file diff --git a/src/Mvc/Controller/ControllerAbstract.php b/src/Mvc/ControllerAbstract.php similarity index 98% rename from src/Mvc/Controller/ControllerAbstract.php rename to src/Mvc/ControllerAbstract.php index 92bbc04..7590173 100644 --- a/src/Mvc/Controller/ControllerAbstract.php +++ b/src/Mvc/ControllerAbstract.php @@ -9,12 +9,13 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace Vegas\Mvc\Controller; +namespace Vegas\Mvc; use Phalcon\Mvc\Controller; /** * Class ControllerAbstract + * * @package Vegas\Mvc\Controller */ abstract class ControllerAbstract extends Controller diff --git a/src/Mvc/View.php b/src/Mvc/View.php index 974f80d..95e2bb8 100644 --- a/src/Mvc/View.php +++ b/src/Mvc/View.php @@ -337,6 +337,7 @@ public function render($controllerName, $actionName, $params = null) { if (empty($this->controllerViewPath)) { $this->setControllerViewPath($controllerName); } + parent::render($this->controllerViewPath, $actionName, $params); } @@ -362,4 +363,26 @@ public function setControllerViewPath($controllerName) $this->controllerViewPath = str_replace('\\','/',strtolower($controllerName)); $this->controllerFullViewPath = $this->getViewsDir() . $this->controllerViewPath; } + + /** + * Check if view for current pair of controller/action exists. + * + * @return bool + */ + public function existsForCurrentAction() + { + $actionName = $this->getDI()->get('router')->getActionName(); + + if (empty($this->controllerViewPath)) { + $this->setControllerViewPath($this->getDI()->get('router')->getControllerName()); + } + + foreach ($this->getRegisteredEngines() As $ext => $engine) { + if (file_exists($this->controllerFullViewPath.DIRECTORY_SEPARATOR.$actionName.$ext)) { + return true; + } + } + + return false; + } } diff --git a/tests/DI/ScaffoldingTest.php b/tests/DI/ScaffoldingTest.php index 6cd5aa3..c98613b 100644 --- a/tests/DI/ScaffoldingTest.php +++ b/tests/DI/ScaffoldingTest.php @@ -95,7 +95,7 @@ public function testDoCreate() public function testDoUpdate() { $values = array('fake_field' => 'testtest'); - + $this->scaffolding->doUpdate($this->record->getId(), $values); $updatedRecordId = $this->scaffolding->getRecord()->getId(); diff --git a/tests/Mvc/Controller/CrudTest.php b/tests/Mvc/Controller/CrudTest.php index 0b481bb..b4d0be2 100644 --- a/tests/Mvc/Controller/CrudTest.php +++ b/tests/Mvc/Controller/CrudTest.php @@ -30,14 +30,11 @@ public function setUp() public function testNotConfiguredCrud() { - $crud = new Crud(); + $_SERVER['REQUEST_METHOD'] = 'GET'; - try { - $crud->initialize(); - throw new \Exception('Not this exception.'); - } catch (\Exception $ex) { - $this->assertInstanceOf('\Vegas\Mvc\Controller\Crud\Exception\NotConfiguredException', $ex); - } + $content = $this->bootstrap->run('/test/brokencrud/new'); + $this->assertContains('500', $content); + $this->assertContains('CRUD is not configured.', $content); } public function testNew() @@ -73,13 +70,13 @@ public function testPostCreate() public function testPostCreateResponse() { - $contentArray = explode('::', $this->di->get('response')->getContent()); + $contentArray = json_decode($this->di->get('response')->getContent(), true); - $model = FakeModel::findById($contentArray[0]); + $model = FakeModel::findById($contentArray['$id']); $this->assertInstanceOf('\Test\Models\Fake', $model); $this->assertEquals(base64_encode(date('Y-m-d')), $model->fake_field); - $this->assertEquals('afterCreate method call', $contentArray[1]); + $this->assertEquals('afterCreate added content', $model->after_create_content); $model->delete(); } @@ -119,14 +116,14 @@ public function testPostUpdate() $_POST['fake_field'] = base64_encode('foobar'); $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); - $this->assertNotEmpty($content); + $this->assertEquals(json_encode($this->model->getId()), $content); } public function testPostUpdateResponse() { - $id = $this->di->get('response')->getContent(); + $contentArray = json_decode($this->di->get('response')->getContent(), true); - $model = FakeModel::findById($id); + $model = FakeModel::findById($contentArray['$id']); $this->assertInstanceOf('\Test\Models\Fake', $model); $this->assertEquals(base64_encode('foobar'), $model->fake_field); @@ -142,9 +139,7 @@ public function testDelete() $this->bootstrap->run('/test/crud/delete/'.$this->model->getId()); - $this->model = FakeModel::findFirst(array(array( - 'fake_field' => base64_encode('foobar') - ))); + $this->model = FakeModel::findById($this->model->getId()); $this->assertFalse($this->model); } diff --git a/tests/Mvc/Controller/ControllerAbstractTest.php b/tests/Mvc/ControllerAbstractTest.php similarity index 97% rename from tests/Mvc/Controller/ControllerAbstractTest.php rename to tests/Mvc/ControllerAbstractTest.php index a0618c9..8e3a8fb 100644 --- a/tests/Mvc/Controller/ControllerAbstractTest.php +++ b/tests/Mvc/ControllerAbstractTest.php @@ -9,7 +9,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace Vegas\Tests\Mvc\Controller; +namespace Vegas\Tests\Mvc; use Vegas\Tests\App\TestCase; diff --git a/tests/fixtures/app/modules/Disabled/controllers/backend/FakeController.php b/tests/fixtures/app/modules/Disabled/controllers/backend/FakeController.php index 8defa6a..e90eaf8 100644 --- a/tests/fixtures/app/modules/Disabled/controllers/backend/FakeController.php +++ b/tests/fixtures/app/modules/Disabled/controllers/backend/FakeController.php @@ -12,8 +12,7 @@ namespace Disabled\Controllers\Backend; - -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; class FakeController extends ControllerAbstract { diff --git a/tests/fixtures/app/modules/Example/controllers/backend/FakeController.php b/tests/fixtures/app/modules/Example/controllers/backend/FakeController.php index 2c5501a..be30608 100644 --- a/tests/fixtures/app/modules/Example/controllers/backend/FakeController.php +++ b/tests/fixtures/app/modules/Example/controllers/backend/FakeController.php @@ -12,8 +12,7 @@ namespace Example\Controllers\Backend; - -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; class FakeController extends ControllerAbstract { diff --git a/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php b/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php index 889efd7..b6d49f8 100644 --- a/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php +++ b/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php @@ -12,26 +12,22 @@ namespace Foo\Controllers\Backend; -use Vegas\Mvc\Controller\Crud; +use Vegas\Mvc\Controller\CrudAbstract; -class CrudController extends Crud +class CrudController extends CrudAbstract { protected $formName = 'Test\Forms\Fake'; protected $modelName = 'Test\Models\Fake'; - public function initialize() + protected function redirectAfterSave() { - parent::initialize(); - - $this->dispatcher->getEventsManager()->attach(Crud\Events::AFTER_CREATE, $this->printAfterSuccess()); - $this->dispatcher->getEventsManager()->attach(Crud\Events::AFTER_UPDATE, $this->printAfterSuccess()); - } - - private function printAfterSuccess() - { - // for testing purposes only return function() { echo $this->scaffolding->getRecord()->getId(); }; } + + protected function redirectAfterDelete() + { + return $this->redirectAfterSave(); + } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Foo/controllers/backend/FakeController.php b/tests/fixtures/app/modules/Foo/controllers/backend/FakeController.php index 727b05e..70c82d5 100644 --- a/tests/fixtures/app/modules/Foo/controllers/backend/FakeController.php +++ b/tests/fixtures/app/modules/Foo/controllers/backend/FakeController.php @@ -12,7 +12,7 @@ namespace Foo\Controllers\Backend; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; class FakeController extends ControllerAbstract { diff --git a/tests/fixtures/app/modules/Foo/controllers/custom/FakeController.php b/tests/fixtures/app/modules/Foo/controllers/custom/FakeController.php index 1842101..71721c2 100644 --- a/tests/fixtures/app/modules/Foo/controllers/custom/FakeController.php +++ b/tests/fixtures/app/modules/Foo/controllers/custom/FakeController.php @@ -12,8 +12,7 @@ namespace Foo\Controllers\Custom; - -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; class FakeController extends ControllerAbstract { diff --git a/tests/fixtures/app/modules/Foo/controllers/frontend/FakeController.php b/tests/fixtures/app/modules/Foo/controllers/frontend/FakeController.php index de0ae92..18f25a1 100644 --- a/tests/fixtures/app/modules/Foo/controllers/frontend/FakeController.php +++ b/tests/fixtures/app/modules/Foo/controllers/frontend/FakeController.php @@ -12,8 +12,7 @@ namespace Foo\Controllers\Frontend; - -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; /** * Class FakeController diff --git a/tests/fixtures/app/modules/Test/config/routes.php b/tests/fixtures/app/modules/Test/config/routes.php index ff71d8b..de9c739 100644 --- a/tests/fixtures/app/modules/Test/config/routes.php +++ b/tests/fixtures/app/modules/Test/config/routes.php @@ -33,6 +33,16 @@ ) ), + 'testbrokencrud' => array( + 'route' => '/test/brokencrud/:action/:params', + 'paths' => array( + 'module' => 'Test', + 'controller' => 'Backend\BrokenCrud', + 'action' => 1, + 'params' => 2 + ) + ), + 'testcrud' => array( 'route' => '/test/crud/:action/:params', 'paths' => array( diff --git a/tests/fixtures/app/modules/Test/controllers/FakeController.php b/tests/fixtures/app/modules/Test/controllers/FakeController.php index 649616b..78f5bd4 100644 --- a/tests/fixtures/app/modules/Test/controllers/FakeController.php +++ b/tests/fixtures/app/modules/Test/controllers/FakeController.php @@ -12,7 +12,7 @@ namespace Test\Controllers; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; use Vegas\Mvc\View; class FakeController extends ControllerAbstract diff --git a/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php new file mode 100644 index 0000000..727a1dc --- /dev/null +++ b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php @@ -0,0 +1,35 @@ + + * @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 Test\Controllers\Backend; + +use Vegas\Mvc\Controller\CrudAbstract; +use Vegas\Mvc\View; + +class BrokenCrudController extends CrudAbstract +{ + public function initialize() + { + parent::initialize(); + $this->view->disableLevel(View::LEVEL_LAYOUT); + } + + protected function redirectAfterSave() + { + return $this->scaffolding->getRecord()->getId(); + } + + protected function redirectAfterDelete() + { + return $this->redirectAfterSave(); + } +} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php index d3567db..f3324f5 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php @@ -12,10 +12,10 @@ namespace Test\Controllers\Backend; -use Vegas\Mvc\Controller\Crud; +use Vegas\Mvc\Controller\CrudAbstract; use Vegas\Mvc\View; -class CrudController extends Crud +class CrudController extends CrudAbstract { protected $formName = 'Test\Forms\Fake'; protected $modelName = 'Test\Models\Fake'; @@ -23,24 +23,25 @@ class CrudController extends Crud public function initialize() { parent::initialize(); - $this->view->disableLevel(View::LEVEL_LAYOUT); + } + + protected function afterCreate() + { + parent::afterCreate(); - $this->dispatcher->getEventsManager()->attach(Crud\Events::AFTER_CREATE, $this->printAfterSuccess()); - $this->dispatcher->getEventsManager()->attach(Crud\Events::AFTER_UPDATE, $this->printAfterSuccess()); + $record = $this->scaffolding->getRecord(); + $record->after_create_content = 'afterCreate added content'; + $record->save(); } - private function printAfterSuccess() + protected function redirectAfterSave() { - // for testing purposes only - return function() { - echo $this->scaffolding->getRecord()->getId(); - }; + return $this->jsonResponse($this->scaffolding->getRecord()->getId()); } - protected function afterCreate() + protected function redirectAfterDelete() { - parent::afterCreate(); - echo '::afterCreate method call'; + return $this->jsonResponse(); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/backend/FakeController.php b/tests/fixtures/app/modules/Test/controllers/backend/FakeController.php index 5165fb5..e66b39e 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/FakeController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/FakeController.php @@ -12,7 +12,7 @@ namespace Test\Controllers\Backend; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; use Vegas\Mvc\View; class FakeController extends ControllerAbstract diff --git a/tests/fixtures/app/modules/Test/controllers/custom/FakeController.php b/tests/fixtures/app/modules/Test/controllers/custom/FakeController.php index 682724b..216458a 100644 --- a/tests/fixtures/app/modules/Test/controllers/custom/FakeController.php +++ b/tests/fixtures/app/modules/Test/controllers/custom/FakeController.php @@ -13,7 +13,7 @@ namespace Test\Controllers\Custom; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; class FakeController extends ControllerAbstract { diff --git a/tests/fixtures/app/modules/Test/controllers/frontend/FakeController.php b/tests/fixtures/app/modules/Test/controllers/frontend/FakeController.php index 311375d..fc55030 100644 --- a/tests/fixtures/app/modules/Test/controllers/frontend/FakeController.php +++ b/tests/fixtures/app/modules/Test/controllers/frontend/FakeController.php @@ -13,7 +13,7 @@ namespace Test\Controllers\Frontend; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; use Vegas\Mvc\View; /** diff --git a/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php b/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php index fd3dd79..35e038f 100644 --- a/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php +++ b/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php @@ -13,7 +13,7 @@ namespace Test\Controllers\Frontend; -use Vegas\Mvc\Controller\ControllerAbstract; +use Vegas\Mvc\ControllerAbstract; use Vegas\Mvc\View; /** diff --git a/tests/fixtures/app/modules/Test/views/backend/crud/new.volt b/tests/fixtures/app/modules/Test/views/backend/crud/new.volt deleted file mode 100644 index 79e0c11..0000000 --- a/tests/fixtures/app/modules/Test/views/backend/crud/new.volt +++ /dev/null @@ -1 +0,0 @@ -{{ form.get('fake_field') }} \ No newline at end of file From 7c1328d3c979fef1ee54b07409822b077cffd9d9 Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 15:45:05 +0100 Subject: [PATCH 02/35] CRUD methods - removed final, added auto-redirect --- src/Mvc/Controller/Crud/HooksTrait.php | 22 +++++- src/Mvc/Controller/CrudAbstract.php | 78 +++---------------- .../controllers/backend/CrudController.php | 12 --- .../backend/BrokenCrudController.php | 10 --- .../controllers/backend/CrudController.php | 8 +- 5 files changed, 36 insertions(+), 94 deletions(-) diff --git a/src/Mvc/Controller/Crud/HooksTrait.php b/src/Mvc/Controller/Crud/HooksTrait.php index 9ed9b07..f0c4511 100644 --- a/src/Mvc/Controller/Crud/HooksTrait.php +++ b/src/Mvc/Controller/Crud/HooksTrait.php @@ -13,6 +13,19 @@ trait HooksTrait { + /** + * Redirect to specific action in the same controller. + * + * @param $action + * @return mixed + */ + protected function redirectToAction($action) + { + return $this->response->redirect([ + 'for' => $this->router->getMatchedRoute(), + 'action' => $action + ]); + } /** * Method invoked on the beginning of the newAction. */ @@ -41,7 +54,7 @@ protected function beforeCreate() protected function afterCreate() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_CREATE, $this); - $this->afterSave(); + return $this->afterSave(); } /** * Method invoked on the end of the createAction failure before picking view. @@ -49,6 +62,7 @@ protected function afterCreate() protected function afterCreateException() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_CREATE_EXCEPTION, $this); + return $this->redirectToAction('index'); } /** * Method invoked on the beginning of the editAction, after setting $this->view->record. @@ -94,7 +108,7 @@ protected function beforeUpdate() protected function afterUpdate() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_UPDATE, $this); - $this->afterSave(); + return $this->afterSave(); } /** * Method invoked on the end of the updateAction failure before picking view. @@ -102,6 +116,7 @@ protected function afterUpdate() protected function afterUpdateException() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_UPDATE_EXCEPTION, $this); + return $this->redirectToAction('index'); } /** * Method invoked on the beginning of the deleteAction. @@ -116,6 +131,7 @@ protected function beforeDelete() protected function afterDelete() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_DELETE, $this); + return $this->redirectToAction('index'); } /** * Method invoked on the end of the deleteAction failure before picking view. @@ -123,6 +139,7 @@ protected function afterDelete() protected function afterDeleteException() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_DELETE_EXCEPTION, $this); + return $this->redirectToAction('index'); } /** * Method invoked just before doUpdate/doCreate method in createAction and updateAction after calling @@ -138,5 +155,6 @@ protected function beforeSave() protected function afterSave() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_SAVE, $this); + return $this->redirectToAction('index'); } } \ No newline at end of file diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 6ca0588..2e19227 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -77,36 +77,6 @@ private function isConfigured() return ($this->di->has('scaffolding') && !empty($this->modelName) && !empty($this->formName)); } - /** - * If user view did not exists, render default one from Crud/views dir. - * - * @return mixed - */ - private function checkForView() - { - var_dump($this->view->existsForControllerAndAction($this->view->exists($this->view->getControllerName(), $this->view->getActionName()))); - var_dump('kwiatek'); - die; - /*$view = $this->view; - - $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud', 'views', '']); - $view->setViewsDir($templatePath); - - die(var_dump('test', $view->getRender('', $this->view->getActionName())));*/ - -/* - var_dump($this->view->getViewsDir()); - - var_dump($this->view->getRender('', $this->router->getActionName(),$this->view->getParams(), function($view) { - $view->setRenderLevel(View::LEVEL_ACTION_VIEW); - }));*/ - - /*if (!$this->view->exists($this->router->getControllerName(), $this->router->getActionName())) { - $this->view->setViewsDir(__DIR__.DIRECTORY_SEPARATOR.'Crud'.DIRECTORY_SEPARATOR.'views'); - die(var_dump($this->view->getRender('', $this->router->getActionName(),$this->view->getParams()))); - }*/ - } - /** * Display records list. * @@ -136,7 +106,7 @@ public function showAction($id) * * @ACL(name="new", description="Create a new record") */ - final public function newAction() + public function newAction() { $this->initializeScaffolding(); @@ -148,7 +118,7 @@ final public function newAction() $templatePath = realpath(implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud', 'views',''])); $view = $this->view; - $view->setViewsDir($templatePath); + //$view->setViewsDir($templatePath); $content = $view->getRender( '', @@ -166,7 +136,7 @@ final public function newAction() * @ACL(name="create", inherit='new') * @return mixed */ - final public function createAction() + public function createAction() { $this->initializeScaffolding(); $this->checkRequest(); @@ -174,16 +144,12 @@ final public function createAction() try { $this->beforeCreate(); $this->scaffolding->doCreate($this->request->getPost()); - $this->afterCreate(); - $this->flash->success($this->successMessage); - return $this->redirectAfterSave(); + return $this->afterCreate(); } catch (Exception $e) { - $this->afterCreateException(); $this->flash->error($e->getMessage()); + return $this->afterCreateException(); } - - return $this->dispatcher->forward(['action' => 'new']); } /** @@ -192,7 +158,7 @@ final public function createAction() * @ACL(name="edit", description="Record edit") * @param $id */ - final public function editAction($id) + public function editAction($id) { $this->initializeScaffolding(); @@ -212,7 +178,7 @@ final public function editAction($id) * @param $id * @return mixed */ - final public function updateAction($id) + public function updateAction($id) { $this->initializeScaffolding(); $this->checkRequest(); @@ -224,16 +190,12 @@ final public function updateAction($id) $this->beforeUpdate(); $this->scaffolding->doUpdate($id, $this->request->getPost()); - $this->afterUpdate(); - $this->flash->success($this->successMessage); - return $this->redirectAfterSave(); + return $this->afterUpdate(); } catch (Exception $e) { - $this->afterUpdateException(); $this->flash->error($e->getMessage()); + return $this->afterUpdateException(); } - - return $this->dispatcher->forward(['action' => 'edit']); } /** @@ -255,7 +217,7 @@ protected function checkRequest() * @param $id * @return mixed */ - final public function deleteAction($id) + public function deleteAction($id) { $this->initializeScaffolding(); @@ -266,27 +228,11 @@ final public function deleteAction($id) $this->beforeDelete(); $this->scaffolding->doDelete($id); - $this->afterDelete(); - $this->flash->success($this->successMessage); + return $this->afterDelete(); } catch (Exception $e) { $this->flash->error($e->getMessage()); + return $this->afterDeleteException(); } - - return $this->redirectAfterDelete(); } - - /** - * Method called after successful update or create. - * - * @return mixed - */ - abstract protected function redirectAfterSave(); - - /** - * Method called after delete. - * - * @return mixed - */ - abstract protected function redirectAfterDelete(); } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php b/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php index b6d49f8..ffbd0f8 100644 --- a/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php +++ b/tests/fixtures/app/modules/Foo/controllers/backend/CrudController.php @@ -18,16 +18,4 @@ class CrudController extends CrudAbstract { protected $formName = 'Test\Forms\Fake'; protected $modelName = 'Test\Models\Fake'; - - protected function redirectAfterSave() - { - return function() { - echo $this->scaffolding->getRecord()->getId(); - }; - } - - protected function redirectAfterDelete() - { - return $this->redirectAfterSave(); - } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php index 727a1dc..1704bbf 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php @@ -22,14 +22,4 @@ public function initialize() parent::initialize(); $this->view->disableLevel(View::LEVEL_LAYOUT); } - - protected function redirectAfterSave() - { - return $this->scaffolding->getRecord()->getId(); - } - - protected function redirectAfterDelete() - { - return $this->redirectAfterSave(); - } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php index f3324f5..99aca60 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php @@ -28,19 +28,19 @@ public function initialize() protected function afterCreate() { - parent::afterCreate(); - $record = $this->scaffolding->getRecord(); $record->after_create_content = 'afterCreate added content'; $record->save(); + + return parent::afterCreate(); } - protected function redirectAfterSave() + protected function afterSave() { return $this->jsonResponse($this->scaffolding->getRecord()->getId()); } - protected function redirectAfterDelete() + protected function afterDelete() { return $this->jsonResponse(); } From 3e8cb9616803f1cee8191b2fdae0e7f5d13b779e Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 17:50:40 +0100 Subject: [PATCH 03/35] fine tuning of crud --- src/Mvc/Controller/Crud/HooksTrait.php | 2 +- src/Mvc/Controller/Crud/views/edit.volt | 2 +- src/Mvc/Controller/Crud/views/new.volt | 2 +- src/Mvc/Controller/CrudAbstract.php | 38 ++++++++++++------------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Mvc/Controller/Crud/HooksTrait.php b/src/Mvc/Controller/Crud/HooksTrait.php index f0c4511..ec8a3d0 100644 --- a/src/Mvc/Controller/Crud/HooksTrait.php +++ b/src/Mvc/Controller/Crud/HooksTrait.php @@ -22,7 +22,7 @@ trait HooksTrait protected function redirectToAction($action) { return $this->response->redirect([ - 'for' => $this->router->getMatchedRoute(), + 'for' => $this->router->getMatchedRoute()->getName(), 'action' => $action ]); } diff --git a/src/Mvc/Controller/Crud/views/edit.volt b/src/Mvc/Controller/Crud/views/edit.volt index dc85276..fa19b15 100755 --- a/src/Mvc/Controller/Crud/views/edit.volt +++ b/src/Mvc/Controller/Crud/views/edit.volt @@ -15,7 +15,7 @@
- {{ partial('./_form', ['form': form]) }} + {{ partial('./views/_form', ['form': form]) }}
diff --git a/src/Mvc/Controller/Crud/views/new.volt b/src/Mvc/Controller/Crud/views/new.volt index 819d946..a9be00f 100755 --- a/src/Mvc/Controller/Crud/views/new.volt +++ b/src/Mvc/Controller/Crud/views/new.volt @@ -15,7 +15,7 @@
- {{ partial('./_form', ['form': form]) }} + {{ partial('./views/_form', ['form': form]) }}
diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 2e19227..9513d84 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -1,15 +1,15 @@ - * class MyController extends Controller\Crud { + * class MyController extends Controller\Crud { * protected $formName = 'My\Forms\My'; // default form used by CRUD * protected $modelName = 'My\Models\My'; // default model used by CRUD * } * - * + * * @author Arkadiusz Ostrycharz * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io @@ -30,6 +30,21 @@ */ abstract class CrudAbstract extends ControllerAbstract { + /** + * Controller initialization block + */ + public function initialize() + { + parent::initialize(); + + if ($this->view instanceof \Vegas\Mvc\View && !$this->view->existsForCurrentAction()) { + $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud','']); + + $this->view->setViewsDir($templatePath); + $this->view->setControllerViewPath('views'); + } + } + use HooksTrait; /** @@ -74,7 +89,7 @@ private function initializeScaffolding() */ private function isConfigured() { - return ($this->di->has('scaffolding') && !empty($this->modelName) && !empty($this->formName)); + return ($this->di->has('scaffolding') && !empty($this->modelName) && !empty($this->formName)); } /** @@ -113,21 +128,6 @@ public function newAction() $this->beforeNew(); $this->view->form = $this->scaffolding->getForm(); $this->afterNew(); - - if (!$this->view->existsForCurrentAction()) { - $templatePath = realpath(implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud', 'views',''])); - - $view = $this->view; - //$view->setViewsDir($templatePath); - - $content = $view->getRender( - '', - $this->router->getActionName(), - $view->getParams() - ); - - echo $content; - } } /** From 3a47c7bd3b13a600759f338f01c321a8ec055307 Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 17:53:56 +0100 Subject: [PATCH 04/35] fine tuning of crud --- src/Mvc/Controller/Crud/HooksTrait.php | 2 -- src/Mvc/Controller/CrudAbstract.php | 8 ++++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Mvc/Controller/Crud/HooksTrait.php b/src/Mvc/Controller/Crud/HooksTrait.php index ec8a3d0..50d1b75 100644 --- a/src/Mvc/Controller/Crud/HooksTrait.php +++ b/src/Mvc/Controller/Crud/HooksTrait.php @@ -62,7 +62,6 @@ protected function afterCreate() protected function afterCreateException() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_CREATE_EXCEPTION, $this); - return $this->redirectToAction('index'); } /** * Method invoked on the beginning of the editAction, after setting $this->view->record. @@ -116,7 +115,6 @@ protected function afterUpdate() protected function afterUpdateException() { $this->dispatcher->getEventsManager()->fire(Events::AFTER_UPDATE_EXCEPTION, $this); - return $this->redirectToAction('index'); } /** * Method invoked on the beginning of the deleteAction. diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 9513d84..0073608 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -148,8 +148,10 @@ public function createAction() return $this->afterCreate(); } catch (Exception $e) { $this->flash->error($e->getMessage()); - return $this->afterCreateException(); + $this->afterCreateException(); } + + $this->view->pick(['new']); } /** @@ -194,8 +196,10 @@ public function updateAction($id) return $this->afterUpdate(); } catch (Exception $e) { $this->flash->error($e->getMessage()); - return $this->afterUpdateException(); + $this->afterUpdateException(); } + + $this->view->pick(['edit']); } /** From 4424c08f88acea51a689b85a3a9b53538038adfa Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 18:00:16 +0100 Subject: [PATCH 05/35] CRUD forwarding instead picking views --- src/Mvc/Controller/CrudAbstract.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 0073608..4006886 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -151,7 +151,7 @@ public function createAction() $this->afterCreateException(); } - $this->view->pick(['new']); + return $this->dispatcher->forward(['action' => 'new']); } /** @@ -199,7 +199,7 @@ public function updateAction($id) $this->afterUpdateException(); } - $this->view->pick(['edit']); + return $this->dispatcher->forward(['action' => 'edit']); } /** From 9e1826f4ac28e0e32fc95761bb3bcb57267f52b0 Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 27 Nov 2014 18:10:30 +0100 Subject: [PATCH 06/35] View in local variable to prevent seg fault with too much DI references info. --- src/Mvc/Controller/CrudAbstract.php | 7 +++++-- .../fixtures/app/modules/Test/views/backend/crud/new.volt | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/app/modules/Test/views/backend/crud/new.volt diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 4006886..e0f9b1e 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -40,8 +40,11 @@ public function initialize() if ($this->view instanceof \Vegas\Mvc\View && !$this->view->existsForCurrentAction()) { $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud','']); - $this->view->setViewsDir($templatePath); - $this->view->setControllerViewPath('views'); + $view = $this->getDI()->get('view'); + $view->setViewsDir($templatePath); + $view->setControllerViewPath('views'); + + $this->view = $view; } } diff --git a/tests/fixtures/app/modules/Test/views/backend/crud/new.volt b/tests/fixtures/app/modules/Test/views/backend/crud/new.volt new file mode 100644 index 0000000..79e0c11 --- /dev/null +++ b/tests/fixtures/app/modules/Test/views/backend/crud/new.volt @@ -0,0 +1 @@ +{{ form.get('fake_field') }} \ No newline at end of file From aecce20bd3d54458a1ed51dd5662576c6741347e Mon Sep 17 00:00:00 2001 From: Arek Date: Mon, 1 Dec 2014 17:05:04 +0100 Subject: [PATCH 07/35] CRUD finished (tests yet to come) --- src/DI/Scaffolding.php | 30 ++++----- src/DI/Scaffolding/Adapter/Mongo.php | 19 ++++-- src/DI/Scaffolding/Adapter/Mysql.php | 18 +++++- src/DI/Scaffolding/AdapterInterface.php | 9 +++ src/DI/ScaffoldingInterface.php | 17 +++++ src/Mvc/Controller/Crud/views/_form.volt | 23 ------- src/Mvc/Controller/Crud/views/edit.volt | 24 ++++++- src/Mvc/Controller/Crud/views/index.volt | 16 +++-- src/Mvc/Controller/Crud/views/new.volt | 24 ++++++- src/Mvc/Controller/Crud/views/show.volt | 2 +- src/Mvc/Controller/CrudAbstract.php | 30 ++++++--- src/Mvc/View.php | 64 ++++++------------- tests/Mvc/Controller/CrudTest.php | 4 +- .../backend/BrokenCrudController.php | 2 +- 14 files changed, 170 insertions(+), 112 deletions(-) delete mode 100755 src/Mvc/Controller/Crud/views/_form.volt diff --git a/src/DI/Scaffolding.php b/src/DI/Scaffolding.php index 9496f21..a1dd227 100644 --- a/src/DI/Scaffolding.php +++ b/src/DI/Scaffolding.php @@ -22,19 +22,10 @@ * class MyController extends Controller\Crud { * protected $formName = 'My\Forms\My'; // default form used by CRUD * protected $modelName = 'My\Models\My'; // default model used by CRUD - - * public function initialize() - * { - * parent::initialize(); - * // we can also add this event in the Module.php to the dispatcher - * $this->dispatcher->getEventsManager()->attach( - * Controller\Crud\Events::AFTER_CREATE, function() { - * $this->response->redirect('user-admin/index'); - * } - * ); - * // attach more events - * } - * // other actions + * protected $fields = [ + * 'name' => 'Name', + * 'url' => 'Url' + * ]; // default field set for index and show actions (all fields will be echoed via readMapped() method) * } * * @@ -144,10 +135,7 @@ public function setModelName($name) } /** - * Retrieves record by its ID - * - * @param $id - * @return mixed + * {@inheritdoc} */ public function doRead($id) { @@ -155,6 +143,14 @@ public function doRead($id) return $this->record; } + /** + * {@inheritdoc} + */ + public function doPaginate($page = 1, $limit = 10) + { + return $this->adapter->getPaginator($page, $limit); + } + /** * {@inheritdoc} */ diff --git a/src/DI/Scaffolding/Adapter/Mongo.php b/src/DI/Scaffolding/Adapter/Mongo.php index d6668b9..5b6ff1f 100644 --- a/src/DI/Scaffolding/Adapter/Mongo.php +++ b/src/DI/Scaffolding/Adapter/Mongo.php @@ -50,21 +50,32 @@ public function __construct() public function retrieveOne($id) { $record = call_user_func(array($this->scaffolding->getRecord(),'findById'),$id); - + if (!$record) { throw new RecordNotFoundException(); } - + return $record; } + /** + * {@inheritdoc} + */ + public function getPaginator($page = 1, $limit = 10) + { + return new \Vegas\Paginator\Adapter\Mongo(array( + 'model' => $this->scaffolding->getRecord(), + 'limit' => $limit, + 'page' => $page + )); + } + /** * {@inheritdoc} */ public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding) { $this->scaffolding = $scaffolding; - + return $this; } - } diff --git a/src/DI/Scaffolding/Adapter/Mysql.php b/src/DI/Scaffolding/Adapter/Mysql.php index 7e223cf..6380369 100644 --- a/src/DI/Scaffolding/Adapter/Mysql.php +++ b/src/DI/Scaffolding/Adapter/Mysql.php @@ -46,20 +46,32 @@ public function __construct() public function retrieveOne($id) { $record = call_user_func(array($this->scaffolding->getRecord(),'findById'),$id); - + if (!$record) { throw new RecordNotFoundException(); } - + return $record; } + /** + * {@inheritdoc} + */ + public function getPaginator($page = 1, $limit = 10) + { + return new \Phalcon\Paginator\Adapter\Model(array( + 'data' => call_user_func(array($this->scaffolding->getRecord(),'find')), + 'limit' => $limit, + 'page' => $page + )); + } + /** * {@inheritdoc} */ public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding) { $this->scaffolding = $scaffolding; - + return $this; } diff --git a/src/DI/Scaffolding/AdapterInterface.php b/src/DI/Scaffolding/AdapterInterface.php index 703c0b9..8f78a31 100644 --- a/src/DI/Scaffolding/AdapterInterface.php +++ b/src/DI/Scaffolding/AdapterInterface.php @@ -25,6 +25,15 @@ interface AdapterInterface */ public function retrieveOne($id); + /** + * Retrieve list of records as paginator object. + * + * @param int $page + * @param int $limit + * @return \Phalcon\Paginator\AdapterInterface + */ + public function getPaginator($page = 1, $limit = 10); + /** * Sets scaffolding instance * diff --git a/src/DI/ScaffoldingInterface.php b/src/DI/ScaffoldingInterface.php index 238d8cf..3826722 100644 --- a/src/DI/ScaffoldingInterface.php +++ b/src/DI/ScaffoldingInterface.php @@ -64,6 +64,23 @@ public function setFormName($name); */ public function setModelName($name); + /** + * Retrieve record by its ID + * + * @param $id + * @return mixed + */ + public function doRead($id); + + /** + * Retrieve list of records as paginator object + * + * @param int $page + * @param int $limit + * @return \Phalcon\Paginator\AdapterInterface + */ + public function doPaginate($page = 1, $limit = 10); + /** * Creates new record * diff --git a/src/Mvc/Controller/Crud/views/_form.volt b/src/Mvc/Controller/Crud/views/_form.volt deleted file mode 100755 index 56f3115..0000000 --- a/src/Mvc/Controller/Crud/views/_form.volt +++ /dev/null @@ -1,23 +0,0 @@ -{% for element in form %} - {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} - {% set hasErrors = form.hasMessagesFor(element.getName()) %} - -
- {% if element.getLabel() %} - - {% endif %} - {% if hasErrors %} - - {% for error in form.getMessagesFor(element.getName()) %} - {{ error }} - {% endfor %} - - {% endif %} - {{ element }} -
-{% endfor %} - -
- - {{ i18n._('Cancel') }} -
\ No newline at end of file diff --git a/src/Mvc/Controller/Crud/views/edit.volt b/src/Mvc/Controller/Crud/views/edit.volt index fa19b15..1599372 100755 --- a/src/Mvc/Controller/Crud/views/edit.volt +++ b/src/Mvc/Controller/Crud/views/edit.volt @@ -15,7 +15,29 @@
- {{ partial('./views/_form', ['form': form]) }} + {% for element in form %} + {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} + {% set hasErrors = form.hasMessagesFor(element.getName()) %} + +
+ {% if element.getLabel() %} + + {% endif %} + {% if hasErrors %} + + {% for error in form.getMessagesFor(element.getName()) %} + {{ error }} + {% endfor %} + + {% endif %} + {{ element }} +
+ {% endfor %} + +
+ + {{ i18n._('Cancel') }} +
diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt index c54a257..63ad24a 100644 --- a/src/Mvc/Controller/Crud/views/index.volt +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -13,19 +13,21 @@
- - {% for field in fields %} - - {% endfor %} - - + + {% for field in fields %} + + {% endfor %} + + {% if(page.items) %} {% for item in page.items %} {% for key, field in fields %} - + {% endfor %} {% for key, field in fields %} - + {% endfor %}
{{ field }} 
{{ field['label'] }} 
{{ item.{key} }} + {{ field.readMapped(key) }} + {{ i18n._("Update")}} diff --git a/src/Mvc/Controller/Crud/views/new.volt b/src/Mvc/Controller/Crud/views/new.volt index a9be00f..bd369b0 100755 --- a/src/Mvc/Controller/Crud/views/new.volt +++ b/src/Mvc/Controller/Crud/views/new.volt @@ -15,7 +15,29 @@
- {{ partial('./views/_form', ['form': form]) }} + {% for element in form %} + {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} + {% set hasErrors = form.hasMessagesFor(element.getName()) %} + +
+ {% if element.getLabel() %} + + {% endif %} + {% if hasErrors %} + + {% for error in form.getMessagesFor(element.getName()) %} + {{ error }} + {% endfor %} + + {% endif %} + {{ element }} +
+ {% endfor %} + +
+ + {{ i18n._('Cancel') }} +
diff --git a/src/Mvc/Controller/Crud/views/show.volt b/src/Mvc/Controller/Crud/views/show.volt index b3d252a..bc1fccd 100644 --- a/src/Mvc/Controller/Crud/views/show.volt +++ b/src/Mvc/Controller/Crud/views/show.volt @@ -10,7 +10,7 @@
{{ field }}{{ item.{key} }}{{ item.readMapped(key) }}
diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index e0f9b1e..4ae0d31 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -37,15 +37,17 @@ public function initialize() { parent::initialize(); - if ($this->view instanceof \Vegas\Mvc\View && !$this->view->existsForCurrentAction()) { - $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud','']); + $this->eventsManager->attach('view:notFoundView', function ($event, $view) { + if ($view->getCurrentRenderLevel() == View::LEVEL_ACTION_VIEW) { + $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud','']); - $view = $this->getDI()->get('view'); - $view->setViewsDir($templatePath); - $view->setControllerViewPath('views'); + $view->setViewsDir($templatePath); + $view->setControllerViewPath('views'); + $view->setRenderLevel(View::LEVEL_ACTION_VIEW); - $this->view = $view; - } + $view->render('', $this->dispatcher->getActionName()); + } + }); } use HooksTrait; @@ -71,6 +73,13 @@ public function initialize() */ protected $modelName; + /** + * Array of fields names that will be used in index and show actions + * + * @var array + */ + protected $fields = []; + /** * Initializes scaffolding * @@ -103,6 +112,10 @@ private function isConfigured() public function indexAction() { $this->initializeScaffolding(); + + $paginator = $this->scaffolding->doPaginate($this->request->get('page', 'int', 1)); + $this->view->page = $paginator->getPaginate(); + $this->view->fields = $this->fields; } /** @@ -116,6 +129,7 @@ public function showAction($id) $this->beforeRead(); $this->view->record = $this->scaffolding->doRead($id); + $this->view->fields = $this->fields; $this->afterRead(); } @@ -242,4 +256,4 @@ public function deleteAction($id) return $this->afterDeleteException(); } } -} \ No newline at end of file +} diff --git a/src/Mvc/View.php b/src/Mvc/View.php index a7f9f32..de64ffa 100644 --- a/src/Mvc/View.php +++ b/src/Mvc/View.php @@ -55,20 +55,20 @@ public function __construct($options = null, $viewDir = null) { $this->registerEngines(array( '.volt' => function ($this, $di) use ($options) { - $volt = new \Vegas\Mvc\View\Engine\Volt($this, $di); - if (isset($options['cacheDir'])) { - $volt->setOptions(array( - 'compiledPath' => $options['cacheDir'], - 'compiledSeparator' => '_', - 'compileAlways' => isset($options['compileAlways']) ? $options['compileAlways'] : false - )); - } - $volt->registerFilters(); - $volt->registerHelpers(); - $volt->setExtension('.volt'); + $volt = new \Vegas\Mvc\View\Engine\Volt($this, $di); + if (isset($options['cacheDir'])) { + $volt->setOptions(array( + 'compiledPath' => $options['cacheDir'], + 'compiledSeparator' => '_', + 'compileAlways' => isset($options['compileAlways']) ? $options['compileAlways'] : false + )); + } + $volt->registerFilters(); + $volt->registerHelpers(); + $volt->setExtension('.volt'); - return $volt; - }, + return $volt; + }, '.phtml' => 'Phalcon\Mvc\View\Engine\Php' )); } @@ -137,6 +137,7 @@ protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cac foreach ($engines as $extension => $engine) { $viewEnginePath = $basePath . $this->resolveFullViewPath($viewPath) . $extension; + if (file_exists($viewEnginePath)) { if (is_object($eventsManager)) { $this->_activeRenderPath = $viewEnginePath; @@ -191,9 +192,7 @@ private function resolveFullViewPath($viewPath) */ private function resolveViewPath($viewPath) { - $path = $this->getViewsDir(); - $path = realpath($path . dirname($viewPath)) . DIRECTORY_SEPARATOR . basename($viewPath); - + $path = realpath($this->_viewsDir . dirname($viewPath)) . DIRECTORY_SEPARATOR . basename($viewPath); return $path; } @@ -289,9 +288,9 @@ private function resolveLocalPath($partialPath) { $partialDir = str_replace('./', '', dirname($partialPath)); $partialsDir = realpath(sprintf('%s%s', - $this->getViewsDir(), - $partialDir - )) . DIRECTORY_SEPARATOR; + $this->_viewsDir, + $partialDir + )) . DIRECTORY_SEPARATOR; return $partialsDir . basename($partialPath); } @@ -317,7 +316,7 @@ private function resolveGlobalPath($partialPath) private function resolveRelativePath($partialPath) { $partialsDirPath = realpath(sprintf('%s%s', - $this->getViewsDir(), + $this->_viewsDir, dirname($partialPath) )) . DIRECTORY_SEPARATOR; @@ -337,7 +336,6 @@ public function render($controllerName, $actionName, $params = null) { if (empty($this->controllerViewPath)) { $this->setControllerViewPath($controllerName); } - parent::render($this->controllerViewPath, $actionName, $params); } @@ -361,28 +359,6 @@ public function getControllerViewPath() public function setControllerViewPath($controllerName) { $this->controllerViewPath = str_replace('\\','/',strtolower($controllerName)); - $this->controllerFullViewPath = $this->getViewsDir() . $this->controllerViewPath; - } - - /** - * Check if view for current pair of controller/action exists. - * - * @return bool - */ - public function existsForCurrentAction() - { - $actionName = $this->getDI()->get('router')->getActionName(); - - if (empty($this->controllerViewPath)) { - $this->setControllerViewPath($this->getDI()->get('router')->getControllerName()); - } - - foreach ($this->getRegisteredEngines() As $ext => $engine) { - if (file_exists($this->controllerFullViewPath.DIRECTORY_SEPARATOR.$actionName.$ext)) { - return true; - } - } - - return false; + $this->controllerFullViewPath = $this->_viewsDir . $this->controllerViewPath; } } diff --git a/tests/Mvc/Controller/CrudTest.php b/tests/Mvc/Controller/CrudTest.php index b4d0be2..636c33f 100644 --- a/tests/Mvc/Controller/CrudTest.php +++ b/tests/Mvc/Controller/CrudTest.php @@ -46,7 +46,7 @@ public function testNew() $this->assertEquals($form->get('fake_field')->render(), $content); } - +/* public function testNotPostCreate() { $content = $this->bootstrap->run('/test/crud/create'); @@ -143,7 +143,7 @@ public function testDelete() $this->assertFalse($this->model); } - +*/ private function prepareFakeObject() { $this->model = FakeModel::findFirst(array(array( diff --git a/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php index 1704bbf..6f71c3d 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/BrokenCrudController.php @@ -20,6 +20,6 @@ class BrokenCrudController extends CrudAbstract public function initialize() { parent::initialize(); - $this->view->disableLevel(View::LEVEL_LAYOUT); + $this->view->disable(); } } \ No newline at end of file From 950e6afeaf3f7d52e3e5f638b3ac8af79a9f64b3 Mon Sep 17 00:00:00 2001 From: Arek Date: Mon, 1 Dec 2014 17:22:30 +0100 Subject: [PATCH 08/35] Paginator refactoring --- src/Db/Mapping/DateTime.php | 46 +++++++++++++++++++++++++++++++++ src/Paginator/Adapter/Mongo.php | 44 +++++++++++++++++-------------- tests/Tag/PaginationTest.php | 6 ++--- 3 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/Db/Mapping/DateTime.php diff --git a/src/Db/Mapping/DateTime.php b/src/Db/Mapping/DateTime.php new file mode 100644 index 0000000..95f52ef --- /dev/null +++ b/src/Db/Mapping/DateTime.php @@ -0,0 +1,46 @@ + + * @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\Db\Mapping; + +use Vegas\Db\MappingInterface; + +/** + * Class DateTime + * + * Simple mapper for decoding JSON value + * + * @package Vegas\Db\Mapping + */ +class DateTime implements MappingInterface +{ + /** + * {@inheritdoc} + */ + public function getName() + { + return 'dateTime'; + } + + /** + * {@inheritdoc} + */ + public function resolve(& $value) + { + if (is_integer($value) && strlen($value) > 0) { + $value = new \DateTime($value); + $value->format(\DateTime::ISO8601); + } + + return $value; + } +} \ No newline at end of file diff --git a/src/Paginator/Adapter/Mongo.php b/src/Paginator/Adapter/Mongo.php index ce566ca..2179ea1 100644 --- a/src/Paginator/Adapter/Mongo.php +++ b/src/Paginator/Adapter/Mongo.php @@ -78,10 +78,8 @@ public function __construct($config) foreach ($config as $key => $value) { $this->$key = $value; } - + $this->validate(); - - $this->model = new $this->modelName(); } /** @@ -93,12 +91,20 @@ public function __construct($config) */ private function validate() { - if (empty($this->modelName)) { + if (empty($this->modelName) && empty($this->model)) { throw new Exception\ModelNotSetException(); } - + + if (empty($this->model)) { + $this->model = new $this->modelName(); + } + + if (empty($this->modelName)) { + $this->modelName = get_class($this->model); + } + if (empty($this->db)) { - throw new Exception\DbNotSetException(); + $this->db = $this->model->getConnection(); } } @@ -108,13 +114,13 @@ private function validate() public function getPaginate() { $page = new \Vegas\Paginator\Page(); - + $page->current = $this->page; $page->next = $this->getNextPage(); $page->before = $this->getPreviousPage(); $page->total_pages = $this->getTotalPages(); $page->items = $this->getResults(); - + return $page; } @@ -128,7 +134,7 @@ public function getPreviousPage() if ($this->page > 1) { return ($this->page-1); } - + return null; } @@ -142,7 +148,7 @@ public function getNextPage() if ($this->page < $this->getTotalPages()) { return ($this->page+1); } - + return null; } @@ -156,7 +162,7 @@ public function getTotalPages() if (empty($this->totalPages)) { $this->totalPages = (int)ceil($this->getCursor()->count()/$this->limit); } - + return $this->totalPages; } @@ -169,7 +175,7 @@ public function getTotalPages() public function setCurrentPage($page) { $this->page = $page; - + return $this; } @@ -181,23 +187,23 @@ public function setCurrentPage($page) public function getResults() { $skip = ($this->page-1)*$this->limit; - + $cursor = $this->getCursor(); $cursor->skip($skip)->limit($this->limit); - + if (!empty($this->sort)) { $cursor->sort($this->sort); } - + $results = array(); - + foreach ($cursor As $row) { $object = new $this->modelName(); $object->writeAttributes($row); - + $results[] = $object; } - + return $results; } @@ -209,7 +215,7 @@ private function getCursor() { $source = $this->model->getSource(); $cursor = $this->db->$source->find($this->query); - + return $cursor; } } diff --git a/tests/Tag/PaginationTest.php b/tests/Tag/PaginationTest.php index da5d1ce..ea4ea9a 100644 --- a/tests/Tag/PaginationTest.php +++ b/tests/Tag/PaginationTest.php @@ -28,14 +28,14 @@ public function testException() $this->assertInstanceOf('\Vegas\Paginator\Adapter\Exception\ModelNotSetException', $ex); } - try { + /*try { $paginator = new Mongo(array( - 'modelName' => '\Vegas\Tests\Stub\Models\FakeModel' + 'modelName' => '\Vegas\Tests\Stub\Models\FakeModel', )); throw new \Exception('Not this exception.'); } catch (\Exception $ex) { $this->assertInstanceOf('\Vegas\Paginator\Adapter\Exception\DbNotSetException', $ex); - } + }*/ } public function testRender() From fed205ab0a672b0bc4218492055ca06b2cc99e9a Mon Sep 17 00:00:00 2001 From: Arek Date: Mon, 1 Dec 2014 17:25:18 +0100 Subject: [PATCH 09/35] CRUD index template quick fix --- src/Mvc/Controller/Crud/views/index.volt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt index 63ad24a..8f05fe3 100644 --- a/src/Mvc/Controller/Crud/views/index.volt +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -15,7 +15,7 @@ {% for field in fields %} - {{ field['label'] }} + {{ field }} {% endfor %}   From 7b66051a32d7b74c01078e6c070dd68f61d15d03 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:40:38 +0100 Subject: [PATCH 10/35] fix index template for crud --- src/Mvc/Controller/Crud/views/index.volt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt index 8f05fe3..e4ce439 100644 --- a/src/Mvc/Controller/Crud/views/index.volt +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -26,7 +26,7 @@ {% for key, field in fields %} - {{ field.readMapped(key) }} + {{ item.readMapped(key) }} {% endfor %} From dc3992c63605cbc4eb39d57ba43f3ac45a4d8f67 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:42:10 +0100 Subject: [PATCH 11/35] simple dateTime mapping for timestamp field --- src/Db/Mapping/DateTime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Db/Mapping/DateTime.php b/src/Db/Mapping/DateTime.php index 95f52ef..8d91307 100644 --- a/src/Db/Mapping/DateTime.php +++ b/src/Db/Mapping/DateTime.php @@ -37,8 +37,8 @@ public function getName() public function resolve(& $value) { if (is_integer($value) && strlen($value) > 0) { - $value = new \DateTime($value); - $value->format(\DateTime::ISO8601); + $dateTime = new \DateTime($value); + $value = $dateTime->format(\DateTime::ISO8601); } return $value; From cdff503ee3334a2dec3aa8f597efbd5fab4d8218 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:46:06 +0100 Subject: [PATCH 12/35] temp dateTime format change --- src/Db/Mapping/DateTime.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Db/Mapping/DateTime.php b/src/Db/Mapping/DateTime.php index 8d91307..9bae3e1 100644 --- a/src/Db/Mapping/DateTime.php +++ b/src/Db/Mapping/DateTime.php @@ -9,7 +9,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - + namespace Vegas\Db\Mapping; use Vegas\Db\MappingInterface; @@ -37,8 +37,9 @@ public function getName() public function resolve(& $value) { if (is_integer($value) && strlen($value) > 0) { - $dateTime = new \DateTime($value); - $value = $dateTime->format(\DateTime::ISO8601); + $dateTime = new \DateTime(); + $dateTime->setTimestamp($value); + $value = $dateTime->format('Y-m-d H:i:s'); } return $value; From 9cdd2fd94c93723b7d98221e65e8600ae3901ea9 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:48:38 +0100 Subject: [PATCH 13/35] crud show template fix --- src/Mvc/Controller/Crud/views/show.volt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mvc/Controller/Crud/views/show.volt b/src/Mvc/Controller/Crud/views/show.volt index bc1fccd..464c6c2 100644 --- a/src/Mvc/Controller/Crud/views/show.volt +++ b/src/Mvc/Controller/Crud/views/show.volt @@ -10,7 +10,7 @@ {% for key, field in fields %} {{ field }} - {{ item.readMapped(key) }} + {{ record.readMapped(key) }} {% endfor %} From a6a271f7b1c3ea77a035ef17d7aaf0bd8dbf2473 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:49:41 +0100 Subject: [PATCH 14/35] crud show template fix --- src/Mvc/Controller/Crud/views/show.volt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Mvc/Controller/Crud/views/show.volt b/src/Mvc/Controller/Crud/views/show.volt index 464c6c2..3f5f8f8 100644 --- a/src/Mvc/Controller/Crud/views/show.volt +++ b/src/Mvc/Controller/Crud/views/show.volt @@ -9,8 +9,10 @@ {% for key, field in fields %} - - + + + + {% endfor %}
{{ field }}{{ record.readMapped(key) }}
{{ field }}{{ record.readMapped(key) }}
From 273136b8a483b6d286cb0a562df7ed3e9566a039 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 14:53:57 +0100 Subject: [PATCH 15/35] CRUD: separate show and index fields, add show button to index.volt --- src/Mvc/Controller/Crud/views/index.volt | 1 + src/Mvc/Controller/CrudAbstract.php | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt index e4ce439..c9dbede 100644 --- a/src/Mvc/Controller/Crud/views/index.volt +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -30,6 +30,7 @@ {% endfor %} + {{ i18n._("Show")}} {{ i18n._("Update")}} {{ i18n._("Delete")}} diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 4ae0d31..146eabe 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -74,11 +74,18 @@ public function initialize() protected $modelName; /** - * Array of fields names that will be used in index and show actions + * Array of fields names that will be used in index action * * @var array */ - protected $fields = []; + protected $indexFields = []; + + /** + * Array of fields names that will be used in show action + * + * @var array + */ + protected $showFields = []; /** * Initializes scaffolding @@ -115,7 +122,7 @@ public function indexAction() $paginator = $this->scaffolding->doPaginate($this->request->get('page', 'int', 1)); $this->view->page = $paginator->getPaginate(); - $this->view->fields = $this->fields; + $this->view->fields = $this->indexFields; } /** @@ -129,7 +136,7 @@ public function showAction($id) $this->beforeRead(); $this->view->record = $this->scaffolding->doRead($id); - $this->view->fields = $this->fields; + $this->view->fields = $this->showFields; $this->afterRead(); } From 9b9f4639a7def99214faa37d9db83e9369b279b2 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 2 Dec 2014 17:03:17 +0100 Subject: [PATCH 16/35] - Module/ModuleLoader renamed to Module/Loader - Module/Loader loads modules from vendors module dir instead src --- src/Application/Bootstrap.php | 3 +-- src/Cli/Bootstrap.php | 4 +--- src/Mvc/Module/{ModuleLoader.php => Loader.php} | 8 ++++---- tests/App/TestCase.php | 2 +- tests/DI/Service/ComponentTest.php | 2 +- tests/Mvc/ApplicationTest.php | 2 +- tests/Mvc/Module/{ModuleLoaderTest.php => LoaderTest.php} | 4 ++-- tests/Mvc/RouterTest.php | 2 +- 8 files changed, 12 insertions(+), 15 deletions(-) rename src/Mvc/Module/{ModuleLoader.php => Loader.php} (95%) rename tests/Mvc/Module/{ModuleLoaderTest.php => LoaderTest.php} (89%) diff --git a/src/Application/Bootstrap.php b/src/Application/Bootstrap.php index 629928d..c281044 100644 --- a/src/Application/Bootstrap.php +++ b/src/Application/Bootstrap.php @@ -20,8 +20,7 @@ use Vegas\Constants; use Vegas\DI\ServiceProviderLoader; use Vegas\Mvc\Dispatcher\Events\BeforeException; -use Vegas\Mvc\Module\ModuleLoader; -use Vegas\Mvc\Module\SubModuleManager; +use Vegas\Mvc\Module\Loader As ModuleLoader; /** * Class Bootstrap diff --git a/src/Cli/Bootstrap.php b/src/Cli/Bootstrap.php index 6caa100..a6b522f 100644 --- a/src/Cli/Bootstrap.php +++ b/src/Cli/Bootstrap.php @@ -13,13 +13,11 @@ namespace Vegas\Cli; use Phalcon\DI\FactoryDefault\CLI; -use Vegas\BootstrapAbstract; use Vegas\BootstrapInterface; use Vegas\Cli\Exception as CliException; use Vegas\Constants; use Vegas\DI\ServiceProviderLoader; -use Vegas\Mvc\Module\ModuleLoader; -use Vegas\Mvc\Module\SubModuleManager; +use Vegas\Mvc\Module\Loader As ModuleLoader; /** * Class Bootstrap diff --git a/src/Mvc/Module/ModuleLoader.php b/src/Mvc/Module/Loader.php similarity index 95% rename from src/Mvc/Module/ModuleLoader.php rename to src/Mvc/Module/Loader.php index 5a75267..ce4d426 100644 --- a/src/Mvc/Module/ModuleLoader.php +++ b/src/Mvc/Module/Loader.php @@ -16,10 +16,10 @@ use Vegas\Util\FileWriter; /** - * Class ModuleLoader - * @package Vegas\Mvc + * Class Loader + * @package Vegas\Mvc\Module */ -class ModuleLoader +class Loader { /** * Default name of file containing module settings @@ -82,7 +82,7 @@ private static function dumpModulesFromVendor(array $modulesList) $directoryIterator = new \DirectoryIterator($vendorDir); foreach ($directoryIterator as $libDir) { if ($libDir->isDot()) continue; - $moduleSettingsFile = $vendorDir. DIRECTORY_SEPARATOR . $libDir . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR .self::MODULE_SETTINGS_FILE; + $moduleSettingsFile = $vendorDir. DIRECTORY_SEPARATOR . $libDir . DIRECTORY_SEPARATOR . 'module' . DIRECTORY_SEPARATOR .self::MODULE_SETTINGS_FILE; if (!file_exists($moduleSettingsFile)) { continue; } diff --git a/tests/App/TestCase.php b/tests/App/TestCase.php index 01ed980..a8864cc 100644 --- a/tests/App/TestCase.php +++ b/tests/App/TestCase.php @@ -14,7 +14,7 @@ use Phalcon\DI; use Vegas\Mvc\Application; use Vegas\Mvc\Controller\Crud; -use Vegas\Mvc\Module\ModuleLoader; +use Vegas\Mvc\Module\Loader As ModuleLoader; class TestCase extends \PHPUnit_Framework_TestCase { diff --git a/tests/DI/Service/ComponentTest.php b/tests/DI/Service/ComponentTest.php index d39b5d1..8361fd7 100644 --- a/tests/DI/Service/ComponentTest.php +++ b/tests/DI/Service/ComponentTest.php @@ -15,7 +15,7 @@ use Test\Components\Fake; use Vegas\DI\Service\Component\Renderer; use Vegas\Mvc\Application; -use Vegas\Mvc\Module\ModuleLoader; +use Vegas\Mvc\Module\Loader As ModuleLoader; use Vegas\Mvc\View; use Vegas\Tests\App\TestCase; diff --git a/tests/Mvc/ApplicationTest.php b/tests/Mvc/ApplicationTest.php index 315a916..de4d2e1 100644 --- a/tests/Mvc/ApplicationTest.php +++ b/tests/Mvc/ApplicationTest.php @@ -14,7 +14,7 @@ use Phalcon\DI; use Vegas\Mvc\Application; -use Vegas\Mvc\Module\ModuleLoader; +use Vegas\Mvc\Module\Loader As ModuleLoader; class ApplicationTest extends \PHPUnit_Framework_TestCase { diff --git a/tests/Mvc/Module/ModuleLoaderTest.php b/tests/Mvc/Module/LoaderTest.php similarity index 89% rename from tests/Mvc/Module/ModuleLoaderTest.php rename to tests/Mvc/Module/LoaderTest.php index 22a24d2..5bc4a54 100644 --- a/tests/Mvc/Module/ModuleLoaderTest.php +++ b/tests/Mvc/Module/LoaderTest.php @@ -13,9 +13,9 @@ namespace Vegas\Tests\Mvc\Module; use Phalcon\DI; -use Vegas\Mvc\Module\ModuleLoader; +use Vegas\Mvc\Module\Loader As ModuleLoader; -class ModuleLoaderTest extends \PHPUnit_Framework_TestCase +class LoaderTest extends \PHPUnit_Framework_TestCase { public function testDump() { diff --git a/tests/Mvc/RouterTest.php b/tests/Mvc/RouterTest.php index 1f11b8d..d49e3b5 100644 --- a/tests/Mvc/RouterTest.php +++ b/tests/Mvc/RouterTest.php @@ -14,7 +14,7 @@ use Phalcon\DI; use Vegas\Http\Method; -use Vegas\Mvc\Module\ModuleLoader; +use Vegas\Mvc\Module\Loader As ModuleLoader; use Vegas\Mvc\Router; class RouterTest extends \PHPUnit_Framework_TestCase From e9c17c35a18f4df97c2e1c4e065f074f0692f16e Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 3 Dec 2014 13:32:09 +0100 Subject: [PATCH 17/35] Volt traits fixes; --- ...gisterFilters.php => RegisterFiltersTrait.php} | 13 ++++++++++--- ...gisterHelpers.php => RegisterHelpersTrait.php} | 15 +++++++++++---- src/Mvc/View/Engine/Volt.php | 4 ++-- tests/fixtures/app/config/config.php | 7 ++----- 4 files changed, 25 insertions(+), 14 deletions(-) rename src/Mvc/View/Engine/{RegisterFilters.php => RegisterFiltersTrait.php} (81%) rename src/Mvc/View/Engine/{RegisterHelpers.php => RegisterHelpersTrait.php} (77%) diff --git a/src/Mvc/View/Engine/RegisterFilters.php b/src/Mvc/View/Engine/RegisterFiltersTrait.php similarity index 81% rename from src/Mvc/View/Engine/RegisterFilters.php rename to src/Mvc/View/Engine/RegisterFiltersTrait.php index 3359afd..2bd84fe 100644 --- a/src/Mvc/View/Engine/RegisterFilters.php +++ b/src/Mvc/View/Engine/RegisterFiltersTrait.php @@ -10,14 +10,13 @@ * file that was distributed with this source code. */ - namespace Vegas\Mvc\View\Engine; /** - * Class RegisterFilters + * Class RegisterFiltersTrait * @package Vegas\Mvc\View\Engine */ -trait RegisterFilters +trait RegisterFiltersTrait { /** * Returns the path to view filter directory @@ -42,5 +41,13 @@ public function registerFilters () $this->registerFilter(lcfirst($filterName)); } } + + /** + * Registers filter indicated by its name + * + * @param $filterName + * @return mixed + */ + abstract public function registerFilter($filterName); } \ No newline at end of file diff --git a/src/Mvc/View/Engine/RegisterHelpers.php b/src/Mvc/View/Engine/RegisterHelpersTrait.php similarity index 77% rename from src/Mvc/View/Engine/RegisterHelpers.php rename to src/Mvc/View/Engine/RegisterHelpersTrait.php index ed9207f..0c0f204 100644 --- a/src/Mvc/View/Engine/RegisterHelpers.php +++ b/src/Mvc/View/Engine/RegisterHelpersTrait.php @@ -10,17 +10,16 @@ * file that was distributed with this source code. */ - namespace Vegas\Mvc\View\Engine; /** - * Class RegisterHelpers + * Class RegisterHelpersTrait * @package Vegas\Mvc\View\Engine */ -trait RegisterHelpers +trait RegisterHelpersTrait { /** - * Returns the ath to view helpers directory + * Returns the path to view helpers directory * * @return string * @internal @@ -42,5 +41,13 @@ public function registerHelpers() $this->registerHelper(lcfirst($helperName)); } } + + /** + * Register helper indicated by its name + * + * @param $helperName + * @return mixed + */ + abstract public function registerHelper($helperName); } \ No newline at end of file diff --git a/src/Mvc/View/Engine/Volt.php b/src/Mvc/View/Engine/Volt.php index 56dd88d..a122339 100644 --- a/src/Mvc/View/Engine/Volt.php +++ b/src/Mvc/View/Engine/Volt.php @@ -24,8 +24,8 @@ */ class Volt extends \Phalcon\Mvc\View\Engine\Volt { - use RegisterFilters; - use RegisterHelpers; + use RegisterFiltersTrait; + use RegisterHelpersTrait; /** * Extension of template file diff --git a/tests/fixtures/app/config/config.php b/tests/fixtures/app/config/config.php index f8ef035..3275e4f 100644 --- a/tests/fixtures/app/config/config.php +++ b/tests/fixtures/app/config/config.php @@ -13,17 +13,13 @@ 'taskDir' => APP_ROOT . '/app/tasks/', 'baseUri' => '/', 'language' => 'nl_NL', - 'subModules' => array( - 'frontend', 'backend', 'custom' - ), 'view' => array( 'cacheDir' => APP_ROOT . '/cache/', 'layout' => 'main', 'layoutsDir' => APP_ROOT . '/app/layouts/', 'partialsDir' => APP_ROOT . '/app/layouts/partials/', 'compileAlways' => true - ), - 'hostname' => 'vegas.dev' + ) ), 'plugins' => array(), @@ -37,6 +33,7 @@ "dbname" => "vegas_test", "port" => 3306, "username" => "root", + 'password'=> 'root', "options" => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) From 340ba9f998db3872cc2321227b7c7d3e0eeaf78d Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 3 Dec 2014 13:48:43 +0100 Subject: [PATCH 18/35] Added Route tests; --- tests/Mvc/Router/RouteTest.php | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/Mvc/Router/RouteTest.php diff --git a/tests/Mvc/Router/RouteTest.php b/tests/Mvc/Router/RouteTest.php new file mode 100644 index 0000000..294f307 --- /dev/null +++ b/tests/Mvc/Router/RouteTest.php @@ -0,0 +1,43 @@ + + * @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\Mvc\Router; + +use Vegas\Mvc\Router\Route; + +class RouteTest extends \PHPUnit_Framework_TestCase +{ + public function testShouldMapConstructorParametersToClassProperties() + { + $routeArray = [ + 'route' => '/url', + 'paths' => array( + 'module' => 'module', + 'controller' => 'controller', + 'action' => 'action', + + 'auth' => array('auth', 'authAdmin') + ), + 'type' => 'static', + 'params' => array() + ]; + + $route = new Route('test', $routeArray); + + $this->assertEquals($route->getName(), 'test'); + $this->assertArrayHasKey('module', $route->getPaths()); + $this->assertArrayHasKey('controller', $route->getPaths()); + $this->assertArrayHasKey('action', $route->getPaths()); + $this->assertArrayHasKey('auth', $route->getPaths()); + + } +} \ No newline at end of file From b5be1b85f814cb30d5bb722848bd3cfc09f1c85b Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 3 Dec 2014 14:20:13 +0100 Subject: [PATCH 19/35] Added Route, Adapter\Standard tests; --- tests/Mvc/Router/Adapter/StandardTest.php | 32 ++++++++++++++++++++++- tests/Mvc/Router/RouteTest.php | 12 +++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests/Mvc/Router/Adapter/StandardTest.php b/tests/Mvc/Router/Adapter/StandardTest.php index 751f14b..df453bc 100644 --- a/tests/Mvc/Router/Adapter/StandardTest.php +++ b/tests/Mvc/Router/Adapter/StandardTest.php @@ -18,9 +18,39 @@ class StandardTest extends \PHPUnit_Framework_TestCase { - public function testAdapterInstance() + public function testShouldAdapterInstanceImplementRouterInterface() { $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $this->assertInstanceOf('\Phalcon\Mvc\RouterInterface', $router); } + + public function testShouldRemoveExtraSlashes() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router->add('/test', [ + 'module' => 'module', + 'controller' => 'controller', + 'action' => 'action' + ])->setName('test'); + + $router->handle('/test/'); + $matchedRoute = $router->getMatchedRoute(); + $this->assertNotNull($matchedRoute); + $this->assertEquals($matchedRoute->getName(), 'test'); + $this->assertEquals($matchedRoute->getPattern(), '/test'); + } + + public function testShouldNotMatchRouteWithExtraSlash() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router->removeExtraSlashes(false); + $router->add('/test', [ + 'module' => 'module', + 'controller' => 'controller', + 'action' => 'action' + ])->setName('test'); + + $router->handle('/test/'); + $this->assertNull($router->getMatchedRoute()); + } } \ No newline at end of file diff --git a/tests/Mvc/Router/RouteTest.php b/tests/Mvc/Router/RouteTest.php index 294f307..1d0d10e 100644 --- a/tests/Mvc/Router/RouteTest.php +++ b/tests/Mvc/Router/RouteTest.php @@ -28,7 +28,11 @@ public function testShouldMapConstructorParametersToClassProperties() 'auth' => array('auth', 'authAdmin') ), 'type' => 'static', - 'params' => array() + 'params' => array( + 'param_1' => 'value_1', + 'param_2' => 'value_2', + 'param_3' => 'value_3' + ) ]; $route = new Route('test', $routeArray); @@ -38,6 +42,10 @@ public function testShouldMapConstructorParametersToClassProperties() $this->assertArrayHasKey('controller', $route->getPaths()); $this->assertArrayHasKey('action', $route->getPaths()); $this->assertArrayHasKey('auth', $route->getPaths()); - + $this->assertEquals('value_1', $route->getParam('param_1')); + $this->assertEquals('value_2', $route->getParam('param_2')); + $this->assertEquals('value_3', $route->getParam('param_3')); + $this->assertSame(json_encode(['auth', 'authAdmin']), $route->getPaths()['auth']); + $this->assertEquals('/url', $route->getRoute()); } } \ No newline at end of file From acbc897d2f45388314e99118ec70da84f8bbef3f Mon Sep 17 00:00:00 2001 From: Arek Date: Thu, 4 Dec 2014 14:59:47 +0100 Subject: [PATCH 20/35] CRUD tests working properly, added external Module loading example and test --- src/Mvc/Controller/Crud/views/edit.volt | 6 +- src/Mvc/Controller/Crud/views/index.volt | 12 ++-- src/Mvc/Controller/Crud/views/new.volt | 6 +- src/Mvc/Controller/Crud/views/show.volt | 2 +- src/Mvc/Controller/CrudAbstract.php | 4 ++ tests/Mvc/Controller/CrudTest.php | 67 +++++++++++++++++-- tests/Mvc/Module/LoaderTest.php | 4 +- .../controllers/backend/CrudController.php | 9 +++ .../modules/Test/views/backend/crud/edit.volt | 1 - .../modules/Test/views/backend/crud/new.volt | 1 - tests/fixtures/composer.json | 12 ++++ .../fake-vendor-module/module/Module.php | 21 ++++++ .../controllers/backend/FakeController.php | 23 +++++++ 13 files changed, 148 insertions(+), 20 deletions(-) delete mode 100644 tests/fixtures/app/modules/Test/views/backend/crud/edit.volt delete mode 100644 tests/fixtures/app/modules/Test/views/backend/crud/new.volt create mode 100644 tests/fixtures/composer.json create mode 100644 tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/Module.php create mode 100644 tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/controllers/backend/FakeController.php diff --git a/src/Mvc/Controller/Crud/views/edit.volt b/src/Mvc/Controller/Crud/views/edit.volt index 1599372..3118976 100755 --- a/src/Mvc/Controller/Crud/views/edit.volt +++ b/src/Mvc/Controller/Crud/views/edit.volt @@ -4,7 +4,7 @@
-

{{ i18n._('Update record') }}

+

Update record

@@ -35,8 +35,8 @@ {% endfor %}
- - {{ i18n._('Cancel') }} + + Cancel
diff --git a/src/Mvc/Controller/Crud/views/index.volt b/src/Mvc/Controller/Crud/views/index.volt index c9dbede..3efc6eb 100644 --- a/src/Mvc/Controller/Crud/views/index.volt +++ b/src/Mvc/Controller/Crud/views/index.volt @@ -1,10 +1,10 @@
-

{{ i18n._('Records list') }}

+

Records list

@@ -30,15 +30,15 @@ {% endfor %} - {{ i18n._("Show")}} - {{ i18n._("Update")}} - {{ i18n._("Delete")}} + Show + Update + Delete {% endfor %} {% else %} - {{ i18n._("No records found.")}} + No records found. {% endif %} diff --git a/src/Mvc/Controller/Crud/views/new.volt b/src/Mvc/Controller/Crud/views/new.volt index bd369b0..017dcd4 100755 --- a/src/Mvc/Controller/Crud/views/new.volt +++ b/src/Mvc/Controller/Crud/views/new.volt @@ -4,7 +4,7 @@
-

{{ i18n._('Add record') }}

+

Add record

@@ -35,8 +35,8 @@ {% endfor %}
- - {{ i18n._('Cancel') }} + + Cancel
diff --git a/src/Mvc/Controller/Crud/views/show.volt b/src/Mvc/Controller/Crud/views/show.volt index 3f5f8f8..c5ec7aa 100644 --- a/src/Mvc/Controller/Crud/views/show.volt +++ b/src/Mvc/Controller/Crud/views/show.volt @@ -1,7 +1,7 @@
-

{{ i18n._('Record details') }}

+

Record details

diff --git a/src/Mvc/Controller/CrudAbstract.php b/src/Mvc/Controller/CrudAbstract.php index 146eabe..c9e4008 100644 --- a/src/Mvc/Controller/CrudAbstract.php +++ b/src/Mvc/Controller/CrudAbstract.php @@ -38,6 +38,10 @@ public function initialize() parent::initialize(); $this->eventsManager->attach('view:notFoundView', function ($event, $view) { + if (!in_array($this->dispatcher->getActionName(), ['new', 'edit', 'show', 'index'])) { + return false; + } + if ($view->getCurrentRenderLevel() == View::LEVEL_ACTION_VIEW) { $templatePath = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'Crud','']); diff --git a/tests/Mvc/Controller/CrudTest.php b/tests/Mvc/Controller/CrudTest.php index 636c33f..2d0b42f 100644 --- a/tests/Mvc/Controller/CrudTest.php +++ b/tests/Mvc/Controller/CrudTest.php @@ -42,14 +42,17 @@ public function testNew() $_SERVER['REQUEST_METHOD'] = 'GET'; $form = new Fake(); + $content = $this->bootstrap->run('/test/crud/new'); - $this->assertEquals($form->get('fake_field')->render(), $content); + $this->assertContains($form->get('fake_field')->render(['class' => ' form-control']), $content); + $this->assertContains('
', $content); } -/* + public function testNotPostCreate() { $content = $this->bootstrap->run('/test/crud/create'); + $this->assertContains('500', $content); $this->assertContains('This is not a POST request!', $content); } @@ -81,6 +84,15 @@ public function testPostCreateResponse() $model->delete(); } + public function testPostCreateException() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['fake_field'] = ''; + + $content = $this->bootstrap->run('/test/crud/create'); + $this->assertContains('Field fake_field is required', $content); + } + public function testEdit() { $this->prepareFakeObject(); @@ -90,7 +102,8 @@ public function testEdit() $form = new Fake($this->model); $content = $this->bootstrap->run('/test/crud/edit/'.$this->model->getId()); - $this->assertEquals($form->get('fake_field')->render(), $content); + $this->assertContains($form->get('fake_field')->render(['class' => ' form-control']), $content); + $this->assertContains('', $content); } public function testNotPostUpdate() @@ -98,6 +111,7 @@ public function testNotPostUpdate() $this->prepareFakeObject(); $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); + $this->assertContains('500', $content); $this->assertContains('This is not a POST request!', $content); } @@ -131,6 +145,41 @@ public function testPostUpdateResponse() $model->delete(); } + public function testPostUpdateException() + { + $this->prepareFakeObject(); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['fake_field'] = ''; + + $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); + $this->assertContains('Field fake_field is required', $content); + } + + public function testIndex() + { + $this->prepareFakeObject(); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $content = $this->bootstrap->run('/test/crud/index/'); + + $this->assertContains('Fake field index', $content); + $this->assertContains($this->model->fake_field, $content); + } + + public function testShow() + { + $this->prepareFakeObject(); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $content = $this->bootstrap->run('/test/crud/show/'.$this->model->getId()); + + $this->assertContains('Fake field', $content); + $this->assertContains($this->model->fake_field, $content); + } + public function testDelete() { $this->model = FakeModel::findFirst(); @@ -143,7 +192,17 @@ public function testDelete() $this->assertFalse($this->model); } -*/ + + public function testDeleteException() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $content = $this->bootstrap->run('/test/crud/delete/RanDoMn0t1D4sUR3'); + + $this->assertContains('500', $content); + $this->assertContains('Invalid object ID', $content); + } + private function prepareFakeObject() { $this->model = FakeModel::findFirst(array(array( diff --git a/tests/Mvc/Module/LoaderTest.php b/tests/Mvc/Module/LoaderTest.php index 5bc4a54..cbb700b 100644 --- a/tests/Mvc/Module/LoaderTest.php +++ b/tests/Mvc/Module/LoaderTest.php @@ -27,8 +27,10 @@ public function testDump() $modulesArray = require TESTS_ROOT_DIR . '/fixtures/app/config/modules.php'; $this->assertInternalType('array', $modulesArray); - $this->assertInternalType('string', ModuleLoader::MODULE_SETTINGS_FILE); + + $this->assertArrayHasKey('Foo', $modulesArray); + $this->assertArrayHasKey('FakeVendorModule', $modulesArray); } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php index 99aca60..b097db5 100644 --- a/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php +++ b/tests/fixtures/app/modules/Test/controllers/backend/CrudController.php @@ -20,6 +20,15 @@ class CrudController extends CrudAbstract protected $formName = 'Test\Forms\Fake'; protected $modelName = 'Test\Models\Fake'; + protected $showFields = [ + 'fake_field' => 'Fake field', + 'created_at' => 'Created at' + ]; + protected $indexFields = [ + 'fake_field' => 'Fake field index', + 'created_at' => 'Created at index' + ]; + public function initialize() { parent::initialize(); diff --git a/tests/fixtures/app/modules/Test/views/backend/crud/edit.volt b/tests/fixtures/app/modules/Test/views/backend/crud/edit.volt deleted file mode 100644 index 79e0c11..0000000 --- a/tests/fixtures/app/modules/Test/views/backend/crud/edit.volt +++ /dev/null @@ -1 +0,0 @@ -{{ form.get('fake_field') }} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/views/backend/crud/new.volt b/tests/fixtures/app/modules/Test/views/backend/crud/new.volt deleted file mode 100644 index 79e0c11..0000000 --- a/tests/fixtures/app/modules/Test/views/backend/crud/new.volt +++ /dev/null @@ -1 +0,0 @@ -{{ form.get('fake_field') }} \ No newline at end of file diff --git a/tests/fixtures/composer.json b/tests/fixtures/composer.json new file mode 100644 index 0000000..f8b8d9f --- /dev/null +++ b/tests/fixtures/composer.json @@ -0,0 +1,12 @@ +{ + "name": "vegas-cmf-fixtures/fake", + "description": "Fake composer.json for testing purposes.", + "license": "MIT", + "authors": [ + { + "name": "Amsterdam Standard Vegas Team", + "email": "vegas@amsterdam-standard.pl" + } + ], + "vendor-dir": "vendor" +} diff --git a/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/Module.php b/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/Module.php new file mode 100644 index 0000000..c72d148 --- /dev/null +++ b/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/Module.php @@ -0,0 +1,21 @@ + + * @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 FakeVendorModule; + +class Module extends \Vegas\Mvc\ModuleAbstract +{ + public function __construct() { + $this->namespace = __NAMESPACE__; + $this->dir = __DIR__; + } +} \ No newline at end of file diff --git a/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/controllers/backend/FakeController.php b/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/controllers/backend/FakeController.php new file mode 100644 index 0000000..7c886cc --- /dev/null +++ b/tests/fixtures/vendor/vegas-cmf/fake-vendor-module/module/controllers/backend/FakeController.php @@ -0,0 +1,23 @@ + + * @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 FakeVendorModule\Controllers\Backend; + +use Vegas\Mvc\ControllerAbstract; + +class FakeController extends ControllerAbstract +{ + public function fakeAction() + { + + } +} \ No newline at end of file From 898029ebaeb0553e1dfcf8933eaf9142b8d8f444 Mon Sep 17 00:00:00 2001 From: slawek Date: Thu, 4 Dec 2014 15:45:36 +0100 Subject: [PATCH 21/35] Added route tests --- src/Mvc/Router.php | 2 +- .../Exception/InvalidRouteNameException.php | 31 +++++++ .../Exception/InvalidRoutePathsException.php | 31 +++++++ src/Mvc/Router/Route.php | 16 +++- src/Mvc/Router/Route/BaseRoute.php | 2 +- src/Mvc/Router/Route/DefaultRoute.php | 11 ++- src/Mvc/Router/Route/NotfoundRoute.php | 1 + tests/Mvc/Router/Adapter/StandardTest.php | 7 +- tests/Mvc/Router/Route/BaseRouteTest.php | 84 +++++++++++++++++++ tests/Mvc/Router/Route/DefaultRouteTest.php | 83 ++++++++++++++++++ tests/Mvc/Router/Route/NotFoundRouteTest.php | 69 +++++++++++++++ 11 files changed, 329 insertions(+), 8 deletions(-) create mode 100644 src/Mvc/Router/Exception/InvalidRouteNameException.php create mode 100644 src/Mvc/Router/Exception/InvalidRoutePathsException.php create mode 100644 tests/Mvc/Router/Route/BaseRouteTest.php create mode 100644 tests/Mvc/Router/Route/DefaultRouteTest.php create mode 100644 tests/Mvc/Router/Route/NotFoundRouteTest.php diff --git a/src/Mvc/Router.php b/src/Mvc/Router.php index 0dbca2f..b84ebfe 100644 --- a/src/Mvc/Router.php +++ b/src/Mvc/Router.php @@ -161,7 +161,7 @@ private function groupRoutes() foreach ($this->routes as $routePattern => $route) { if (!isset($route['type'])) { - $route['type'] = Router::DEFAULT_ROUTE; + $route['type'] = Router::BASE_ROUTE; } $this->validateRouteType($route['type']); diff --git a/src/Mvc/Router/Exception/InvalidRouteNameException.php b/src/Mvc/Router/Exception/InvalidRouteNameException.php new file mode 100644 index 0000000..e921799 --- /dev/null +++ b/src/Mvc/Router/Exception/InvalidRouteNameException.php @@ -0,0 +1,31 @@ + + * @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\Mvc\Router\Exception; + +use Vegas\Mvc\Router\Exception as RouterException; + +/** + * Class InvalidRouteTypeException + * Thrown when trying to add route with empty name + * + * @package Vegas\Mvc\Router\Exception + */ +class InvalidRouteNameException extends RouterException +{ + /** + * Exception default message + * + * @var string + */ + protected $message = 'Invalid route name'; +} \ No newline at end of file diff --git a/src/Mvc/Router/Exception/InvalidRoutePathsException.php b/src/Mvc/Router/Exception/InvalidRoutePathsException.php new file mode 100644 index 0000000..60af60b --- /dev/null +++ b/src/Mvc/Router/Exception/InvalidRoutePathsException.php @@ -0,0 +1,31 @@ + + * @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\Mvc\Router\Exception; + +use Vegas\Mvc\Router\Exception as RouterException; + +/** + * Class InvalidRouteTypeException + * Thrown when trying to add route with empty path + * + * @package Vegas\Mvc\Router\Exception + */ +class InvalidRoutePathsException extends RouterException +{ + /** + * Exception default message + * + * @var string + */ + protected $message = 'Invalid route paths'; +} \ No newline at end of file diff --git a/src/Mvc/Router/Route.php b/src/Mvc/Router/Route.php index ab3158f..1844f76 100644 --- a/src/Mvc/Router/Route.php +++ b/src/Mvc/Router/Route.php @@ -11,6 +11,8 @@ */ namespace Vegas\Mvc\Router; +use Vegas\Mvc\Router\Exception\InvalidRouteNameException; +use Vegas\Mvc\Router\Exception\InvalidRoutePathsException; /** * Class Route @@ -60,12 +62,24 @@ class Route * * @param $name Name of route * @param $routeArray + * @throws Exception\InvalidRouteNameException + * @throws Exception\InvalidRoutePathsException */ public function __construct($name, $routeArray) { + if (!$name) { + throw new InvalidRouteNameException(); + } $this->name = $name; - $this->route = $routeArray['route']; + + //route is optional + $this->route = isset($routeArray['route']) ? $routeArray['route'] : ''; + + if (!isset($routeArray['paths']) || empty($routeArray['paths'])) { + throw new InvalidRoutePathsException(); + } $this->paths = $routeArray['paths']; + //params are optional $this->params = isset($routeArray['params']) ? $routeArray['params'] : array(); // encode auth array if exists - phalcon application cannot handle arrays as param here diff --git a/src/Mvc/Router/Route/BaseRoute.php b/src/Mvc/Router/Route/BaseRoute.php index bd0d01b..cb04bd7 100644 --- a/src/Mvc/Router/Route/BaseRoute.php +++ b/src/Mvc/Router/Route/BaseRoute.php @@ -18,7 +18,7 @@ /** * Class DefaultRoute - * Base route type, useful for rules which should be overwritten by any default or static route + * Base route type * * @package Vegas\Mvc\Router\Route */ diff --git a/src/Mvc/Router/Route/DefaultRoute.php b/src/Mvc/Router/Route/DefaultRoute.php index 8d91d61..0b2619f 100644 --- a/src/Mvc/Router/Route/DefaultRoute.php +++ b/src/Mvc/Router/Route/DefaultRoute.php @@ -19,6 +19,7 @@ /** * Class DefaultRoute * Default route type. + * @see http://docs.phalconphp.com/pl/latest/reference/routing.html#setting-default-paths * * @package Vegas\Mvc\Router\Route */ @@ -30,9 +31,11 @@ class DefaultRoute implements RouteInterface */ public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) { - $router - ->add($route->getRoute(), $route->getPaths()) - ->setName($route->getName()) - ->setHostName($route->getParam('hostname')); + $router->setDefaults(array_merge( + $route->getPaths(), + [ + 'params' => $route->getParams() + ] + )); } } \ No newline at end of file diff --git a/src/Mvc/Router/Route/NotfoundRoute.php b/src/Mvc/Router/Route/NotfoundRoute.php index a9af8f5..99b8846 100644 --- a/src/Mvc/Router/Route/NotfoundRoute.php +++ b/src/Mvc/Router/Route/NotfoundRoute.php @@ -20,6 +20,7 @@ * Class NotfoundRoute * * When none of the routes specified in the router are matched, the following route will be use + * @see http://docs.phalconphp.com/pl/latest/reference/routing.html#not-found-paths * * @package Vegas\Mvc\Router\Route */ diff --git a/tests/Mvc/Router/Adapter/StandardTest.php b/tests/Mvc/Router/Adapter/StandardTest.php index df453bc..7515f22 100644 --- a/tests/Mvc/Router/Adapter/StandardTest.php +++ b/tests/Mvc/Router/Adapter/StandardTest.php @@ -12,7 +12,6 @@ namespace Vegas\Tests\Mvc\Router\Adapter; - use Phalcon\DI; class StandardTest extends \PHPUnit_Framework_TestCase @@ -24,6 +23,12 @@ public function testShouldAdapterInstanceImplementRouterInterface() $this->assertInstanceOf('\Phalcon\Mvc\RouterInterface', $router); } + public function testShouldNotKeepDefaultRoutes() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $this->assertEmpty($router->getRoutes()); + } + public function testShouldRemoveExtraSlashes() { $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); diff --git a/tests/Mvc/Router/Route/BaseRouteTest.php b/tests/Mvc/Router/Route/BaseRouteTest.php new file mode 100644 index 0000000..3abbad4 --- /dev/null +++ b/tests/Mvc/Router/Route/BaseRouteTest.php @@ -0,0 +1,84 @@ + + * @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\Mvc\Router\Route; + +use Phalcon\DI; +use Vegas\Mvc\Router\Route\BaseRoute; +use Vegas\Mvc\Router\Route; + +class BaseRouteTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldAddBaseRouteToRouter() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('test', [ + 'route' => '/test', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Test', + 'action' => 'test' + ], + 'params' => [ + 'param' => 'value', + 'hostname' => 'test.localhost' + ] + ]); + + $baseRoute = new BaseRoute(); + $baseRoute->add($router, $route); + + $this->assertNotEmpty($router->getRoutes()); + $this->assertNotNull($router->getRouteByName('test')); + $testRoute = $router->getRouteByName('test'); + $this->assertInstanceOf('\Phalcon\Mvc\Router\RouteInterface', $testRoute); + $this->assertEquals('test.localhost', $testRoute->getHostname()); + $this->assertSame(['module'=>'Test','controller'=>'Test','action'=>'test'], $testRoute->getPaths()); + $this->assertEquals('/test', $testRoute->getPattern()); + } + + public function testShouldMatchBaseRoute() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('test', [ + 'route' => '/test', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Test', + 'action' => 'test' + ], + 'params' => [ + 'param' => 'value', + 'hostname' => 'test.localhost' + ] + ]); + + $baseRoute = new BaseRoute(); + $baseRoute->add($router, $route); + + //note. the HTTP_HOST is empty atm + $router->handle('/test'); + $this->assertNull($router->getMatchedRoute()); + + //hardcode the HTTP_HOST + $_SERVER['HTTP_HOST'] = 'test.localhost'; + $router->handle('/test'); + $matchedRoute = $router->getMatchedRoute(); + + $this->assertNotNull($matchedRoute); + $this->assertEquals($route->getName(), $matchedRoute->getName()); + $this->assertEquals($route->getRoute(), $matchedRoute->getPattern()); + } +} \ No newline at end of file diff --git a/tests/Mvc/Router/Route/DefaultRouteTest.php b/tests/Mvc/Router/Route/DefaultRouteTest.php new file mode 100644 index 0000000..8a1a864 --- /dev/null +++ b/tests/Mvc/Router/Route/DefaultRouteTest.php @@ -0,0 +1,83 @@ + + * @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\Mvc\Router\Route; + +use Phalcon\DI; +use Vegas\Mvc\Router\Route; + +class DefaultRouteTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldAddDefaultRouteToRouter() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('def', [ + 'paths' => [ + 'module' => 'Def', + 'controller' => 'Def', + 'action' => 'def' + ], + 'params' => [ + 'p1' => 'v1' + ] + ]); + + $defaultRoute = new Route\DefaultRoute(); + $defaultRoute->add($router, $route); + + $this->assertNotEmpty($router->getDefaults()); + $this->assertArrayHasKey('module', $router->getDefaults()); + $this->assertArrayHasKey('controller', $router->getDefaults()); + $this->assertArrayHasKey('action', $router->getDefaults()); + $this->assertArrayHasKey('params', $router->getDefaults()); + $this->assertEquals('Def', $router->getDefaults()['module']); + $this->assertEquals('Def', $router->getDefaults()['controller']); + $this->assertEquals('def', $router->getDefaults()['action']); + $this->assertEquals('v1', $router->getDefaults()['params']['p1']); + } + + public function testShouldUseDefaultActionForRouteWithoutActionInPaths() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('def', [ + 'paths' => [ + 'module' => 'Def', + 'controller' => 'Def', + 'action' => 'def' + ] + ]); + + $defaultRoute = new Route\DefaultRoute(); + $defaultRoute->add($router, $route); + + $testRoute = new Route('test', [ + 'route' => '/test', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Test' + ], + 'params' => [ + 'param' => 'value' + ] + ]); + $baseRoute = new Route\BaseRoute(); + $baseRoute->add($router, $testRoute); + + $router->handle('/test'); + $this->assertEquals('def', $router->getActionName()); + $this->assertEquals('Test', $router->getControllerName()); + $this->assertEquals('Test', $router->getModuleName()); + } +} \ No newline at end of file diff --git a/tests/Mvc/Router/Route/NotFoundRouteTest.php b/tests/Mvc/Router/Route/NotFoundRouteTest.php new file mode 100644 index 0000000..5b28342 --- /dev/null +++ b/tests/Mvc/Router/Route/NotFoundRouteTest.php @@ -0,0 +1,69 @@ + + * @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\Mvc\Router\Route; + +use Phalcon\DI; +use Vegas\Mvc\Router\Route\BaseRoute; +use Vegas\Mvc\Router\Route; + +class NotFoundRouteTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldAddNotFoundRouteToRouter() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('notfound', [ + 'paths' => [ + 'module' => 'Not', + 'controller' => 'Found', + 'action' => 'error404' + ] + ]); + + $notFoundRoute = new Route\NotfoundRoute(); + $notFoundRoute->add($router, $route); + + $reflectionObject = new \ReflectionObject($router); + $notFoundProperty = $reflectionObject->getProperty('_notFoundPaths'); + $notFoundProperty->setAccessible(true); + $routerNotFoundPaths = $notFoundProperty->getValue($router); + + $this->assertEquals($route->getPaths()['module'], $routerNotFoundPaths['module']); + $this->assertEquals($route->getPaths()['controller'], $routerNotFoundPaths['controller']); + $this->assertEquals($route->getPaths()['action'], $routerNotFoundPaths['action']); + } + + public function testShouldUseNotFoundRoute() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('notfound', [ + 'paths' => [ + 'module' => 'Not', + 'controller' => 'Found', + 'action' => 'error404' + ] + ]); + + $notFoundRoute = new Route\NotfoundRoute(); + $notFoundRoute->add($router, $route); + + $router->handle('/test'); + $matchedRoute = $router->getMatchedRoute(); + $this->assertNull($matchedRoute); + $this->assertEquals($route->getPaths()['module'], $router->getModuleName()); + $this->assertEquals($route->getPaths()['controller'], $router->getControllerName()); + $this->assertEquals($route->getPaths()['action'], $router->getActionName()); + } +} \ No newline at end of file From 95f634d15608413a063341607c39c01e78d68475 Mon Sep 17 00:00:00 2001 From: slawek Date: Fri, 5 Dec 2014 14:36:52 +0100 Subject: [PATCH 22/35] Router tests; --- src/Mvc/Router/Route/RestRoute.php | 6 +- tests/Mvc/Router/Route/BaseRouteTest.php | 11 +- tests/Mvc/Router/Route/DefaultRouteTest.php | 17 +- tests/Mvc/Router/Route/RestRouteTest.php | 138 +++++++++ tests/Mvc/Router/Route/StaticRouteTest.php | 77 +++++ tests/Mvc/Router/RouteTest.php | 27 ++ tests/Mvc/RouterTest.php | 303 ++++++++++++-------- 7 files changed, 457 insertions(+), 122 deletions(-) create mode 100644 tests/Mvc/Router/Route/RestRouteTest.php create mode 100644 tests/Mvc/Router/Route/StaticRouteTest.php diff --git a/src/Mvc/Router/Route/RestRoute.php b/src/Mvc/Router/Route/RestRoute.php index 383ac3a..e00e5c3 100644 --- a/src/Mvc/Router/Route/RestRoute.php +++ b/src/Mvc/Router/Route/RestRoute.php @@ -42,13 +42,15 @@ public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) //add routes with http method //for each action new route is adding with specified HTTP Method. foreach ($actions as $actionRoute => $actionMethods) { - if ($actionRoute == '/') $actionRoute = ''; + if ($actionRoute == '/') { + $actionRoute = ''; + } foreach ($actionMethods as $action => $method) { $paths = $route->getPaths(); $paths['action'] = $action; $newRoute = $router->add($route->getRoute() . $actionRoute, $paths)->via($method); - $newRoute->setName($route->getName() . '/' . $action); + $newRoute->setName($route->getName() . $actionRoute . '/' . $action); $newRoute->setHostName($route->getParam('hostname')); } } diff --git a/tests/Mvc/Router/Route/BaseRouteTest.php b/tests/Mvc/Router/Route/BaseRouteTest.php index 3abbad4..86c7a2b 100644 --- a/tests/Mvc/Router/Route/BaseRouteTest.php +++ b/tests/Mvc/Router/Route/BaseRouteTest.php @@ -27,7 +27,7 @@ public function testShouldAddBaseRouteToRouter() 'route' => '/test', 'paths' => [ 'module' => 'Test', - 'controller' => 'Test', + 'controller' => 'TestCon', 'action' => 'test' ], 'params' => [ @@ -44,7 +44,9 @@ public function testShouldAddBaseRouteToRouter() $testRoute = $router->getRouteByName('test'); $this->assertInstanceOf('\Phalcon\Mvc\Router\RouteInterface', $testRoute); $this->assertEquals('test.localhost', $testRoute->getHostname()); - $this->assertSame(['module'=>'Test','controller'=>'Test','action'=>'test'], $testRoute->getPaths()); + $this->assertEquals('Test', $testRoute->getPaths()['module']); + $this->assertEquals('TestCon', $testRoute->getPaths()['controller']); + $this->assertEquals('test', $testRoute->getPaths()['action']); $this->assertEquals('/test', $testRoute->getPattern()); } @@ -56,7 +58,7 @@ public function testShouldMatchBaseRoute() 'route' => '/test', 'paths' => [ 'module' => 'Test', - 'controller' => 'Test', + 'controller' => 'TestCon', 'action' => 'test' ], 'params' => [ @@ -79,6 +81,9 @@ public function testShouldMatchBaseRoute() $this->assertNotNull($matchedRoute); $this->assertEquals($route->getName(), $matchedRoute->getName()); + $this->assertEquals($matchedRoute->getPaths()['module'], $route->getPaths()['module']); + $this->assertEquals($matchedRoute->getPaths()['controller'], $route->getPaths()['controller']); + $this->assertEquals($matchedRoute->getPaths()['action'], $route->getPaths()['action']); $this->assertEquals($route->getRoute(), $matchedRoute->getPattern()); } } \ No newline at end of file diff --git a/tests/Mvc/Router/Route/DefaultRouteTest.php b/tests/Mvc/Router/Route/DefaultRouteTest.php index 8a1a864..4ea09b2 100644 --- a/tests/Mvc/Router/Route/DefaultRouteTest.php +++ b/tests/Mvc/Router/Route/DefaultRouteTest.php @@ -54,7 +54,7 @@ public function testShouldUseDefaultActionForRouteWithoutActionInPaths() $route = new Route('def', [ 'paths' => [ 'module' => 'Def', - 'controller' => 'Def', + 'controller' => 'DefController', 'action' => 'def' ] ]); @@ -66,7 +66,7 @@ public function testShouldUseDefaultActionForRouteWithoutActionInPaths() 'route' => '/test', 'paths' => [ 'module' => 'Test', - 'controller' => 'Test' + 'controller' => 'TestCon' ], 'params' => [ 'param' => 'value' @@ -76,8 +76,15 @@ public function testShouldUseDefaultActionForRouteWithoutActionInPaths() $baseRoute->add($router, $testRoute); $router->handle('/test'); - $this->assertEquals('def', $router->getActionName()); - $this->assertEquals('Test', $router->getControllerName()); - $this->assertEquals('Test', $router->getModuleName()); + + $matchedRoute = $router->getMatchedRoute(); + + $this->assertEquals($route->getPaths()['action'], $router->getActionName()); + $this->assertEquals($testRoute->getPaths()['controller'], $router->getControllerName()); + $this->assertEquals($testRoute->getPaths()['module'], $router->getModuleName()); + $this->assertEquals($testRoute->getRoute(), $matchedRoute->getPattern()); + $this->assertEquals($testRoute->getPaths()['controller'], $matchedRoute->getPaths()['controller']); + $this->assertEquals($testRoute->getPaths()['module'], $matchedRoute->getPaths()['module']); + $this->assertArrayNotHasKey('action', $testRoute->getPaths()); } } \ No newline at end of file diff --git a/tests/Mvc/Router/Route/RestRouteTest.php b/tests/Mvc/Router/Route/RestRouteTest.php new file mode 100644 index 0000000..d351e4a --- /dev/null +++ b/tests/Mvc/Router/Route/RestRouteTest.php @@ -0,0 +1,138 @@ + + * @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\Mvc\Router\Route; + +use Phalcon\DI; +use Phalcon\Mvc\Router\RouteInterface; +use Vegas\Http\Method; +use Vegas\Mvc\Router\Route\BaseRoute; +use Vegas\Mvc\Router\Route; + +class RestRouteTest extends \PHPUnit_Framework_TestCase +{ + /** + * Helper function for handling specified URI + * + * @param $router + * @param $method + * @param $uri + * @return \Phalcon\Mvc\Router\Route + */ + private function handleUri($router, $method, $uri) { + $_SERVER['REQUEST_METHOD'] = $method; + $router->handle($uri); + + return $router->getMatchedRoute(); + } + + public function testShouldAddRestRouteToRouter() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('rest', [ + 'route' => '/rest', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Rest' + ], + 'type' => 'rest', + 'params' => [ + 'actions' => [ + '/' => [ + 'index' => Method::GET, + 'create' => Method::POST + ] + ] + ] + ]); + + $restRoute = new Route\RestRoute(); + $restRoute->add($router, $route); + + $this->assertNotEmpty($router->getRoutes()); + $this->assertFalse($router->getRouteByName('rest')); + + $this->assertEquals(Method::GET, $router->getRouteByName('rest/index')->getHttpMethods()); + $this->assertEquals(Method::POST, $router->getRouteByName('rest/create')->getHttpMethods()); + } + + public function testShouldMatchRestRoute() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('rest', [ + 'route' => '/rest', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Rest' + ], + 'type' => 'rest', + 'params' => [ + 'actions' => [ + '/' => [ + 'index' => Method::GET, + 'create' => Method::POST + ] + ] + ] + ]); + + $restRoute = new Route\RestRoute(); + $restRoute->add($router, $route); + + $indexRoute = $this->handleUri($router, 'GET', '/rest'); + $this->assertNotNull($indexRoute); + $this->assertEquals('Test', $indexRoute->getPaths()['module']); + $this->assertEquals('Rest', $indexRoute->getPaths()['controller']); + $this->assertEquals('index', $indexRoute->getPaths()['action']); + $this->assertEquals('/rest', $indexRoute->getPattern()); + + $createRoute = $this->handleUri($router, 'POST', '/rest'); + $this->assertNotNull($createRoute); + $this->assertEquals('Test', $createRoute->getPaths()['module']); + $this->assertEquals('Rest', $createRoute->getPaths()['controller']); + $this->assertEquals('create', $createRoute->getPaths()['action']); + $this->assertEquals('/rest', $createRoute->getPattern()); + } + + public function testShouldNotMatchRestRouteWithWrongRequestMethod() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('rest', [ + 'route' => '/rest', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Rest' + ], + 'type' => 'rest', + 'params' => [ + 'actions' => [ + '/' => [ + 'index' => Method::GET, + 'create' => Method::POST + ] + ] + ] + ]); + + $restRoute = new Route\RestRoute(); + $restRoute->add($router, $route); + + $this->assertNull($this->handleUri($router, 'PUT', '/rest')); + $this->assertNull($this->handleUri($router, 'DELETE', '/rest')); + $this->assertNull($this->handleUri($router, 'PATCH', '/rest')); + $this->assertNull($this->handleUri($router, 'OPTIONS', '/rest')); + $this->assertNull($this->handleUri($router, 'HEAD', '/rest')); + } +} \ No newline at end of file diff --git a/tests/Mvc/Router/Route/StaticRouteTest.php b/tests/Mvc/Router/Route/StaticRouteTest.php new file mode 100644 index 0000000..9ee209b --- /dev/null +++ b/tests/Mvc/Router/Route/StaticRouteTest.php @@ -0,0 +1,77 @@ + + * @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\Mvc\Router\Route; + +use Phalcon\DI; +use Vegas\Mvc\Router\Route\BaseRoute; +use Vegas\Mvc\Router\Route; + +class StaticRouteTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldAddStaticRouteToRouter() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('static', [ + 'route' => '/static', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Static', + 'action' => 'test' + ], + 'type' => 'static' + ]); + + $staticRoute = new Route\StaticRoute(); + $staticRoute->add($router, $route); + + $this->assertNotEmpty($router->getRoutes()); + $this->assertNotNull($router->getRouteByName('static')); + $testRoute = $router->getRouteByName('static'); + $this->assertInstanceOf('\Phalcon\Mvc\Router\RouteInterface', $testRoute); + $this->assertEquals('Test', $testRoute->getPaths()['module']); + $this->assertEquals('Static', $testRoute->getPaths()['controller']); + $this->assertEquals('test', $testRoute->getPaths()['action']); + $this->assertEquals('/static', $testRoute->getPattern()); + } + + public function testShouldMatchStaticRoute() + { + $router = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + + $route = new Route('static', [ + 'route' => '/static', + 'paths' => [ + 'module' => 'Test', + 'controller' => 'Static', + 'action' => 'test' + ], + 'type' => 'static' + ]); + + $staticRoute = new Route\StaticRoute(); + $staticRoute->add($router, $route); + + $router->handle('/static'); + + $matchedRoute = $router->getMatchedRoute(); + $this->assertNotNull($matchedRoute); + + $this->assertEquals($matchedRoute->getName(), $route->getName()); + $this->assertEquals($matchedRoute->getPaths()['module'], $route->getPaths()['module']); + $this->assertEquals($matchedRoute->getPaths()['controller'], $route->getPaths()['controller']); + $this->assertEquals($matchedRoute->getPaths()['action'], $route->getPaths()['action']); + $this->assertEquals($matchedRoute->getPattern(), $route->getRoute()); + } +} \ No newline at end of file diff --git a/tests/Mvc/Router/RouteTest.php b/tests/Mvc/Router/RouteTest.php index 1d0d10e..7797498 100644 --- a/tests/Mvc/Router/RouteTest.php +++ b/tests/Mvc/Router/RouteTest.php @@ -47,5 +47,32 @@ public function testShouldMapConstructorParametersToClassProperties() $this->assertEquals('value_3', $route->getParam('param_3')); $this->assertSame(json_encode(['auth', 'authAdmin']), $route->getPaths()['auth']); $this->assertEquals('/url', $route->getRoute()); + + $this->assertInternalType('array', $route->getParams()); + $this->assertInternalType('string', $route->getRoute()); + $this->assertInternalType('array', $route->getPaths()); + $this->assertInternalType('string', $route->getName()); + } + + public function testShouldThrowExceptionForInvalidName() + { + $exception = null; + try { + new Route(false, []); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Mvc\Router\Exception\InvalidRouteNameException', $exception); + } + + public function testShouldThrowExceptionForInvalidPaths() + { + $exception = null; + try { + new Route('test', []); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Mvc\Router\Exception\InvalidRoutePathsException', $exception); } } \ No newline at end of file diff --git a/tests/Mvc/RouterTest.php b/tests/Mvc/RouterTest.php index d49e3b5..3e7011e 100644 --- a/tests/Mvc/RouterTest.php +++ b/tests/Mvc/RouterTest.php @@ -25,6 +25,12 @@ public function setUp() } private $testRoutes = array( + 'default' => [ + 'paths' => [ + 'action' => 'index' + ], + 'type' => 'default' + ], 'statictest' => array( 'route' => '/static/qwerty', 'paths' => array( @@ -40,7 +46,7 @@ public function setUp() 'controller' => 'nonstatictest', 'action' => 'test' ), - 'type' => 'default', + 'type' => 'base', 'params' => array() ), 'test' => array( @@ -78,6 +84,7 @@ public function setUp() 'create' => Method::POST ), '/{id}' => array( + 'index' => Method::GET, 'show' => Method::GET, 'update' => Method::PUT, 'delete' => Method::DELETE @@ -112,50 +119,44 @@ public function setUp() ) ); - public function testRouteDefinition() + public function testShouldContainGivenRouterAdapter() { $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); - $router->addRoutes($this->testRoutes); - $route = new Router\Route('test', end($this->testRoutes)); - $this->assertInternalType('array', $route->getParams()); - $this->assertInternalType('string', $route->getRoute()); - $this->assertInternalType('array', $route->getPaths()); - $this->assertInternalType('string', $route->getName()); + $this->assertInstanceOf('\Vegas\Mvc\Router\Adapter\Standard', $router->getRouter()); + $this->assertSame($routerAdapter, $router->getRouter()); + } + public function testShouldAddRoutes() + { + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); + $router->addRoutes($this->testRoutes); $router->setup(); $this->assertNotEmpty($router->getRouter()->getRoutes()); + $this->assertNotEmpty($router->getRouter()->getRouteByName('statictest')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('nonstatictest')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('test')); $this->assertNotEmpty($router->getRouter()->getRouteByName('articles/index')); $this->assertNotEmpty($router->getRouter()->getRouteByName('articles/create')); - $this->assertEmpty($router->getRouter()->getRouteByName('articles/update')); - $this->assertEmpty($router->getRouter()->getRouteByName('articles/delete')); - $this->assertEmpty($router->getRouter()->getRouteByName('articles/show')); - - $this->assertEquals(Method::GET, $router->getRouter()->getRouteByName('articles/index')->getHttpMethods()); - $this->assertEquals(Method::POST, $router->getRouter()->getRouteByName('articles/create')->getHttpMethods()); - - $this->assertNotEmpty($router->getRouter()->getRouteByName('products/create')); - $this->assertNotEmpty($router->getRouter()->getRouteByName('products/delete')); - $this->assertNotEmpty($router->getRouter()->getRouteByName('products/show')); $this->assertNotEmpty($router->getRouter()->getRouteByName('products/index')); - $this->assertNotEmpty($router->getRouter()->getRouteByName('products/update')); - - $this->assertEquals(Method::POST, $router->getRouter()->getRouteByName('products/create')->getHttpMethods()); - $this->assertEquals(Method::DELETE, $router->getRouter()->getRouteByName('products/delete')->getHttpMethods()); - $this->assertEquals(Method::GET, $router->getRouter()->getRouteByName('products/show')->getHttpMethods()); - $this->assertEquals(Method::GET, $router->getRouter()->getRouteByName('products/index')->getHttpMethods()); - $this->assertEquals(Method::PUT, $router->getRouter()->getRouteByName('products/update')->getHttpMethods()); - - //checks if static route is added in the end - $routes = $router->getRouter()->getRoutes(); - $lastRoute = $routes[count($routes)-1]; - $this->assertEquals($lastRoute->getName(), 'statictest'); + $this->assertNotEmpty($router->getRouter()->getRouteByName('products/create')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('products/{id}/index')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('products/{id}/show')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('products/{id}/update')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('products/{id}/delete')); + $this->assertEmpty($router->getRouter()->getRouteByName('galleries')); + $this->assertEmpty($router->getRouter()->getRouteByName('notfound')); + $this->assertEmpty($router->getRouter()->getRouteByName('default')); + $this->assertNotEmpty($router->getRouter()->getRouteByName('dashboard')); + } - //handle non-existing route type + public function testShouldThrowExceptionForInvalidRouteType() + { $failRoute = array('fake' => array( 'route' => 'fakeurl', 'paths' => array( @@ -165,12 +166,20 @@ public function testRouteDefinition() 'params' => array() )); - $this->setExpectedException('\Vegas\Mvc\Router\Exception\InvalidRouteTypeException'); - $router->addRoute($failRoute); - $router->setup(); + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); + + $exception = null; + try { + $router->addRoute($failRoute); + $router->setup(); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Mvc\Router\Exception\InvalidRouteTypeException', $exception); } - public function testRouteMatching() + public function testShouldMatchStaticRoute() { $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); @@ -178,131 +187,201 @@ public function testRouteMatching() $router->setup(); - $testCorrectRoutes = array( - '/static/test', - '/test/edit' - ); - $defaultRouter = $router->getRouter(); - foreach ($testCorrectRoutes as $url) { - $defaultRouter->handle($url); - $this->assertTrue($defaultRouter->wasMatched()); - } - /** - * Helper function for handling specified URI - * - * @param $method - * @param $uri - */ - $handleUri = function($method, $uri) use ($defaultRouter) { - $this->setRequestMethod($method); - $defaultRouter->handle($uri); - $this->assertTrue($defaultRouter->wasMatched()); - }; - - $handleUri(Method::GET, '/products/123'); - $handleUri(Method::PUT, '/products/123'); - $handleUri(Method::DELETE, '/products/123'); - - $handleUri(Method::POST, '/products/'); - $handleUri(Method::GET, '/products/'); - - $handleUri(Method::GET, '/articles'); - $handleUri(Method::POST, '/articles'); - - //non existing routes - $this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException'); - $handleUri(Method::POST, '/products/123'); - $handleUri(Method::DELETE, '/products'); - $handleUri(Method::PUT, '/products'); - $handleUri(Method::PUT, '/products/123'); - $handleUri(Method::DELETE, '/articles/1234'); - $handleUri(Method::PUT, '/articles/1234'); - $handleUri(Method::PUT, '/articles'); - $handleUri(Method::GET, '/articles/1234'); + $staticRoute = $defaultRouter->getRouteByName('statictest'); + + $defaultRouter->handle('/static/qwerty'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($staticRoute->getName(), $matchedRoute->getName()); + $this->assertEquals('statictest', $matchedRoute->getPaths()['controller']); + $this->assertEquals('test', $matchedRoute->getPaths()['action']); + $this->assertEquals($staticRoute->getPattern(), $matchedRoute->getPattern()); + + $nonStaticRoute = $defaultRouter->getRouteByName('nonstatictest'); + + $defaultRouter->handle('/static/asdfgh'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($nonStaticRoute->getName(), $matchedRoute->getName()); + $this->assertEquals('nonstatictest', $matchedRoute->getPaths()['controller']); + $this->assertEquals('test', $matchedRoute->getPaths()['action']); + $this->assertEquals($nonStaticRoute->getPattern(), $matchedRoute->getPattern()); } - public function testStaticRoutes() + public function testShouldAddModuleRoutes() { $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); - $router->addRoutes($this->testRoutes); + $modules = ModuleLoader::dump(DI::getDefault()); + foreach ($modules as $module) { + $router->addModuleRoutes($module); + } $router->setup(); $defaultRouter = $router->getRouter(); + $defaultRouter->handle('/test/fake/test'); - $defaultRouter->handle('/static/qwerty'); - $this->assertNotEmpty($defaultRouter->getMatchedRoute()); - $matchedRoute = $defaultRouter->getMatchedRoute(); - $paths = $matchedRoute->getPaths(); - $this->assertEquals('statictest', $paths['controller']); + $route = $defaultRouter->getRouteByName('testfake'); - $defaultRouter->handle('/static/asdfgh'); - $this->assertNotEmpty($defaultRouter->getMatchedRoute()); $matchedRoute = $defaultRouter->getMatchedRoute(); - $paths = $matchedRoute->getPaths(); - $this->assertEquals('nonstatictest', $paths['controller']); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($route->getPaths()['controller'], $defaultRouter->getControllerName()); + $this->assertEquals($route->getPaths()['action'], $defaultRouter->getActionName()); + $this->assertEquals($route->getPaths()['module'], $defaultRouter->getModuleName()); + $this->assertEquals($route->getName(), $matchedRoute->getName()); + $this->assertEquals($route->getPaths()['action'], $matchedRoute->getPaths()['action']); + $this->assertEquals($route->getPaths()['controller'], $matchedRoute->getPaths()['controller']); + $this->assertEquals($route->getPaths()['module'], $matchedRoute->getPaths()['module']); } - public function testHostNameConstraints() + public function testShouldMatchRouteWithHostNameResolvedFromHttpHost() { + $_SERVER['HTTP_HOST'] = 'test.vegas.dev'; + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); - $router->addRoutes($this->testRoutes); + $router->addRoutes([ + 'test' => [ + 'route' => '/', + 'paths' => [ + 'module' => 'Mod', + 'controller' => 'Con', + 'action' => 'Act' + ] + ] + ]); $router->setup(); $defaultRouter = $router->getRouter(); + $route = $defaultRouter->getRouteByName('test'); - $_SERVER['HTTP_HOST'] = 'test.vegas.dev'; $defaultRouter->handle('/'); - $this->assertNotEmpty($defaultRouter->getMatchedRoute()); $matchedRoute = $defaultRouter->getMatchedRoute(); - $paths = $matchedRoute->getPaths(); - $this->assertEquals('dashboard', $paths['controller']); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($route->getPaths()['module'], $matchedRoute->getPaths()['module']); + $this->assertEquals($route->getPaths()['controller'], $matchedRoute->getPaths()['controller']); + $this->assertEquals($route->getPaths()['action'], $matchedRoute->getPaths()['action']); } - public function testModuleRoutes() + public function testShouldMatchRouteWithHostNameResolvedFromApplicationConfig() { + $_SERVER['HTTP_HOST'] = 'test.vegas.dev'; + DI::getDefault()->get('config')->application->hostname = 'test.vegas.dev'; + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); - - $modules = ModuleLoader::dump(DI::getDefault()); - foreach ($modules as $module) { - $router->addModuleRoutes($module); - } + $router->addRoutes([ + 'test' => [ + 'route' => '/', + 'paths' => [ + 'module' => 'Mod', + 'controller' => 'Con', + 'action' => 'Act' + ] + ] + ]); $router->setup(); $defaultRouter = $router->getRouter(); + $route = $defaultRouter->getRouteByName('test'); - $defaultRouter->handle('/test/fake/test'); - - $this->assertNotEmpty($defaultRouter->getMatchedRoute()); - $this->assertEquals('Backend\Fake', $defaultRouter->getControllerName()); - $this->assertEquals('test', $defaultRouter->getActionName()); + $defaultRouter->handle('/'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($route->getPaths()['module'], $matchedRoute->getPaths()['module']); + $this->assertEquals($route->getPaths()['controller'], $matchedRoute->getPaths()['controller']); + $this->assertEquals($route->getPaths()['action'], $matchedRoute->getPaths()['action']); } - public function testNotFound() + public function testShouldNotMatchRouteWithHostNameResolvedFromHttpHost() { + $_SERVER['HTTP_HOST'] = 'test.vegas.dev'; + DI::getDefault()->get('config')->application->hostname = null; + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); + $router->addRoutes([ + 'test' => [ + 'route' => '/', + 'paths' => [ + 'module' => 'Mod', + 'controller' => 'Con', + 'action' => 'Act' + ], + 'params' => [ + 'hostname' => 'test2.vegas.dev' + ] + ] + ]); - $router->addRoutes($this->testRoutes); $router->setup(); $defaultRouter = $router->getRouter(); - $defaultRouter->handle('/fake-route-ever/123-5/454353'); - $this->assertFalse($defaultRouter->wasMatched()); - $this->assertEquals('error', $defaultRouter->getControllerName()); - $this->assertEquals('error404', $defaultRouter->getActionName()); + $defaultRouter->handle('/'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertEmpty($matchedRoute);; } - private function setRequestMethod($method) + public function testShouldNotMatchRouteWithHostNameResolvedFromApplicationConfig() + { + $_SERVER['HTTP_HOST'] = 'test.vegas.dev'; + DI::getDefault()->get('config')->application->hostname = 'test2.vegas.dev'; + + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); + $router->addRoutes([ + 'test' => [ + 'route' => '/', + 'paths' => [ + 'module' => 'Mod', + 'controller' => 'Con', + 'action' => 'Act' + ] + ] + ]); + + $router->setup(); + + $defaultRouter = $router->getRouter(); + + $defaultRouter->handle('/'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertEmpty($matchedRoute);; + } + public function testShouldMatchRouteWithEmptyHostName() { - $_SERVER['REQUEST_METHOD'] = $method; + $_SERVER['HTTP_HOST'] = null; + DI::getDefault()->get('config')->application->hostname = null; + + $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); + $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); + $router->addRoutes([ + 'test' => [ + 'route' => '/', + 'paths' => [ + 'module' => 'Mod', + 'controller' => 'Con', + 'action' => 'Act' + ] + ] + ]); + + $router->setup(); + + $defaultRouter = $router->getRouter(); + $route = $defaultRouter->getRouteByName('test'); + + $defaultRouter->handle('/'); + $matchedRoute = $defaultRouter->getMatchedRoute(); + $this->assertNotEmpty($matchedRoute); + $this->assertEquals($route->getPaths()['module'], $matchedRoute->getPaths()['module']); + $this->assertEquals($route->getPaths()['controller'], $matchedRoute->getPaths()['controller']); + $this->assertEquals($route->getPaths()['action'], $matchedRoute->getPaths()['action']); } } \ No newline at end of file From 8c60a9f408c9a267755d7f53cfe8b892d1f73cb3 Mon Sep 17 00:00:00 2001 From: slawek Date: Fri, 5 Dec 2014 16:11:40 +0100 Subject: [PATCH 23/35] Refactoring; Db tests; --- src/Application/Bootstrap.php | 2 - src/BootstrapInterface.php | 4 +- .../Decorator/Helper/MappingHelperTrait.php | 28 ++++++++- .../Decorator/Helper/WriteAttributesTrait.php | 9 +++ src/Mvc/Module/Loader.php | 61 +++++++++++++------ .../Adapter/Decorator/ModelAbstractTest.php | 11 +++- tests/Db/MappingTest.php | 33 +++++++++- tests/Mvc/Module/LoaderTest.php | 2 +- tests/Util/FileWriterTest.php | 59 ++++++++++++++++++ 9 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 tests/Util/FileWriterTest.php diff --git a/src/Application/Bootstrap.php b/src/Application/Bootstrap.php index c281044..3e6b2bf 100644 --- a/src/Application/Bootstrap.php +++ b/src/Application/Bootstrap.php @@ -183,8 +183,6 @@ protected function initRoutes() /** * Registers default dispatcher - * - * @param $di */ protected function initDispatcher() { diff --git a/src/BootstrapInterface.php b/src/BootstrapInterface.php index 99b64be..b47f9fe 100644 --- a/src/BootstrapInterface.php +++ b/src/BootstrapInterface.php @@ -19,8 +19,8 @@ interface BootstrapInterface { /** - * Executes all bootstrap initializator - * This method can be overloaded to load own initializator. + * Executes all bootstrap initialization methods + * This method can be overloaded to load own initialization method. * @return mixed */ public function setup(); diff --git a/src/Db/Decorator/Helper/MappingHelperTrait.php b/src/Db/Decorator/Helper/MappingHelperTrait.php index aa1f36a..44f5fad 100644 --- a/src/Db/Decorator/Helper/MappingHelperTrait.php +++ b/src/Db/Decorator/Helper/MappingHelperTrait.php @@ -58,10 +58,36 @@ public function readMapped($name) if (!$this->hasMapping($name) && isset($this->mappings[$name])) { $this->addMapping($name, $this->mappings[$name]); } - $value = parent::readAttribute($name); + $value = $this->readAttribute($name); $value = $this->resolveMapping($name, $value); return $value; } + + /** + * @param $name + * @return mixed + */ + abstract public function hasMapping($name); + + /** + * @param $name + * @param $mapping + * @return mixed + */ + abstract public function addMapping($name, $mapping); + + /** + * @param $name + * @return mixed + */ + abstract public function readAttribute($name); + + /** + * @param $name + * @param $value + * @return mixed + */ + abstract public function resolveMapping($name, $value); } \ No newline at end of file diff --git a/src/Db/Decorator/Helper/WriteAttributesTrait.php b/src/Db/Decorator/Helper/WriteAttributesTrait.php index 250d167..76d7e83 100644 --- a/src/Db/Decorator/Helper/WriteAttributesTrait.php +++ b/src/Db/Decorator/Helper/WriteAttributesTrait.php @@ -29,4 +29,13 @@ public function writeAttributes($attributes) $this->writeAttribute($attribute, $value); } } + + /** + * Writes given value to attribute + * + * @param $name + * @param $value + * @return mixed + */ + abstract public function writeAttribute($name, $value); } \ No newline at end of file diff --git a/src/Mvc/Module/Loader.php b/src/Mvc/Module/Loader.php index ce4d426..b3b74e1 100644 --- a/src/Mvc/Module/Loader.php +++ b/src/Mvc/Module/Loader.php @@ -34,27 +34,38 @@ class Loader */ public static function dump(DiInterface $di) { - $modulesList = array(); + $modulesList = []; //extracts list of modules from module directory $config = $di->get('config'); $directoryIterator = new \DirectoryIterator($config->application->moduleDir); foreach ($directoryIterator as $moduleDir) { - if ($moduleDir->isDot()) continue; - $moduleSettingsFile = $moduleDir->getPathname() . DIRECTORY_SEPARATOR . self::MODULE_SETTINGS_FILE; + if ($moduleDir->isDot()) { + continue; + } + + $moduleSettingsFile = $moduleDir->getPathname() + . DIRECTORY_SEPARATOR + . self::MODULE_SETTINGS_FILE; if (!file_exists($moduleSettingsFile)) { continue; } - $modulesList[$moduleDir->getBasename()] = array( - 'className' => $moduleDir->getBasename() . '\\' . pathinfo(self::MODULE_SETTINGS_FILE, PATHINFO_FILENAME), + $modulesList[$moduleDir->getBasename()] = [ + 'className' => $moduleDir->getBasename() + . '\\' + . pathinfo(self::MODULE_SETTINGS_FILE, PATHINFO_FILENAME), 'path' => $moduleSettingsFile - ); + ]; } - $modulesList = self::dumpModulesFromVendor($modulesList); + self::dumpModulesFromVendor($modulesList); //saves generated array to php source file - FileWriter::write($config->application->configDir . 'modules.php', self::createFileContent($modulesList), true); + FileWriter::write( + $config->application->configDir . 'modules.php', + self::createFileContent($modulesList), + true + ); return $modulesList; } @@ -65,34 +76,46 @@ public static function dump(DiInterface $di) * @param $modulesList * @return mixed */ - private static function dumpModulesFromVendor(array $modulesList) + private static function dumpModulesFromVendor(array &$modulesList) { if (!file_exists(APP_ROOT.'/composer.json')) { return $modulesList; } - $fileContent = file_get_contents(APP_ROOT.DIRECTORY_SEPARATOR.'composer.json'); + $fileContent = file_get_contents(APP_ROOT . DIRECTORY_SEPARATOR . 'composer.json'); $json = json_decode($fileContent, true); - $vendorDir = realpath(APP_ROOT.(isset($json['config']['vendor-dir']) ? - DIRECTORY_SEPARATOR.$json['config']['vendor-dir'] : - DIRECTORY_SEPARATOR.'vendor')); + $vendorDir = realpath(APP_ROOT . + ( + isset($json['config']['vendor-dir']) + ? DIRECTORY_SEPARATOR . $json['config']['vendor-dir'] + : DIRECTORY_SEPARATOR.'vendor' + ) + ); - $vendorDir .= DIRECTORY_SEPARATOR.'vegas-cmf'; + $vendorDir .= DIRECTORY_SEPARATOR . 'vegas-cmf'; $directoryIterator = new \DirectoryIterator($vendorDir); foreach ($directoryIterator as $libDir) { - if ($libDir->isDot()) continue; - $moduleSettingsFile = $vendorDir. DIRECTORY_SEPARATOR . $libDir . DIRECTORY_SEPARATOR . 'module' . DIRECTORY_SEPARATOR .self::MODULE_SETTINGS_FILE; + if ($libDir->isDot()) { + continue; + } + //creates path to Module.php file + $moduleSettingsFile = implode(DIRECTORY_SEPARATOR, [ + $vendorDir, $libDir, 'module', self::MODULE_SETTINGS_FILE + ]); + if (!file_exists($moduleSettingsFile)) { continue; } $baseName = \Phalcon\Text::camelize($libDir->getBasename()); if (!isset($modulesList[$baseName])) { - $modulesList[$baseName] = array( - 'className' => $baseName . '\\' . pathinfo(self::MODULE_SETTINGS_FILE, PATHINFO_FILENAME), + $modulesList[$baseName] = [ + 'className' => $baseName + . '\\' + . pathinfo(self::MODULE_SETTINGS_FILE, PATHINFO_FILENAME), 'path' => $moduleSettingsFile - ); + ]; } } diff --git a/tests/Db/Adapter/Decorator/ModelAbstractTest.php b/tests/Db/Adapter/Decorator/ModelAbstractTest.php index b5bd47d..ceb839b 100644 --- a/tests/Db/Adapter/Decorator/ModelAbstractTest.php +++ b/tests/Db/Adapter/Decorator/ModelAbstractTest.php @@ -24,7 +24,6 @@ public function getSource() } } - class ModelAbstractTest extends \PHPUnit_Framework_TestCase { @@ -43,7 +42,13 @@ public static function setUpBeforeClass() ); } - public function testCreateRecord() + public static function tearDownAfterClass() + { + $di = DI::getDefault(); + $di->get('db')->execute('DROP TABLE IF EXISTS fake_table '); + } + + public function testShouldCreateRecord() { $data = array( 'title' => 'Title test', @@ -69,7 +74,7 @@ public function testCreateRecord() $this->assertTrue($fake->save()); } - public function testUpdateRecord() + public function testShouldUpdateRecord() { $fake = FakeModel::findFirst(); $fake->title = 'New title'; diff --git a/tests/Db/MappingTest.php b/tests/Db/MappingTest.php index 87c5d82..ca78c63 100644 --- a/tests/Db/MappingTest.php +++ b/tests/Db/MappingTest.php @@ -124,7 +124,34 @@ public function resolve(& $value) class MappingTest extends \PHPUnit_Framework_TestCase { - public function testMappingManager() + public static function setUpBeforeClass() + { + $data = array( + 'title' => 'Title test', + 'content' => 'Content test', + 'category_id' => rand(1000, 9999) + ); + + $fake = new FakeModel(); + $fake->writeAttributes($data); + $fake->save(); + + $fake = new Fake(); + $fake->writeAttributes($data); + $fake->save(); + } + + public static function tearDownAfterClass() + { + foreach (Fake::find() as $fake) { + $fake->delete(); + } + foreach (FakeModel::find() as $fake) { + $fake->delete(); + } + } + + public function testShouldAddMapperToMappingManager() { //define mappings $mappingManager = new MappingManager(); @@ -161,7 +188,7 @@ public function testMappingManager() $this->assertEquals(sprintf('Mapping class \'%s\' was not found', 'fake_mapping'), $m); } - public function testResolveCollectionMappings() + public function testShouldResolveCollectionMappings() { $mappingManager = new MappingManager(); $mappingManager->add(new Json()); @@ -229,7 +256,7 @@ public function testResolveCollectionMappings() $this->assertEquals($nonCamelText, $fakeDoc->readMapped('somecamel')); } - public function testResolveModelMappings() + public function testShouldResolveModelMappings() { $mappingManager = new MappingManager(); $mappingManager->add(new Json()); diff --git a/tests/Mvc/Module/LoaderTest.php b/tests/Mvc/Module/LoaderTest.php index cbb700b..12b5e12 100644 --- a/tests/Mvc/Module/LoaderTest.php +++ b/tests/Mvc/Module/LoaderTest.php @@ -17,7 +17,7 @@ class LoaderTest extends \PHPUnit_Framework_TestCase { - public function testDump() + public function testShouldLoadAllModulesFromApplication() { $modules = ModuleLoader::dump(DI::getDefault()); diff --git a/tests/Util/FileWriterTest.php b/tests/Util/FileWriterTest.php new file mode 100644 index 0000000..80b039a --- /dev/null +++ b/tests/Util/FileWriterTest.php @@ -0,0 +1,59 @@ + + * @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\Util; + +use Vegas\Util\FileWriter; + +class FileWriterTest extends \Vegas\Tests\App\TestCase +{ + public function testShouldWriteContentToFile() + { + $content = sha1(time()); + $path = TESTS_ROOT_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'test.tmp'; + $this->assertGreaterThan(0, FileWriter::write($path, $content, false)); + $this->assertEquals($content, file_get_contents($path)); + + unlink($path); + } + + public function testShouldNotWriteContentToIdenticalFile() + { + $content = sha1(time()); + $path = TESTS_ROOT_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'test.tmp'; + $this->assertGreaterThan(0, FileWriter::write($path, $content, false)); + $this->assertSame(0, FileWriter::write($path, $content, true)); + + unlink($path); + } + + public function testShouldUpdateFileContentWithDifferentContent() + { + $content = sha1(microtime()); + $path = TESTS_ROOT_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'test.tmp'; + $this->assertGreaterThan(0, FileWriter::write($path, $content, false)); + $content = sha1(microtime()); + $this->assertGreaterThan(0, FileWriter::write($path, $content, true)); + + unlink($path); + } + + public function testShouldWriteContentToFileWhenNotExists() + { + $content = sha1(microtime()); + $path = TESTS_ROOT_DIR . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'test.tmp'; + $this->assertGreaterThan(0, FileWriter::write($path, $content, true)); + + unlink($path); + } +} + \ No newline at end of file From fc0e0753b18ed23cf8f7d55caf5519c767193bf2 Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 10 Dec 2014 16:07:34 +0100 Subject: [PATCH 24/35] Refactoring; Added Vegas\Test; Bootstrap rewriting; --- src/Application/Bootstrap.php | 26 +- src/Cli/Bootstrap.php | 12 +- src/Cli/ColorsOuputTrait.php | 64 ++-- src/Cli/Console.php | 3 +- src/Cli/EventsListener/TaskListener.php | 3 +- src/Cli/Loader.php | 2 - src/Cli/Task.php | 4 +- src/DI/Scaffolding.php | 5 +- src/DI/Scaffolding/Adapter/Mongo.php | 15 +- src/DI/Scaffolding/Adapter/Mysql.php | 16 +- src/DI/Scaffolding/AdapterInterface.php | 6 +- src/DI/ScaffoldingInterface.php | 7 +- .../Service/Component/RendererInterface.php | 5 +- src/DI/Service/ComponentAbstract.php | 3 +- src/DI/ServiceManager.php | 3 +- src/DI/ServiceProviderLoader.php | 27 +- src/Db/Adapter/Mongo/AdapterTrait.php | 12 +- src/Db/AdapterInterface.php | 5 +- src/Db/Decorator/CollectionAbstract.php | 3 +- src/Db/Decorator/ModelAbstract.php | 3 +- .../Exception/NoRequiredServiceException.php | 4 +- src/Mvc/Application.php | 3 +- src/Mvc/Bootstrap.php | 104 +++++++ src/Mvc/ControllerAbstract.php | 16 +- src/Mvc/Dispatcher/ExceptionResolver.php | 8 +- src/Mvc/Module/Loader.php | 10 +- src/Mvc/Router.php | 9 +- src/Mvc/Router/Adapter/Standard.php | 5 +- .../Exception/InvalidRouteNameException.php | 2 +- .../Exception/InvalidRoutePathsException.php | 2 +- src/Mvc/Router/Route.php | 2 +- src/Mvc/Router/Route/BaseRoute.php | 3 +- src/Mvc/Router/Route/DefaultRoute.php | 3 +- src/Mvc/Router/Route/NotfoundRoute.php | 3 +- src/Mvc/Router/Route/RestRoute.php | 3 +- src/Mvc/Router/Route/StaticRoute.php | 3 +- src/Mvc/Router/RouteInterface.php | 5 +- src/Mvc/View.php | 17 +- src/Mvc/View/Engine/Volt.php | 1 - src/Mvc/View/Engine/Volt/Filter/ToString.php | 1 - .../View/Engine/Volt/Helper/Pagination.php | 1 - .../View/Engine/Volt/Helper/ShortenText.php | 1 - .../View/Engine/Volt/VoltFilterAbstract.php | 1 - src/Paginator/Adapter/Mongo.php | 3 +- src/Tag/Pagination.php | 5 +- src/Task/AssetsTask.php | 6 +- src/Task/CacheTask.php | 3 +- src/Test/Bootstrap.php | 19 ++ src/Test/Http/Request.php | 290 ++++++++++++++++++ src/Test/TestCase.php | 85 +++++ src/Translate/Adapter/Gettext.php | 2 +- tests/Mvc/Router/RouteTest.php | 2 +- tests/Util/FileWriterTest.php | 2 +- 53 files changed, 712 insertions(+), 136 deletions(-) create mode 100644 src/Mvc/Bootstrap.php create mode 100644 src/Test/Bootstrap.php create mode 100644 src/Test/Http/Request.php create mode 100644 src/Test/TestCase.php diff --git a/src/Application/Bootstrap.php b/src/Application/Bootstrap.php index 3e6b2bf..3f9e661 100644 --- a/src/Application/Bootstrap.php +++ b/src/Application/Bootstrap.php @@ -12,6 +12,7 @@ namespace Vegas\Application; +use Phalcon\Config; use Phalcon\DI\FactoryDefault; use Phalcon\DI; use Phalcon\DiInterface; @@ -19,8 +20,12 @@ use Vegas\BootstrapInterface; use Vegas\Constants; use Vegas\DI\ServiceProviderLoader; +use Vegas\Mvc\Application; use Vegas\Mvc\Dispatcher\Events\BeforeException; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; +use Vegas\Mvc\Module\Loader; +use Vegas\Mvc\Router\Adapter\Standard; +use Vegas\Mvc\Router; /** * Class Bootstrap @@ -41,14 +46,14 @@ class Bootstrap implements BootstrapInterface /** * MVC Application * - * @var \Vegas\Mvc\Application + * @var Application */ protected $application; /** * Application config * - * @var \Phalcon\Config + * @var Config */ protected $config; @@ -57,13 +62,13 @@ class Bootstrap implements BootstrapInterface * Initializes MVC Application * Initializes DI for Application * - * @param \Phalcon\Config $config + * @param Config $config */ - public function __construct(\Phalcon\Config $config) + public function __construct(Config $config) { $this->config = $config; $this->di = new FactoryDefault(); - $this->application = new \Vegas\Mvc\Application(); + $this->application = new Application(); } /** @@ -127,7 +132,7 @@ protected function initLoader() protected function initModules() { //registers modules defined in modules.php file - $modulesFile = $this->config->application->configDir . 'modules.php'; + $modulesFile = $this->config->application->configDir . Loader::MODULE_STATIC_FILE; if (!file_exists($modulesFile) || $this->di->get('environment') != Constants::DEFAULT_ENV) { ModuleLoader::dump($this->di); } @@ -160,8 +165,8 @@ protected function initServices() protected function initRoutes() { //setups router - $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard($this->di); - $router = new \Vegas\Mvc\Router($this->di, $routerAdapter); + $routerAdapter = new Standard($this->di); + $router = new Router($this->di, $routerAdapter); //adds routes defined in modules $modules = $this->application->getModules(); @@ -189,6 +194,9 @@ protected function initDispatcher() $this->di->set('dispatcher', function() { $dispatcher = new Dispatcher(); + /** + * @var \Phalcon\Events\Manager $eventsManager + */ $eventsManager = $this->di->getShared('eventsManager'); $eventsManager->attach('dispatch:beforeException', BeforeException::getEvent()); diff --git a/src/Cli/Bootstrap.php b/src/Cli/Bootstrap.php index a6b522f..48da2d5 100644 --- a/src/Cli/Bootstrap.php +++ b/src/Cli/Bootstrap.php @@ -12,12 +12,14 @@ namespace Vegas\Cli; +use Phalcon\Config; use Phalcon\DI\FactoryDefault\CLI; use Vegas\BootstrapInterface; +use Vegas\Cli\EventsListener\TaskListener; use Vegas\Cli\Exception as CliException; use Vegas\Constants; use Vegas\DI\ServiceProviderLoader; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; /** * Class Bootstrap @@ -39,9 +41,9 @@ class Bootstrap implements BootstrapInterface * Initializes Console Application * Initializes DI for CLI application * - * @param \Phalcon\Config $config + * @param Config $config */ - public function __construct(\Phalcon\Config $config) + public function __construct(Config $config) { $this->config = $config; $this->di = new CLI(); @@ -145,10 +147,10 @@ protected function initEventsManager() $eventsManager = $this->di->getShared('eventsManager'); //attaches new event console:beforeTaskHandle and console:afterTaskHandle $eventsManager->attach( - 'console:beforeHandleTask', \Vegas\Cli\EventsListener\TaskListener::beforeHandleTask($this->arguments) + 'console:beforeHandleTask', TaskListener::beforeHandleTask($this->arguments) ); $eventsManager->attach( - 'console:afterHandleTask', \Vegas\Cli\EventsListener\TaskListener::afterHandleTask() + 'console:afterHandleTask', TaskListener::afterHandleTask() ); $this->console->setEventsManager($eventsManager); } diff --git a/src/Cli/ColorsOuputTrait.php b/src/Cli/ColorsOuputTrait.php index b2d79f8..6b985d7 100644 --- a/src/Cli/ColorsOuputTrait.php +++ b/src/Cli/ColorsOuputTrait.php @@ -22,13 +22,13 @@ trait ColorsOuputTrait * @var array * @internal */ - private $foreground_colors = array(); + private $foregroundColors = array(); /** * @var array * @internal */ - private $background_colors = array(); + private $backgroundColors = array(); /** * Constructors @@ -37,31 +37,31 @@ trait ColorsOuputTrait public function __construct() { // Set up shell colors - $this->foreground_colors['black'] = '0;30'; - $this->foreground_colors['dark_gray'] = '1;30'; - $this->foreground_colors['blue'] = '0;34'; - $this->foreground_colors['light_blue'] = '1;34'; - $this->foreground_colors['green'] = '0;32'; - $this->foreground_colors['light_green'] = '1;32'; - $this->foreground_colors['cyan'] = '0;36'; - $this->foreground_colors['light_cyan'] = '1;36'; - $this->foreground_colors['red'] = '0;31'; - $this->foreground_colors['light_red'] = '1;31'; - $this->foreground_colors['purple'] = '0;35'; - $this->foreground_colors['light_purple'] = '1;35'; - $this->foreground_colors['brown'] = '0;33'; - $this->foreground_colors['yellow'] = '1;33'; - $this->foreground_colors['light_gray'] = '0;37'; - $this->foreground_colors['white'] = '1;37'; + $this->foregroundColors['black'] = '0;30'; + $this->foregroundColors['dark_gray'] = '1;30'; + $this->foregroundColors['blue'] = '0;34'; + $this->foregroundColors['light_blue'] = '1;34'; + $this->foregroundColors['green'] = '0;32'; + $this->foregroundColors['light_green'] = '1;32'; + $this->foregroundColors['cyan'] = '0;36'; + $this->foregroundColors['light_cyan'] = '1;36'; + $this->foregroundColors['red'] = '0;31'; + $this->foregroundColors['light_red'] = '1;31'; + $this->foregroundColors['purple'] = '0;35'; + $this->foregroundColors['light_purple'] = '1;35'; + $this->foregroundColors['brown'] = '0;33'; + $this->foregroundColors['yellow'] = '1;33'; + $this->foregroundColors['light_gray'] = '0;37'; + $this->foregroundColors['white'] = '1;37'; - $this->background_colors['black'] = '40'; - $this->background_colors['red'] = '41'; - $this->background_colors['green'] = '42'; - $this->background_colors['yellow'] = '43'; - $this->background_colors['blue'] = '44'; - $this->background_colors['magenta'] = '45'; - $this->background_colors['cyan'] = '46'; - $this->background_colors['light_gray'] = '47'; + $this->backgroundColors['black'] = '40'; + $this->backgroundColors['red'] = '41'; + $this->backgroundColors['green'] = '42'; + $this->backgroundColors['yellow'] = '43'; + $this->backgroundColors['blue'] = '44'; + $this->backgroundColors['magenta'] = '45'; + $this->backgroundColors['cyan'] = '46'; + $this->backgroundColors['light_gray'] = '47'; } /** @@ -77,12 +77,12 @@ public function getColoredString($string, $foreground_color = null, $background_ $colored_string = ""; // Check if given foreground color found - if (isset($this->foreground_colors[$foreground_color])) { - $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; + if (isset($this->foregroundColors[$foreground_color])) { + $colored_string .= "\033[" . $this->foregroundColors[$foreground_color] . "m"; } // Check if given background color found - if (isset($this->background_colors[$background_color])) { - $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; + if (isset($this->backgroundColors[$background_color])) { + $colored_string .= "\033[" . $this->backgroundColors[$background_color] . "m"; } // Add string and end coloring @@ -98,7 +98,7 @@ public function getColoredString($string, $foreground_color = null, $background_ */ public function getForegroundColors() { - return array_keys($this->foreground_colors); + return array_keys($this->foregroundColors); } /** @@ -108,6 +108,6 @@ public function getForegroundColors() */ public function getBackgroundColors() { - return array_keys($this->background_colors); + return array_keys($this->backgroundColors); } } \ No newline at end of file diff --git a/src/Cli/Console.php b/src/Cli/Console.php index 0f81301..97dc521 100644 --- a/src/Cli/Console.php +++ b/src/Cli/Console.php @@ -11,6 +11,7 @@ */ namespace Vegas\Cli; +use Phalcon\Loader; /** * Class Console @@ -51,7 +52,7 @@ public function registerModules($modules) */ private function registerSharedData($modules) { - $loader = new \Phalcon\Loader(); + $loader = new Loader(); foreach ($modules As $name => $module) { diff --git a/src/Cli/EventsListener/TaskListener.php b/src/Cli/EventsListener/TaskListener.php index 2ea94a8..f713a55 100644 --- a/src/Cli/EventsListener/TaskListener.php +++ b/src/Cli/EventsListener/TaskListener.php @@ -16,6 +16,7 @@ use Phalcon\CLI\Dispatcher; use Phalcon\Events\Event; use Vegas\Cli\OptionParser; +use Vegas\Cli\Task; /** * Class TaskListener @@ -50,7 +51,7 @@ public static function beforeHandleTask($argv) */ public static function afterHandleTask() { - return function(Event $event, Console $console, \Vegas\Cli\Task $task) { + return function(Event $event, Console $console, Task $task) { echo $task->getOutput(); }; } diff --git a/src/Cli/Loader.php b/src/Cli/Loader.php index 785d665..b08a3ed 100644 --- a/src/Cli/Loader.php +++ b/src/Cli/Loader.php @@ -12,9 +12,7 @@ namespace Vegas\Cli; -use Vegas\Cli\Console; use Vegas\Cli\Exception as CliException; -use Vegas\Cli\Exception\TaskActionNotSpecifiedException; use Vegas\Cli\Exception\TaskNotFoundException; /** diff --git a/src/Cli/Task.php b/src/Cli/Task.php index 0a56fb9..a05ed92 100644 --- a/src/Cli/Task.php +++ b/src/Cli/Task.php @@ -120,6 +120,8 @@ public function beforeExecuteRoute() $this->renderActionHelp(); } + + return true; } /** @@ -278,7 +280,7 @@ public function putError($str) * * @param $object */ - public function pubObject($object) + public function putObject($object) { $this->appendLine($this->getColoredString(print_r($object, true), 'black', 'light_gray')); } diff --git a/src/DI/Scaffolding.php b/src/DI/Scaffolding.php index a1dd227..dc27d7f 100644 --- a/src/DI/Scaffolding.php +++ b/src/DI/Scaffolding.php @@ -11,6 +11,7 @@ */ namespace Vegas\DI; +use Vegas\DI\Scaffolding\AdapterInterface; use Vegas\DI\Scaffolding\Exception\DeleteFailureException; use Vegas\DI\Scaffolding\Exception\InvalidFormException; @@ -31,7 +32,7 @@ * * @package Vegas\DI */ -class Scaffolding implements \Vegas\DI\ScaffoldingInterface +class Scaffolding implements ScaffoldingInterface { /** * Dependency injector @@ -78,7 +79,7 @@ class Scaffolding implements \Vegas\DI\ScaffoldingInterface /** * {@inheritdoc} */ - public function __construct(\Vegas\DI\Scaffolding\AdapterInterface $adapter) + public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; $this->adapter->setScaffolding($this); diff --git a/src/DI/Scaffolding/Adapter/Mongo.php b/src/DI/Scaffolding/Adapter/Mongo.php index 5b6ff1f..877e597 100644 --- a/src/DI/Scaffolding/Adapter/Mongo.php +++ b/src/DI/Scaffolding/Adapter/Mongo.php @@ -13,7 +13,12 @@ namespace Vegas\DI\Scaffolding\Adapter; use Phalcon\DI; +use Vegas\Db\Adapter\Mongo\AdapterTrait; +use Vegas\Db\AdapterInterface; +use Vegas\DI\Scaffolding\AdapterInterface as ScaffoldingAdapterInterface; use Vegas\DI\Scaffolding\Exception\RecordNotFoundException; +use Vegas\DI\Scaffolding; +use Vegas\Paginator\Adapter\Mongo as PaginatorAdapterMongo; /** * Class Mongo @@ -22,14 +27,14 @@ * * @package Vegas\DI\Scaffolding\Adapter */ -class Mongo implements \Vegas\Db\AdapterInterface, \Vegas\DI\Scaffolding\AdapterInterface +class Mongo implements AdapterInterface, ScaffoldingAdapterInterface { - use \Vegas\Db\Adapter\Mongo\AdapterTrait; + use AdapterTrait; /** * Scaffolding instance * - * @var \Vegas\DI\Scaffolding + * @var Scaffolding */ protected $scaffolding; @@ -63,7 +68,7 @@ public function retrieveOne($id) */ public function getPaginator($page = 1, $limit = 10) { - return new \Vegas\Paginator\Adapter\Mongo(array( + return new PaginatorAdapterMongo(array( 'model' => $this->scaffolding->getRecord(), 'limit' => $limit, 'page' => $page @@ -73,7 +78,7 @@ public function getPaginator($page = 1, $limit = 10) /** * {@inheritdoc} */ - public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding) { + public function setScaffolding(Scaffolding $scaffolding) { $this->scaffolding = $scaffolding; return $this; diff --git a/src/DI/Scaffolding/Adapter/Mysql.php b/src/DI/Scaffolding/Adapter/Mysql.php index 6380369..cde1921 100644 --- a/src/DI/Scaffolding/Adapter/Mysql.php +++ b/src/DI/Scaffolding/Adapter/Mysql.php @@ -13,7 +13,13 @@ namespace Vegas\DI\Scaffolding\Adapter; use Phalcon\DI; +use Phalcon\DiInterface; +use Phalcon\Paginator\Adapter\Model as PaginatorAdapterModel; +use Vegas\Db\AdapterInterface; +use Vegas\Db\Exception\NoRequiredServiceException; +use Vegas\DI\Scaffolding\AdapterInterface as ScaffoldingAdapterInterface; use Vegas\DI\Scaffolding\Exception\RecordNotFoundException; +use Vegas\DI\Scaffolding; /** * Class Mysql @@ -22,12 +28,12 @@ * * @package Vegas\DI\Scaffolding\Adapter */ -class Mysql implements \Vegas\Db\AdapterInterface, \Vegas\DI\Scaffolding\AdapterInterface +class Mysql implements AdapterInterface, ScaffoldingAdapterInterface { /** * Scaffolding instance * - * @var \Vegas\DI\Scaffolding + * @var Scaffolding */ protected $scaffolding; @@ -59,7 +65,7 @@ public function retrieveOne($id) */ public function getPaginator($page = 1, $limit = 10) { - return new \Phalcon\Paginator\Adapter\Model(array( + return new PaginatorAdapterModel(array( 'data' => call_user_func(array($this->scaffolding->getRecord(),'find')), 'limit' => $limit, 'page' => $page @@ -69,7 +75,7 @@ public function getPaginator($page = 1, $limit = 10) /** * {@inheritdoc} */ - public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding) { + public function setScaffolding(Scaffolding $scaffolding) { $this->scaffolding = $scaffolding; return $this; @@ -78,7 +84,7 @@ public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding) { /** * {@inheritdoc} */ - public function verifyRequiredServices(\Phalcon\DiInterface $di) + public function verifyRequiredServices(DiInterface $di) { if (!$di->has('db')) { throw new NoRequiredServiceException(); diff --git a/src/DI/Scaffolding/AdapterInterface.php b/src/DI/Scaffolding/AdapterInterface.php index 8f78a31..3f27e5f 100644 --- a/src/DI/Scaffolding/AdapterInterface.php +++ b/src/DI/Scaffolding/AdapterInterface.php @@ -11,6 +11,8 @@ */ namespace Vegas\DI\Scaffolding; +use Vegas\DI\Scaffolding; + /** * Interface AdapterInterface * @package Vegas\DI\Scaffolding @@ -37,8 +39,8 @@ public function getPaginator($page = 1, $limit = 10); /** * Sets scaffolding instance * - * @param \Vegas\DI\Scaffolding $scaffolding + * @param Scaffolding $scaffolding * @return mixed */ - public function setScaffolding(\Vegas\DI\Scaffolding $scaffolding); + public function setScaffolding(Scaffolding $scaffolding); } diff --git a/src/DI/ScaffoldingInterface.php b/src/DI/ScaffoldingInterface.php index 3826722..ca362be 100644 --- a/src/DI/ScaffoldingInterface.php +++ b/src/DI/ScaffoldingInterface.php @@ -10,6 +10,7 @@ * file that was distributed with this source code. */ namespace Vegas\DI; +use Vegas\DI\Scaffolding\AdapterInterface; /** * Interface ScaffoldingInterface @@ -21,14 +22,14 @@ interface ScaffoldingInterface * Constructor * Sets scaffolding adapter * - * @param Scaffolding\AdapterInterface $adapter + * @param AdapterInterface $adapter */ - public function __construct(\Vegas\DI\Scaffolding\AdapterInterface $adapter); + public function __construct(AdapterInterface $adapter); /** * Returns scaffolding adapter * - * @return Scaffolding\AdapterInterface + * @return AdapterInterface */ public function getAdapter(); diff --git a/src/DI/Service/Component/RendererInterface.php b/src/DI/Service/Component/RendererInterface.php index 9ae3431..69cc60b 100644 --- a/src/DI/Service/Component/RendererInterface.php +++ b/src/DI/Service/Component/RendererInterface.php @@ -10,6 +10,7 @@ * file that was distributed with this source code. */ namespace Vegas\DI\Service\Component; +use Phalcon\Mvc\View; /** * Interface RendererInterface @@ -21,9 +22,9 @@ interface RendererInterface * Constructor * Sets view instance * - * @param \Phalcon\Mvc\View $view + * @param View $view */ - public function __construct(\Phalcon\Mvc\View $view = null); + public function __construct(View $view = null); /** * Sets module name diff --git a/src/DI/Service/ComponentAbstract.php b/src/DI/Service/ComponentAbstract.php index de91931..37f995d 100644 --- a/src/DI/Service/ComponentAbstract.php +++ b/src/DI/Service/ComponentAbstract.php @@ -12,6 +12,7 @@ namespace Vegas\DI\Service; use Phalcon\DI\InjectionAwareInterface; +use Vegas\DI\InjectionAwareTrait; /** * Class ComponentAbstract @@ -19,7 +20,7 @@ */ abstract class ComponentAbstract implements ComponentInterface, InjectionAwareInterface { - use \Vegas\DI\InjectionAwareTrait; + use InjectionAwareTrait; /** * Renderer instance diff --git a/src/DI/ServiceManager.php b/src/DI/ServiceManager.php index 4296fbd..0eb2cac 100644 --- a/src/DI/ServiceManager.php +++ b/src/DI/ServiceManager.php @@ -21,13 +21,14 @@ */ namespace Vegas\DI; +use Phalcon\DI\InjectionAwareInterface; use Vegas\DI\Service\Exception; /** * Class ServiceManager * @package Vegas\DI */ -class ServiceManager implements \Phalcon\DI\InjectionAwareInterface +class ServiceManager implements InjectionAwareInterface { use InjectionAwareTrait; diff --git a/src/DI/ServiceProviderLoader.php b/src/DI/ServiceProviderLoader.php index 9a5ec94..7a20df7 100644 --- a/src/DI/ServiceProviderLoader.php +++ b/src/DI/ServiceProviderLoader.php @@ -12,7 +12,9 @@ namespace Vegas\DI; +use Phalcon\Config; use Phalcon\DiInterface; +use Phalcon\Loader; use Vegas\Constants; use Vegas\Util\FileWriter; @@ -22,6 +24,11 @@ */ class ServiceProviderLoader { + /** + * Name of file containing static list of services + */ + const SERVICES_STATIC_FILE = 'services.php'; + /** * Dumps services to source file * @@ -41,7 +48,11 @@ public static function dump(DiInterface $di) } //saves generated array to php source file - FileWriter::write($config->application->configDir . 'services.php', self::createFileContent($servicesList), true); + FileWriter::write( + $config->application->configDir . self::SERVICES_STATIC_FILE, + self::createFileContent($servicesList), + true + ); ksort($servicesList); return $servicesList; @@ -64,12 +75,16 @@ private static function createFileContent($servicesList) */ public static function autoload(DiInterface $di) { + /** + * @var \Phalcon\Config $config + */ $config = $di->get('config'); $configDir = $config->application->configDir; - if (!file_exists($configDir . 'services.php') || $di->get('environment') != Constants::DEFAULT_ENV) { + if (!file_exists($configDir . self::SERVICES_STATIC_FILE) + || $di->get('environment') != Constants::DEFAULT_ENV) { $services = self::dump($di); } else { - $services = require($configDir . 'services.php'); + $services = require($configDir . self::SERVICES_STATIC_FILE); } self::setupServiceProvidersAutoloader($config, $services); @@ -113,14 +128,14 @@ public static function autoload(DiInterface $di) /** * Registers classes that contains services providers * - * @param \Phalcon\Config $config + * @param Config $config * @param $services * @internal */ - private static function setupServiceProvidersAutoloader(\Phalcon\Config $config, $services) + private static function setupServiceProvidersAutoloader(Config $config, $services) { //creates the autoloader - $loader = new \Phalcon\Loader(); + $loader = new Loader(); //setup default path when is not defined foreach ($services as $className => $path) { diff --git a/src/Db/Adapter/Mongo/AdapterTrait.php b/src/Db/Adapter/Mongo/AdapterTrait.php index acd26e2..6c03a8a 100644 --- a/src/Db/Adapter/Mongo/AdapterTrait.php +++ b/src/Db/Adapter/Mongo/AdapterTrait.php @@ -11,6 +11,8 @@ */ namespace Vegas\Db\Adapter\Mongo; +use Phalcon\DiInterface; +use Phalcon\Mvc\Collection\Manager; use Vegas\Db\Exception\NoRequiredServiceException; /** @@ -25,10 +27,10 @@ trait AdapterTrait /** * Verifies required services for Mongo adapter * - * @param \Phalcon\DiInterface $di + * @param DiInterface $di * @throws \Vegas\Db\Exception\NoRequiredServiceException */ - public function verifyRequiredServices(\Phalcon\DiInterface $di) + public function verifyRequiredServices(DiInterface $di) { if (!$di->has('mongo')) { throw new NoRequiredServiceException(); @@ -38,13 +40,13 @@ public function verifyRequiredServices(\Phalcon\DiInterface $di) /** * Setups extra services (if not exist) required by mongo service * - * @param \Phalcon\DiInterface $di + * @param DiInterface $di */ - public function setupExtraServices(\Phalcon\DiInterface $di) + public function setupExtraServices(DiInterface $di) { if (!$di->has('collectionManager')) { $di->set('collectionManager', function() { - return new \Phalcon\Mvc\Collection\Manager(); + return new Manager(); }); } } diff --git a/src/Db/AdapterInterface.php b/src/Db/AdapterInterface.php index 34b01e2..177eedc 100644 --- a/src/Db/AdapterInterface.php +++ b/src/Db/AdapterInterface.php @@ -10,6 +10,7 @@ * file that was distributed with this source code. */ namespace Vegas\Db; +use Phalcon\DiInterface; /** * Interface AdapterInterface @@ -20,8 +21,8 @@ interface AdapterInterface /** * Verifies services required by db adapter * - * @param \Phalcon\DiInterface $di + * @param DiInterface $di * @return mixed */ - public function verifyRequiredServices(\Phalcon\DiInterface $di); + public function verifyRequiredServices(DiInterface $di); } diff --git a/src/Db/Decorator/CollectionAbstract.php b/src/Db/Decorator/CollectionAbstract.php index c92ba48..ba39b77 100644 --- a/src/Db/Decorator/CollectionAbstract.php +++ b/src/Db/Decorator/CollectionAbstract.php @@ -11,6 +11,7 @@ */ namespace Vegas\Db\Decorator; +use Phalcon\Mvc\Collection; use Vegas\Db\Decorator\Helper\MappingHelperTrait; use Vegas\Db\Decorator\Helper\SlugTrait; use Vegas\Db\Decorator\Helper\WriteAttributesTrait; @@ -20,7 +21,7 @@ * Class CollectionAbstract * @package Vegas\Db\Decorator */ -abstract class CollectionAbstract extends \Phalcon\Mvc\Collection +abstract class CollectionAbstract extends Collection { use MappingResolverTrait; use MappingHelperTrait; diff --git a/src/Db/Decorator/ModelAbstract.php b/src/Db/Decorator/ModelAbstract.php index e137300..f41b1da 100644 --- a/src/Db/Decorator/ModelAbstract.php +++ b/src/Db/Decorator/ModelAbstract.php @@ -12,6 +12,7 @@ namespace Vegas\Db\Decorator; +use Phalcon\Mvc\Model; use Vegas\Db\Decorator\Helper\MappingHelperTrait; use Vegas\Db\Decorator\Helper\SlugTrait; use Vegas\Db\Decorator\Helper\WriteAttributesTrait; @@ -21,7 +22,7 @@ * Class ModelAbstract * @package Vegas\Db\Decorator */ -abstract class ModelAbstract extends \Phalcon\Mvc\Model +abstract class ModelAbstract extends Model { use MappingResolverTrait; use MappingHelperTrait; diff --git a/src/Db/Exception/NoRequiredServiceException.php b/src/Db/Exception/NoRequiredServiceException.php index 1520fc4..2d28851 100644 --- a/src/Db/Exception/NoRequiredServiceException.php +++ b/src/Db/Exception/NoRequiredServiceException.php @@ -11,11 +11,13 @@ */ namespace Vegas\Db\Exception; +use Vegas\Db\Exception; + /** * Class NoRequiredServiceException * @package Vegas\Db\Exception */ -class NoRequiredServiceException extends \Vegas\Db\Exception +class NoRequiredServiceException extends Exception { /** * Exception default message diff --git a/src/Mvc/Application.php b/src/Mvc/Application.php index 8bca2fe..ecb0da7 100644 --- a/src/Mvc/Application.php +++ b/src/Mvc/Application.php @@ -11,6 +11,7 @@ */ namespace Vegas\Mvc; +use Phalcon\Loader; /** * Class Application @@ -51,7 +52,7 @@ public function registerModules($modules, $merge = null) */ private function registerSharedData($modules) { - $loader = new \Phalcon\Loader(); + $loader = new Loader(); foreach ($modules As $name => $module) { diff --git a/src/Mvc/Bootstrap.php b/src/Mvc/Bootstrap.php new file mode 100644 index 0000000..bf72552 --- /dev/null +++ b/src/Mvc/Bootstrap.php @@ -0,0 +1,104 @@ + + * @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\Mvc; + +use Phalcon\Config; +use Phalcon\DI\FactoryDefault; +use Phalcon\DiInterface; +use Vegas\BootstrapInterface; + +class Bootstrap implements BootstrapInterface +{ + /** + * Dependency injection + * + * @var DiInterface + */ + protected $di; + + /** + * MVC Application + * + * @var Application + */ + protected $application; + + /** + * Application config + * + * @var Config + */ + protected $config; + + /** + * Constructor + * Initializes MVC Application + * Initializes DI for Application + * + * @param Config $config + */ + public function __construct(Config $config) + { + $this->config = $config; + $this->di = new FactoryDefault(); + $this->application = new Application(); + } + + /** + * Sets Dependency Injector + * + * @param DiInterface $di + */ + public function setDi(DiInterface $di) + { + $this->di = $di; + } + + /** + * Returns Dependency Injector + * + * @return FactoryDefault|DiInterface + */ + public function getDI() + { + return $this->di; + } + + /** + * @return Application + */ + public function getApplication() + { + return $this->application; + } + /** + * Executes all bootstrap initialization methods + * This method can be overloaded to load own initialization method. + * @return mixed + */ + public function setup() + { + // TODO: Implement setup() method. + } + + /** + * Runs application + * + * @return mixed + */ + public function run() + { + // TODO: Implement run() method. + } +} + \ No newline at end of file diff --git a/src/Mvc/ControllerAbstract.php b/src/Mvc/ControllerAbstract.php index 7590173..07325e5 100644 --- a/src/Mvc/ControllerAbstract.php +++ b/src/Mvc/ControllerAbstract.php @@ -12,6 +12,8 @@ namespace Vegas\Mvc; use Phalcon\Mvc\Controller; +use Vegas\Exception; +use Vegas\Mvc\View; /** * Class ControllerAbstract @@ -30,7 +32,7 @@ public function initialize() * It allows to use $this->view->partial inside of action method before action view is being rendered */ $this->eventsManager->attach('view:beforeRenderView', function($event, $view, $engineViewPath) { - if ($view instanceof \Vegas\Mvc\View) { + if ($view instanceof View) { $view->setControllerViewPath($this->router->getControllerName()); } }); @@ -69,32 +71,32 @@ protected function _($text) * Throws exception with code 403 * * @param string $message - * @throws \Vegas\Exception + * @throws Exception */ protected function throw403($message = '') { - throw new \Vegas\Exception($message, 403); + throw new Exception($message, 403); } /** * Throws exception with code 404 * * @param string $message - * @throws \Vegas\Exception + * @throws Exception */ protected function throw404($message = '') { - throw new \Vegas\Exception($message, 404); + throw new Exception($message, 404); } /** * Throws exception with code 500 * * @param string $message - * @throws \Vegas\Exception + * @throws Exception */ protected function throw500($message = '') { - throw new \Vegas\Exception($message, 500); + throw new Exception($message, 500); } } diff --git a/src/Mvc/Dispatcher/ExceptionResolver.php b/src/Mvc/Dispatcher/ExceptionResolver.php index e73f6ba..74aa82e 100644 --- a/src/Mvc/Dispatcher/ExceptionResolver.php +++ b/src/Mvc/Dispatcher/ExceptionResolver.php @@ -11,20 +11,22 @@ */ namespace Vegas\Mvc\Dispatcher; +use Phalcon\DI\InjectionAwareInterface; use Phalcon\Dispatcher; use Vegas\Constants; +use Vegas\DI\InjectionAwareTrait; use Vegas\Exception; -use Vegas\Mvc\Dispatcher\Exception\CannotHandleErrorException; use Vegas\Exception as VegasException; +use Vegas\Mvc\Dispatcher\Exception\CannotHandleErrorException; use Vegas\Mvc\View; /** * Class ExceptionResolver * @package Vegas\Mvc\Dispatcher */ -class ExceptionResolver implements \Phalcon\DI\InjectionAwareInterface +class ExceptionResolver implements InjectionAwareInterface { - use \Vegas\DI\InjectionAwareTrait; + use InjectionAwareTrait; /** * Resolves application error and renders error diff --git a/src/Mvc/Module/Loader.php b/src/Mvc/Module/Loader.php index b3b74e1..56307bf 100644 --- a/src/Mvc/Module/Loader.php +++ b/src/Mvc/Module/Loader.php @@ -13,6 +13,7 @@ namespace Vegas\Mvc\Module; use Phalcon\DiInterface; +use Phalcon\Text; use Vegas\Util\FileWriter; /** @@ -26,6 +27,11 @@ class Loader */ const MODULE_SETTINGS_FILE = 'Module.php'; + /** + * Name of file containing list of modules + */ + const MODULE_STATIC_FILE = 'modules.php'; + /** * Generates list of modules into source file * @@ -62,7 +68,7 @@ public static function dump(DiInterface $di) //saves generated array to php source file FileWriter::write( - $config->application->configDir . 'modules.php', + $config->application->configDir . self::MODULE_STATIC_FILE, self::createFileContent($modulesList), true ); @@ -108,7 +114,7 @@ private static function dumpModulesFromVendor(array &$modulesList) continue; } - $baseName = \Phalcon\Text::camelize($libDir->getBasename()); + $baseName = Text::camelize($libDir->getBasename()); if (!isset($modulesList[$baseName])) { $modulesList[$baseName] = [ 'className' => $baseName diff --git a/src/Mvc/Router.php b/src/Mvc/Router.php index b84ebfe..567605d 100644 --- a/src/Mvc/Router.php +++ b/src/Mvc/Router.php @@ -14,6 +14,7 @@ use Phalcon\DI\InjectionAwareInterface; use Phalcon\DiInterface; +use Phalcon\Mvc\RouterInterface; use Vegas\DI\InjectionAwareTrait; use Vegas\Mvc\Router\Exception\InvalidRouteTypeException; use Vegas\Mvc\Router\Route; @@ -58,7 +59,7 @@ class Router implements InjectionAwareInterface private $routes = array(); /** - * @var \Phalcon\Mvc\RouterInterface + * @var RouterInterface * @internal */ private $adapter; @@ -76,9 +77,9 @@ class Router implements InjectionAwareInterface * Sets router adapter * * @param DiInterface $di - * @param \Phalcon\Mvc\RouterInterface $routerAdapter + * @param RouterInterface $routerAdapter */ - public function __construct(DiInterface $di, \Phalcon\Mvc\RouterInterface $routerAdapter) + public function __construct(DiInterface $di, RouterInterface $routerAdapter) { $this->setDI($di); $this->adapter = $routerAdapter; @@ -190,7 +191,7 @@ private function validateRouteType($type) /** * Returns router adapter * - * @return \Phalcon\Mvc\RouterInterface + * @return RouterInterface */ public function getRouter() { diff --git a/src/Mvc/Router/Adapter/Standard.php b/src/Mvc/Router/Adapter/Standard.php index af24c63..5ba8439 100644 --- a/src/Mvc/Router/Adapter/Standard.php +++ b/src/Mvc/Router/Adapter/Standard.php @@ -15,6 +15,7 @@ use Phalcon\DI; use Phalcon\DiInterface; +use Phalcon\Mvc\Router; use Vegas\DI\InjectionAwareTrait; /** @@ -24,7 +25,7 @@ * @package Vegas\Mvc\Router\Adapter * @see http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router.html */ -class Standard extends \Phalcon\Mvc\Router implements DI\InjectionAwareInterface +class Standard extends Router implements DI\InjectionAwareInterface { /** @@ -33,7 +34,7 @@ class Standard extends \Phalcon\Mvc\Router implements DI\InjectionAwareInterface * @param DiInterface $dependencyInjector * @param bool $keepDefaultRoutes */ - public function __construct(\Phalcon\DiInterface $dependencyInjector, $keepDefaultRoutes = false) + public function __construct(DiInterface $dependencyInjector, $keepDefaultRoutes = false) { parent::__construct($keepDefaultRoutes); $this->removeExtraSlashes(true); diff --git a/src/Mvc/Router/Exception/InvalidRouteNameException.php b/src/Mvc/Router/Exception/InvalidRouteNameException.php index e921799..a5cf0b3 100644 --- a/src/Mvc/Router/Exception/InvalidRouteNameException.php +++ b/src/Mvc/Router/Exception/InvalidRouteNameException.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Mvc/Router/Exception/InvalidRoutePathsException.php b/src/Mvc/Router/Exception/InvalidRoutePathsException.php index 60af60b..ca524e4 100644 --- a/src/Mvc/Router/Exception/InvalidRoutePathsException.php +++ b/src/Mvc/Router/Exception/InvalidRoutePathsException.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Mvc/Router/Route.php b/src/Mvc/Router/Route.php index 1844f76..b50593a 100644 --- a/src/Mvc/Router/Route.php +++ b/src/Mvc/Router/Route.php @@ -60,7 +60,7 @@ class Route /** * Constructor * - * @param $name Name of route + * @param $name Route name * @param $routeArray * @throws Exception\InvalidRouteNameException * @throws Exception\InvalidRoutePathsException diff --git a/src/Mvc/Router/Route/BaseRoute.php b/src/Mvc/Router/Route/BaseRoute.php index cb04bd7..539edff 100644 --- a/src/Mvc/Router/Route/BaseRoute.php +++ b/src/Mvc/Router/Route/BaseRoute.php @@ -13,6 +13,7 @@ namespace Vegas\Mvc\Router\Route; +use Phalcon\Mvc\RouterInterface; use Vegas\Mvc\Router\Route; use Vegas\Mvc\Router\RouteInterface; @@ -28,7 +29,7 @@ class BaseRoute implements RouteInterface /** * {@inheritdoc} */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) + public function add(RouterInterface $router, Route $route) { $router ->add($route->getRoute(), $route->getPaths()) diff --git a/src/Mvc/Router/Route/DefaultRoute.php b/src/Mvc/Router/Route/DefaultRoute.php index 0b2619f..049a1a2 100644 --- a/src/Mvc/Router/Route/DefaultRoute.php +++ b/src/Mvc/Router/Route/DefaultRoute.php @@ -13,6 +13,7 @@ namespace Vegas\Mvc\Router\Route; +use Phalcon\Mvc\RouterInterface; use Vegas\Mvc\Router\Route; use Vegas\Mvc\Router\RouteInterface; @@ -29,7 +30,7 @@ class DefaultRoute implements RouteInterface /** * {@inheritdoc} */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) + public function add(RouterInterface $router, Route $route) { $router->setDefaults(array_merge( $route->getPaths(), diff --git a/src/Mvc/Router/Route/NotfoundRoute.php b/src/Mvc/Router/Route/NotfoundRoute.php index 99b8846..8a904aa 100644 --- a/src/Mvc/Router/Route/NotfoundRoute.php +++ b/src/Mvc/Router/Route/NotfoundRoute.php @@ -13,6 +13,7 @@ namespace Vegas\Mvc\Router\Route; +use Phalcon\Mvc\RouterInterface; use Vegas\Mvc\Router\Route; use Vegas\Mvc\Router\RouteInterface; @@ -29,7 +30,7 @@ class NotfoundRoute implements RouteInterface /** * {@inheritdoc} */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) + public function add(RouterInterface $router, Route $route) { $router ->notFound($route->getPaths()); diff --git a/src/Mvc/Router/Route/RestRoute.php b/src/Mvc/Router/Route/RestRoute.php index e00e5c3..97dcfec 100644 --- a/src/Mvc/Router/Route/RestRoute.php +++ b/src/Mvc/Router/Route/RestRoute.php @@ -12,6 +12,7 @@ namespace Vegas\Mvc\Router\Route; +use Phalcon\Mvc\RouterInterface; use Vegas\Http\Method; use Vegas\Mvc\Router\Route; use Vegas\Mvc\Router\RouteInterface; @@ -27,7 +28,7 @@ class RestRoute implements RouteInterface /** * {@inheritdoc} */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) + public function add(RouterInterface $router, Route $route) { //resolves actions with http methods $actions = $route->getParam('actions'); diff --git a/src/Mvc/Router/Route/StaticRoute.php b/src/Mvc/Router/Route/StaticRoute.php index 27f3427..a7647b0 100644 --- a/src/Mvc/Router/Route/StaticRoute.php +++ b/src/Mvc/Router/Route/StaticRoute.php @@ -13,6 +13,7 @@ namespace Vegas\Mvc\Router\Route; +use Phalcon\Mvc\RouterInterface; use Vegas\Mvc\Router\Route; use Vegas\Mvc\Router\RouteInterface; @@ -27,7 +28,7 @@ class StaticRoute implements RouteInterface /** * {@inheritdoc} */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route) + public function add(RouterInterface $router, Route $route) { $router ->add($route->getRoute(), $route->getPaths()) diff --git a/src/Mvc/Router/RouteInterface.php b/src/Mvc/Router/RouteInterface.php index 1819730..f129e97 100644 --- a/src/Mvc/Router/RouteInterface.php +++ b/src/Mvc/Router/RouteInterface.php @@ -12,6 +12,7 @@ namespace Vegas\Mvc\Router; +use Phalcon\Mvc\RouterInterface; use Vegas\Mvc\Router\Route; /** @@ -24,9 +25,9 @@ interface RouteInterface /** * Methods adds new Route definition to indicated Router * - * @param \Phalcon\Mvc\RouterInterface $router + * @param RouterInterface $router * @param Route $route * @return mixed */ - public function add(\Phalcon\Mvc\RouterInterface $router, Route $route); + public function add(RouterInterface $router, Route $route); } \ No newline at end of file diff --git a/src/Mvc/View.php b/src/Mvc/View.php index de64ffa..404bfdb 100644 --- a/src/Mvc/View.php +++ b/src/Mvc/View.php @@ -13,6 +13,8 @@ namespace Vegas\Mvc; use Phalcon\Mvc\View as PhalconView; +use Phalcon\Mvc\View\Exception; +use Vegas\Mvc\View\Engine\Volt; /** * Class View @@ -55,7 +57,7 @@ public function __construct($options = null, $viewDir = null) { $this->registerEngines(array( '.volt' => function ($this, $di) use ($options) { - $volt = new \Vegas\Mvc\View\Engine\Volt($this, $di); + $volt = new Volt($this, $di); if (isset($options['cacheDir'])) { $volt->setOptions(array( 'compiledPath' => $options['cacheDir'], @@ -89,7 +91,8 @@ public function __construct($options = null, $viewDir = null) { * @param boolean $silence * @param boolean $mustClean * @param \Phalcon\Cache\BackendInterface $cache - * @throws PhalconView\Exception + * @return null|void + * @throws Exception */ protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cache) { @@ -110,7 +113,7 @@ protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cac $key = $cacheOptions['key']; } if (isset($cacheOptions['lifetime'])) { - $lifetime = $cacheOptions['lifetime']; + $lifeTime = $cacheOptions['lifetime']; } } @@ -118,7 +121,11 @@ protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cac $key = md5($viewPath); } - $cachedView = $cache->start($key, $lifetime); + if (!isset($lifeTime)) { + $lifeTime = 0; + } + + $cachedView = $cache->start($key, $lifeTime); if (!$cachedView) { $this->_content = $cachedView; return null; @@ -161,7 +168,7 @@ protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cac } if (!$silence) { - throw new \Phalcon\Mvc\View\Exception(sprintf("View %s was not found in the views directory", $viewEnginePath)); + throw new Exception(sprintf("View %s was not found in the views directory", $viewEnginePath)); } } } diff --git a/src/Mvc/View/Engine/Volt.php b/src/Mvc/View/Engine/Volt.php index a122339..570923e 100644 --- a/src/Mvc/View/Engine/Volt.php +++ b/src/Mvc/View/Engine/Volt.php @@ -17,7 +17,6 @@ use Vegas\Mvc\View\Engine\Volt\VoltFilterAbstract; use Vegas\Mvc\View\Engine\Volt\VoltHelperAbstract; - /** * Class Volt * @package Vegas\Mvc\View\Engine diff --git a/src/Mvc/View/Engine/Volt/Filter/ToString.php b/src/Mvc/View/Engine/Volt/Filter/ToString.php index e903773..2ed449e 100644 --- a/src/Mvc/View/Engine/Volt/Filter/ToString.php +++ b/src/Mvc/View/Engine/Volt/Filter/ToString.php @@ -12,7 +12,6 @@ namespace Vegas\Mvc\View\Engine\Volt\Filter; - use Vegas\Mvc\View\Engine\Volt\VoltFilterAbstract; /** diff --git a/src/Mvc/View/Engine/Volt/Helper/Pagination.php b/src/Mvc/View/Engine/Volt/Helper/Pagination.php index 65bb9d3..a7e9a91 100644 --- a/src/Mvc/View/Engine/Volt/Helper/Pagination.php +++ b/src/Mvc/View/Engine/Volt/Helper/Pagination.php @@ -12,7 +12,6 @@ namespace Vegas\Mvc\View\Engine\Volt\Helper; - use Vegas\Mvc\View\Engine\Volt\VoltHelperAbstract; /** diff --git a/src/Mvc/View/Engine/Volt/Helper/ShortenText.php b/src/Mvc/View/Engine/Volt/Helper/ShortenText.php index 1550e97..f1484cf 100644 --- a/src/Mvc/View/Engine/Volt/Helper/ShortenText.php +++ b/src/Mvc/View/Engine/Volt/Helper/ShortenText.php @@ -12,7 +12,6 @@ namespace Vegas\Mvc\View\Engine\Volt\Helper; - use Vegas\Mvc\View\Engine\Volt\VoltHelperAbstract; /** diff --git a/src/Mvc/View/Engine/Volt/VoltFilterAbstract.php b/src/Mvc/View/Engine/Volt/VoltFilterAbstract.php index 5c3b530..5ac738b 100644 --- a/src/Mvc/View/Engine/Volt/VoltFilterAbstract.php +++ b/src/Mvc/View/Engine/Volt/VoltFilterAbstract.php @@ -12,7 +12,6 @@ namespace Vegas\Mvc\View\Engine\Volt; - use Phalcon\Mvc\View\Engine\Volt\Compiler; /** diff --git a/src/Paginator/Adapter/Mongo.php b/src/Paginator/Adapter/Mongo.php index 2179ea1..9a767ab 100644 --- a/src/Paginator/Adapter/Mongo.php +++ b/src/Paginator/Adapter/Mongo.php @@ -12,6 +12,7 @@ namespace Vegas\Paginator\Adapter; use Phalcon\Paginator\AdapterInterface; +use Vegas\Paginator\Page; /** * Class Mongo @@ -113,7 +114,7 @@ private function validate() */ public function getPaginate() { - $page = new \Vegas\Paginator\Page(); + $page = new Page(); $page->current = $this->page; $page->next = $this->getNextPage(); diff --git a/src/Tag/Pagination.php b/src/Tag/Pagination.php index f059646..4609228 100644 --- a/src/Tag/Pagination.php +++ b/src/Tag/Pagination.php @@ -12,6 +12,7 @@ */ namespace Vegas\Tag; +use Phalcon\DI; /** * Class Pagination @@ -54,9 +55,9 @@ class Pagination /** * Constructor * Sets default settings - * @param \Phalcon\DI $di + * @param DI $di */ - public function __construct(\Phalcon\DI $di) + public function __construct(DI $di) { $this->di = $di; $this->settings = array( diff --git a/src/Task/AssetsTask.php b/src/Task/AssetsTask.php index a8ef40d..2a7db32 100644 --- a/src/Task/AssetsTask.php +++ b/src/Task/AssetsTask.php @@ -12,13 +12,15 @@ namespace Vegas\Task; +use Vegas\Cli\Task\Action; use Vegas\Cli\Task\Option; +use Vegas\Cli\Task; /** * Class AssetsTask * @package Vegas\Task */ -class AssetsTask extends \Vegas\Cli\Task +class AssetsTask extends Task { /** * Publishes assets provided by vegas-libraries installed via composer @@ -111,7 +113,7 @@ private function copyRecursive($source, $dest, $permissions = 0755) */ public function setOptions() { - $action = new \Vegas\Cli\Task\Action('publish', 'Publish all assets'); + $action = new Action('publish', 'Publish all assets'); $dir = new Option('dir', 'd', 'Assets directory. Usage vegas:assets publish -d /path/to/assets'); $action->addOption($dir); diff --git a/src/Task/CacheTask.php b/src/Task/CacheTask.php index 9c9711f..636b7b3 100644 --- a/src/Task/CacheTask.php +++ b/src/Task/CacheTask.php @@ -14,12 +14,13 @@ use Phalcon\DI; use Vegas\Cli\Task\Action; +use Vegas\Cli\Task; /** * Class CacheTask * @package Vegas\Task */ -class CacheTask extends \Vegas\Cli\Task +class CacheTask extends Task { /** * Cleans application cache diff --git a/src/Test/Bootstrap.php b/src/Test/Bootstrap.php new file mode 100644 index 0000000..40ab941 --- /dev/null +++ b/src/Test/Bootstrap.php @@ -0,0 +1,19 @@ + + * @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\Test; + +class Bootstrap +{ + +} + \ No newline at end of file diff --git a/src/Test/Http/Request.php b/src/Test/Http/Request.php new file mode 100644 index 0000000..988fa32 --- /dev/null +++ b/src/Test/Http/Request.php @@ -0,0 +1,290 @@ + + * @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\Test\Http; + +/** + * Class Request + * @package Vegas\Test\Http + */ +class Request extends \Phalcon\Http\Request +{ + /** + * Sets request raw body + * + * @param $rawBody + * @return $this + */ + public function setRawBody($rawBody) + { + $this->_rawBody = $rawBody; + return $this; + } + + /** + * Sets $_POST parameter + * + * @param $name + * @param $value + * @return $this + */ + public function setPost($name, $value) + { + $_POST[$name] = $value; + return $this; + } + + /** + * Sets $_REQUEST parameter + * + * @param $name + * @param $value + * @return $this + */ + public function setRequestParameter($name, $value) + { + $_REQUEST[$name] = $value; + return $this; + } + + /** + * Sets $_PUT parameter + * + * @param $name + * @param $value + * @return $this + */ + public function setQuery($name, $value) + { + $_GET[$name] = $value; + return $this; + } + + /** + * Sets $_SERVER parameter + * + * @param $name + * @param $value + * @return $this + */ + public function setServer($name, $value) + { + $_SERVER[$name] = $value; + return $this; + } + + /** + * Sets request header + * + * @param $name + * @param $value + * @return $this + */ + public function setHeader($name, $value) + { + $this->setServer($name, $value); + return $this; + } + + /** + * Sets and converts raw body to JSON + * + * @param $rawBody + * @return $this + */ + public function setJsonRawBody($rawBody) + { + $this->_rawBody = json_encode($rawBody); + return $this; + } + + /** + * Sets request HTTP HOST + * + * @param $httpHost + * @return $this + */ + public function setHttpHost($httpHost) + { + $this->setServer('HTTP_HOST', $httpHost); + return $this; + } + + /** + * Sets request PORT + * + * @param $port + * @return $this + */ + public function setPort($port) + { + $this->setServer('SERVER_PORT', $port); + return $this; + } + + /** + * Sets server names + * + * @param $serverName + * @return $this + */ + public function setServerName($serverName) + { + $this->setServer('SERVER_NAME', $serverName); + return $this; + } + + /** + * Sets request URI + * + * @param $uri + * @return $this + */ + public function setRequestUri($uri) + { + $this->setServer('REQUEST_URI', $uri); + return $this; + } + + /** + * Sets remote address + * + * @param $remoteAddress + * @return $this + */ + public function setRemoteAddress($remoteAddress) + { + $this->setServer('REMOTE_ADDR', $remoteAddress); + return $this; + } + + /** + * Sets request method + * + * @param $method + * @return $this + */ + public function setRequestMethod($method) + { + $this->setServer('REQUEST_METHOD', $method); + return $this; + } + + /** + * Sets HTTP User-Agent + * + * @param $userAgent + * @return $this + */ + public function setHttpUserAgent($userAgent) + { + $this->setServer('HTTP_USER_AGENT', $userAgent); + return $this; + } + + /** + * Sets http referer + * + * @param $httpReferer + * @return $this + */ + public function setHttpReferer($httpReferer) + { + $this->setServer('HTTP_REFERER', $httpReferer); + return $this; + } + + /** + * Sets HTTP_ACCEPT + * + * @param $httpAccept + * @return $this + */ + public function setHttpAccept($httpAccept) + { + $this->setServer('HTTP_ACCEPT', $httpAccept); + return $this; + } + + /** + * Sets basic Auth + * + * @param $user + * @param $password + * @return $this + */ + public function setBasicAuth($user, $password) + { + $this->setServer('PHP_AUTH_USER', $user); + $this->setServer('PHP_AUTH_PW', $password); + return $this; + } + + /** + * Sets digest Auth + * + * @param $digestAuth + * @return $this + */ + public function setDigestAuth($digestAuth) + { + $this->setServer('PHP_AUTH_DIGEST', $digestAuth); + return $this; + } + + /** + * Sets accept language + * + * @param $acceptLanguage + * @return $this + */ + public function setAcceptLanguage($acceptLanguage) + { + $this->setServer('HTTP_ACCEPT_LANGUAGE', $acceptLanguage); + return $this; + } + + /** + * Sets accept charset + * + * @param $acceptCharset + * @return $this + */ + public function setAcceptCharset($acceptCharset) + { + $this->setServer('HTTP_ACCEPT_CHARSET', $acceptCharset); + return $this; + } + + /** + * Sets request Content-Type + * + * @param $contentType + * @return $this + */ + public function setContentType($contentType) + { + $this->setHeader('Content-Type', $contentType); + return $this; + } + + /** + * Sets Ajax request + * + * @return $this + */ + public function setAjaxRequest() + { + $this->setServer('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'); + return $this; + } +} \ No newline at end of file diff --git a/src/Test/TestCase.php b/src/Test/TestCase.php new file mode 100644 index 0000000..d2c1364 --- /dev/null +++ b/src/Test/TestCase.php @@ -0,0 +1,85 @@ + + * @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\Test; + +class TestCase extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Bootstrap + */ + protected $bootstrap; + protected $di; + + /** + * Constructs a test case with the given name. + * + * @param string $name + * @param array $data + * @param string $dataName + */ + public function __construct($name = null, array $data = [], $dataName = '') + { + parent::__construct($name, $data, $dataName); + + $loader = new \Phalcon\Loader(); + + $loader->registerNamespaces([ + 'Tests' => APP_ROOT . '/tests', + ], true); + + $loader->registerDirs([APP_ROOT. '/app'], true); + $loader->register(); + + $config = require APP_ROOT . '/tests/config.php'; + + $bootstrap = new \Bootstrap(new \Phalcon\Config($config)); + + $this->di = $bootstrap->getDI(); + $this->di->set('request', function() { + return new \Vegas\Test\Http\Request(); + }, true); + + $bootstrap->setup(); + $this->bootstrap = $bootstrap; + + } + + /** + * Handles URI + * + * @param $uri + * @return \Phalcon\Http\ResponseInterface + */ + public function handle($uri) + { + $response = $this->bootstrap->getApplication()->handle($uri); + return $response; + } + + /** + * @return \Vegas\Test\Http\Request + */ + protected function request() + { + return $this->di->get('request'); + } + + /** + * @return \Phalcon\DI\FactoryDefault|\Phalcon\DiInterface + */ + public function getDI() + { + return $this->di; + } +} + \ No newline at end of file diff --git a/src/Translate/Adapter/Gettext.php b/src/Translate/Adapter/Gettext.php index 77c831b..766aed0 100644 --- a/src/Translate/Adapter/Gettext.php +++ b/src/Translate/Adapter/Gettext.php @@ -6,7 +6,7 @@ namespace Vegas\Translate\Adapter; use Phalcon\Translate\Adapter; -use Phalcon\Translate\Adapter\Gettext As PhalconGettext; +use Phalcon\Translate\Adapter\Gettext as PhalconGettext; use Phalcon\Translate\Exception; class Gettext extends PhalconGettext diff --git a/tests/Mvc/Router/RouteTest.php b/tests/Mvc/Router/RouteTest.php index 7797498..edda6f5 100644 --- a/tests/Mvc/Router/RouteTest.php +++ b/tests/Mvc/Router/RouteTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Util/FileWriterTest.php b/tests/Util/FileWriterTest.php index 80b039a..09f59bb 100644 --- a/tests/Util/FileWriterTest.php +++ b/tests/Util/FileWriterTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * From 9fe55597190888a142707eb1b2d88baf72493e8c Mon Sep 17 00:00:00 2001 From: slawek Date: Thu, 11 Dec 2014 16:24:02 +0100 Subject: [PATCH 25/35] Bootstrap rewrite; General refactoring; Added Vegas\Test tools; Tests refactoring; Code inspection; --- .gitignore | 3 +- src/Application/Bootstrap.php | 241 ------------------ src/Bootstrap/EnvironmentInitializerTrait.php | 40 +++ src/Bootstrap/LoaderInitializerTrait.php | 37 +++ src/Bootstrap/ModulesInitializerTrait.php | 82 ++++++ src/Bootstrap/RoutesInitializerTrait.php | 55 ++++ src/Bootstrap/ServicesInitializerTrait.php | 41 +++ src/BootstrapInterface.php | 10 + src/Cli/Bootstrap.php | 123 ++++----- src/Cli/EventsListener/TaskListener.php | 4 +- src/Cli/OptionParser.php | 35 +-- src/DI/ServiceManager.php | 2 + src/DI/ServiceProviderLoader.php | 65 +++-- src/Db/Decorator/ModelAbstract.php | 2 +- src/Mvc/Bootstrap.php | 68 ++++- .../Dispatcher/Events/ExceptionListener.php | 43 ++++ .../Exception/InvalidModulesListException.php | 25 ++ src/Mvc/Module/Loader.php | 35 +-- src/Mvc/ModuleAbstract.php | 9 +- src/Test/Bootstrap.php | 176 ++++++++++++- src/Test/Http/Request.php | 3 +- src/Test/TestCase.php | 49 ++-- src/Util/FileWriter.php | 24 +- tests/App/TestCase.php | 2 +- tests/Cli/BootstrapTest.php | 28 +- tests/DI/ScaffoldingTest.php | 20 ++ tests/DI/Service/ComponentTest.php | 10 +- tests/DI/ServiceManagerTest.php | 12 + tests/DI/ServiceProviderLoaderTest.php | 12 +- .../Adapter/Decorator/ModelAbstractTest.php | 10 + tests/Db/MappingTest.php | 44 ++-- tests/Mvc/ApplicationTest.php | 9 +- tests/Mvc/Controller/CrudTest.php | 126 ++++----- tests/Mvc/ControllerAbstractTest.php | 41 ++- tests/Mvc/Module/LoaderTest.php | 8 +- tests/Mvc/ModuleAbstractTest.php | 2 +- tests/Mvc/Router/Route/NotFoundRouteTest.php | 1 - tests/Mvc/Router/Route/RestRouteTest.php | 2 - tests/Mvc/Router/Route/StaticRouteTest.php | 1 - tests/Mvc/RouterTest.php | 8 +- tests/Mvc/View/Engine/VoltTest.php | 2 +- tests/Mvc/ViewTest.php | 69 ++--- tests/Tag/PaginationTest.php | 3 +- tests/Task/AssetsTest.php | 1 - tests/Task/CacheTest.php | 1 - tests/Util/FileWriterTest.php | 2 +- tests/bootstrap.php | 15 +- .../config/config.php => config.sample.php} | 5 +- tests/fixtures/app/config/routes.php | 11 + .../controllers/frontend/HomeController.php | 23 ++ .../controllers/frontend/FooController.php | 8 + .../app/modules/Test/services/Fake.php | 1 - tests/fixtures/app/plugins/Foo.php | 2 +- .../services/FlashSessionServiceProvider.php | 2 +- .../app/services/I18nServiceProvider.php | 4 +- .../services/ScaffoldingServiceProvider.php | 2 +- 56 files changed, 1039 insertions(+), 620 deletions(-) delete mode 100644 src/Application/Bootstrap.php create mode 100644 src/Bootstrap/EnvironmentInitializerTrait.php create mode 100644 src/Bootstrap/LoaderInitializerTrait.php create mode 100644 src/Bootstrap/ModulesInitializerTrait.php create mode 100644 src/Bootstrap/RoutesInitializerTrait.php create mode 100644 src/Bootstrap/ServicesInitializerTrait.php create mode 100644 src/Mvc/Dispatcher/Events/ExceptionListener.php create mode 100644 src/Mvc/Module/Exception/InvalidModulesListException.php rename tests/{fixtures/app/config/config.php => config.sample.php} (88%) create mode 100644 tests/fixtures/app/config/routes.php create mode 100644 tests/fixtures/app/modules/Example/controllers/frontend/HomeController.php diff --git a/.gitignore b/.gitignore index 6d2566c..ead9853 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ com_crashlytics_export_strings.xml /vendor/ composer.phar composer.lock -travis.log \ No newline at end of file +travis.log +tests/config.php \ No newline at end of file diff --git a/src/Application/Bootstrap.php b/src/Application/Bootstrap.php deleted file mode 100644 index 3f9e661..0000000 --- a/src/Application/Bootstrap.php +++ /dev/null @@ -1,241 +0,0 @@ - - * @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\Application; - -use Phalcon\Config; -use Phalcon\DI\FactoryDefault; -use Phalcon\DI; -use Phalcon\DiInterface; -use Phalcon\Mvc\Dispatcher; -use Vegas\BootstrapInterface; -use Vegas\Constants; -use Vegas\DI\ServiceProviderLoader; -use Vegas\Mvc\Application; -use Vegas\Mvc\Dispatcher\Events\BeforeException; -use Vegas\Mvc\Module\Loader as ModuleLoader; -use Vegas\Mvc\Module\Loader; -use Vegas\Mvc\Router\Adapter\Standard; -use Vegas\Mvc\Router; - -/** - * Class Bootstrap - * - * Bootstraps mvc application - * - * @package Vegas\Application - */ -class Bootstrap implements BootstrapInterface -{ - /** - * Dependency injection - * - * @var DiInterface - */ - protected $di; - - /** - * MVC Application - * - * @var Application - */ - protected $application; - - /** - * Application config - * - * @var Config - */ - protected $config; - - /** - * Constructor - * Initializes MVC Application - * Initializes DI for Application - * - * @param Config $config - */ - public function __construct(Config $config) - { - $this->config = $config; - $this->di = new FactoryDefault(); - $this->application = new Application(); - } - - /** - * Sets Dependency Injector - * - * @param DiInterface $di - */ - public function setDi(DiInterface $di) - { - $this->di = $di; - } - - /** - * Returns Dependency Injector - * - * @return FactoryDefault|DiInterface - */ - public function getDI() - { - return $this->di; - } - - /** - * Initializes application environment - */ - protected function initEnvironment() - { - if (isset($this->config->application->environment)) { - $env = $this->config->application->environment; - } else { - $env = Constants::DEFAULT_ENV; - } - - if (!defined('APPLICATION_ENV')) { - define('APPLICATION_ENV', $env); - } - - $this->di->set('environment', function() use ($env) { - return $env; - }, true); - } - - /** - * Initializes loader - * Registers library and plugin directory - */ - protected function initLoader() - { - $loader = new \Phalcon\Loader(); - $loader->registerDirs( - array( - $this->config->application->libraryDir, - $this->config->application->pluginDir - ) - )->register(); - } - - /** - * Initializes application modules - */ - protected function initModules() - { - //registers modules defined in modules.php file - $modulesFile = $this->config->application->configDir . Loader::MODULE_STATIC_FILE; - if (!file_exists($modulesFile) || $this->di->get('environment') != Constants::DEFAULT_ENV) { - ModuleLoader::dump($this->di); - } - $this->application->registerModules(require $modulesFile); - - //prepares modules configurations - foreach ($this->application->getModules() as $module) { - $moduleConfigFile = dirname($module['path']) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'; - if (file_exists($moduleConfigFile)) { - $this->config->merge(require $moduleConfigFile); - } - } - - $this->di->set('modules', function() { - return $this->application->getModules(); - }); - } - - /** - * Initializes services - */ - protected function initServices() - { - ServiceProviderLoader::autoload($this->di); - } - - /** - * Initializes routing - */ - protected function initRoutes() - { - //setups router - $routerAdapter = new Standard($this->di); - $router = new Router($this->di, $routerAdapter); - - //adds routes defined in modules - $modules = $this->application->getModules(); - foreach ($modules as $module) { - $router->addModuleRoutes($module); - } - - //adds routes defined in default file - $defaultRoutesFile = $this->config->application->configDir . DIRECTORY_SEPARATOR . 'routes.php'; - if (file_exists($defaultRoutesFile)) { - $router->addRoutes(require $defaultRoutesFile); - } - - //setup router rules - $router->setup(); - - $this->di->set('router', $router->getRouter()); - } - - /** - * Registers default dispatcher - */ - protected function initDispatcher() - { - $this->di->set('dispatcher', function() { - $dispatcher = new Dispatcher(); - - /** - * @var \Phalcon\Events\Manager $eventsManager - */ - $eventsManager = $this->di->getShared('eventsManager'); - $eventsManager->attach('dispatch:beforeException', BeforeException::getEvent()); - - $dispatcher->setEventsManager($eventsManager); - - return $dispatcher; - }); - } - - /** - * Setups application - * - * @return $this - */ - public function setup() - { - $this->di->set('config', $this->config); - - $this->initEnvironment(); - $this->initLoader(); - $this->initModules(); - $this->initRoutes(); - $this->initServices(); - $this->initDispatcher(); - - $this->application->setDI($this->di); - DI::setDefault($this->di); - - return $this; - } - - /** - * Start handling MVC requests - * - * @param string $uri - * @return string - */ - public function run($uri = null) - { - return $this->application->handle($uri)->getContent(); - } -} \ No newline at end of file diff --git a/src/Bootstrap/EnvironmentInitializerTrait.php b/src/Bootstrap/EnvironmentInitializerTrait.php new file mode 100644 index 0000000..b034f21 --- /dev/null +++ b/src/Bootstrap/EnvironmentInitializerTrait.php @@ -0,0 +1,40 @@ + + * @copyright (c) 2014, Amsterdam Standard + */ + +namespace Vegas\Bootstrap; + +use Phalcon\Config; +use Vegas\Constants; + +trait EnvironmentInitializerTrait +{ + + /** + * Initializes application environment + */ + protected function initEnvironment(Config $config) + { + if (isset($config->application->environment)) { + $env = $config->application->environment; + } else { + $env = Constants::DEFAULT_ENV; + } + + if (!defined('APPLICATION_ENV')) { + define('APPLICATION_ENV', $env); + } + + $this->getDI()->set('environment', function() use ($env) { + return $env; + }, true); + } + + /** + * @return mixed + */ + abstract public function getDI(); +} + \ No newline at end of file diff --git a/src/Bootstrap/LoaderInitializerTrait.php b/src/Bootstrap/LoaderInitializerTrait.php new file mode 100644 index 0000000..c87b5a2 --- /dev/null +++ b/src/Bootstrap/LoaderInitializerTrait.php @@ -0,0 +1,37 @@ + + * @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\Bootstrap; + +use Phalcon\Config; +use Phalcon\Loader; + +trait LoaderInitializerTrait +{ + + /** + * Initializes loader + * Registers library and plugin directory + */ + protected function initLoader(Config $config) + { + $loader = new Loader(); + $loader->registerDirs( + array( + $config->application->libraryDir, + $config->application->pluginDir, + $config->application->taskDir + ) + )->register(); + } +} + \ No newline at end of file diff --git a/src/Bootstrap/ModulesInitializerTrait.php b/src/Bootstrap/ModulesInitializerTrait.php new file mode 100644 index 0000000..1ef67e1 --- /dev/null +++ b/src/Bootstrap/ModulesInitializerTrait.php @@ -0,0 +1,82 @@ + + * @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\Bootstrap; + +use Phalcon\Config; +use Phalcon\DI\FactoryDefault; +use Phalcon\DI; +use Phalcon\Mvc\Dispatcher; +use Vegas\Constants; +use Vegas\Mvc\Module\Exception\InvalidModulesListException; +use Vegas\Mvc\Module\Loader as ModuleLoader; +use Vegas\Mvc\Module\Loader; +use Vegas\Mvc\Router; + +trait ModulesInitializerTrait +{ + /** + * Initializes application modules + */ + protected function initModules(Config $config) + { + $moduleLoader = new ModuleLoader($this->getDI()); + //registers modules defined in modules.php file + $modulesFile = $config->application->configDir + . Loader::MODULE_STATIC_FILE; + /** + * For non-default environment modules are being dumped in each application start + */ + if (!file_exists($modulesFile) || $this->getDI()->get('environment') != Constants::DEFAULT_ENV) { + $modules = $moduleLoader->dump( + $config->application->moduleDir, + $config->application->configDir + ); + } else { + $modules = require $modulesFile; + } + if (!is_array($modules)) { + throw new InvalidModulesListException(); + } + $this->getApplication()->registerModules($modules); + + //prepares modules configurations + foreach ($this->getApplication()->getModules() as $module) { + $moduleConfigFile = dirname($module['path']) + . DIRECTORY_SEPARATOR + . 'config' + . DIRECTORY_SEPARATOR + . 'config.php'; + if (file_exists($moduleConfigFile)) { + $moduleConfig = require $moduleConfigFile; + if (is_array($moduleConfig)) { + $config->merge($moduleConfig); + } + } + } + + $this->getDI()->set('modules', function() { + return $this->getApplication()->getModules(); + }); + } + + /** + * @return mixed + */ + abstract public function getDI(); + + /** + * @return mixed + */ + abstract public function getApplication(); +} + \ No newline at end of file diff --git a/src/Bootstrap/RoutesInitializerTrait.php b/src/Bootstrap/RoutesInitializerTrait.php new file mode 100644 index 0000000..ea01d4b --- /dev/null +++ b/src/Bootstrap/RoutesInitializerTrait.php @@ -0,0 +1,55 @@ + + * @copyright (c) 2014, Amsterdam Standard + */ + +namespace Vegas\Bootstrap; + +use Phalcon\Config; +use Phalcon\DI\FactoryDefault; +use Phalcon\DI; +use Phalcon\Mvc\Dispatcher; +use Vegas\Mvc\Router\Adapter\Standard; +use Vegas\Mvc\Router; + +trait RoutesInitializerTrait +{ + /** + * Initializes routing + */ + protected function initRoutes(Config $config) + { + //setups router + $routerAdapter = new Standard($this->getDI()); + $router = new Router($this->getDI(), $routerAdapter); + + //adds routes defined in modules + $modules = $this->getApplication()->getModules(); + foreach ($modules as $module) { + $router->addModuleRoutes($module); + } + + //adds routes defined in default file + $defaultRoutesFile = $config->application->configDir + . DIRECTORY_SEPARATOR + . 'routes.php'; + if (file_exists($defaultRoutesFile)) { + $routes = require $defaultRoutesFile; + if (is_array($routes)) { + $router->addRoutes($routes); + } + } + + //setup router rules + $router->setup(); + + //registers router into DI + $this->getDI()->set('router', $router->getRouter()); + } + + abstract public function getApplication(); + + abstract public function getDI(); +} + \ No newline at end of file diff --git a/src/Bootstrap/ServicesInitializerTrait.php b/src/Bootstrap/ServicesInitializerTrait.php new file mode 100644 index 0000000..010c5fc --- /dev/null +++ b/src/Bootstrap/ServicesInitializerTrait.php @@ -0,0 +1,41 @@ + + * @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\Bootstrap; + +use Phalcon\Config; +use Vegas\DI\ServiceProviderLoader; + +trait ServicesInitializerTrait +{ + /** + * Initializes services + */ + protected function initServices(Config $config) + { + $serviceProviderLoader = new ServiceProviderLoader($this->getDI()); + $serviceProviderLoader->autoload( + $config->application->serviceDir, + $config->application->configDir + ); + } + + /** + * @return mixed + */ + abstract public function getApplication(); + + /** + * @return mixed + */ + abstract public function getDI(); +} \ No newline at end of file diff --git a/src/BootstrapInterface.php b/src/BootstrapInterface.php index b47f9fe..4ab6b6d 100644 --- a/src/BootstrapInterface.php +++ b/src/BootstrapInterface.php @@ -25,6 +25,16 @@ interface BootstrapInterface */ public function setup(); + /** + * @return mixed + */ + public function getApplication(); + + /** + * @return mixed + */ + public function getDI(); + /** * Runs application * diff --git a/src/Cli/Bootstrap.php b/src/Cli/Bootstrap.php index 48da2d5..16c087b 100644 --- a/src/Cli/Bootstrap.php +++ b/src/Cli/Bootstrap.php @@ -14,12 +14,16 @@ use Phalcon\Config; use Phalcon\DI\FactoryDefault\CLI; +use Phalcon\DI\FactoryDefault; +use Phalcon\DiInterface; +use Phalcon\Loader as PhalconLoader; +use Vegas\Bootstrap\EnvironmentInitializerTrait; +use Vegas\Bootstrap\LoaderInitializerTrait; +use Vegas\Bootstrap\ModulesInitializerTrait; +use Vegas\Bootstrap\ServicesInitializerTrait; use Vegas\BootstrapInterface; use Vegas\Cli\EventsListener\TaskListener; use Vegas\Cli\Exception as CliException; -use Vegas\Constants; -use Vegas\DI\ServiceProviderLoader; -use Vegas\Mvc\Module\Loader as ModuleLoader; /** * Class Bootstrap @@ -27,6 +31,16 @@ */ class Bootstrap implements BootstrapInterface { + use ModulesInitializerTrait { + initModules as baseInitModule; + } + + use ServicesInitializerTrait; + + use EnvironmentInitializerTrait; + + use LoaderInitializerTrait; + /** * Application arguments * @@ -47,84 +61,54 @@ public function __construct(Config $config) { $this->config = $config; $this->di = new CLI(); - $this->console = new Console(); + $this->application = new Console(); } - /** - * Initializes loader - * Registers library and plugin directory + * Sets Dependency Injector + * + * @param DiInterface $di */ - protected function initLoader() + public function setDI(DiInterface $di) { - $loader = new \Phalcon\Loader(); - $loader->registerDirs( - array( - $this->config->application->libraryDir, - $this->config->application->pluginDir, - $this->config->application->taskDir - ) - )->register(); + $this->di = $di; } /** - * Initializes application environment + * Returns Dependency Injector + * + * @return CLI|DiInterface */ - protected function initEnvironment() + public function getDI() { - if (isset($this->config->application->environment)) { - $env = $this->config->application->environment; - } else { - $env = Constants::DEFAULT_ENV; - } - - if (!defined('APPLICATION_ENV')) { - define('APPLICATION_ENV', $env); - } + return $this->di; + } - $this->di->set('environment', function() use ($env) { - return $env; - }, true); + /** + * @return Console + */ + public function getApplication() + { + return $this->application; } /** * Initializes application modules */ - protected function initModules() + protected function initModules(Config $config) { - //registers modules defined in modules.php file - $modulesFile = $this->config->application->configDir . 'modules.php'; - if (!file_exists($modulesFile) || $this->di->get('environment') != Constants::DEFAULT_ENV) { - ModuleLoader::dump($this->di); - } - $this->console->registerModules(require $modulesFile); + $this->baseInitModule($config); $namespaces = array(); //prepares modules configurations and modules task namespace - foreach ($this->console->getModules() as $moduleName => $module) { - $moduleConfigFile = dirname($module['path']) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'; - if (file_exists($moduleConfigFile)) { - $this->config->merge(require $moduleConfigFile); - } - - $namespaces[$moduleName . '\Tasks'] = dirname($module['path']) . DIRECTORY_SEPARATOR . 'tasks'; + foreach ($this->application->getModules() as $moduleName => $module) { + $namespaces[$moduleName . '\Tasks'] = dirname($module['path']) + . DIRECTORY_SEPARATOR . 'tasks'; } //registers module's tasks directories - $loader = new \Phalcon\Loader(); + $loader = new PhalconLoader(); $loader->registerNamespaces($namespaces, true); $loader->register(); - - $this->di->set('modules', function() { - return $this->console->getModules(); - }); - } - - /** - * Initializes services - */ - protected function initServices() - { - ServiceProviderLoader::autoload($this->di); } @@ -143,16 +127,18 @@ public function setArguments($args) */ protected function initEventsManager() { + $taskListener = new TaskListener(); + //extracts default events manager $eventsManager = $this->di->getShared('eventsManager'); //attaches new event console:beforeTaskHandle and console:afterTaskHandle $eventsManager->attach( - 'console:beforeHandleTask', TaskListener::beforeHandleTask($this->arguments) + 'console:beforeHandleTask', $taskListener->beforeHandleTask($this->arguments) ); $eventsManager->attach( - 'console:afterHandleTask', TaskListener::afterHandleTask() + 'console:afterHandleTask', $taskListener->afterHandleTask() ); - $this->console->setEventsManager($eventsManager); + $this->application->setEventsManager($eventsManager); } /** @@ -162,13 +148,13 @@ public function setup() { $this->di->set('config', $this->config); - $this->initEnvironment(); - $this->initLoader(); - $this->initModules(); - $this->initServices(); + $this->initEnvironment($this->config); + $this->initLoader($this->config); + $this->initModules($this->config); + $this->initServices($this->config); $this->initEventsManager(); - $this->console->setDI($this->di); + $this->application->setDI($this->di); return $this; } @@ -178,9 +164,8 @@ public function setup() public function run() { $argumentParser = new Loader(); - $arguments = $argumentParser->parseArguments($this->console, $this->arguments); + $arguments = $argumentParser->parseArguments($this->application, $this->arguments); - $this->console->handle($arguments); + $this->application->handle($arguments); } -} - \ No newline at end of file +} \ No newline at end of file diff --git a/src/Cli/EventsListener/TaskListener.php b/src/Cli/EventsListener/TaskListener.php index f713a55..9368a39 100644 --- a/src/Cli/EventsListener/TaskListener.php +++ b/src/Cli/EventsListener/TaskListener.php @@ -30,7 +30,7 @@ class TaskListener * @param $argv * @return callable */ - public static function beforeHandleTask($argv) + public function beforeHandleTask($argv) { return function(Event $event, Console $console, Dispatcher $dispatcher) use ($argv) { //parse parameters @@ -49,7 +49,7 @@ public static function beforeHandleTask($argv) * * @return callable */ - public static function afterHandleTask() + public function afterHandleTask() { return function(Event $event, Console $console, Task $task) { echo $task->getOutput(); diff --git a/src/Cli/OptionParser.php b/src/Cli/OptionParser.php index e78b889..a72692e 100644 --- a/src/Cli/OptionParser.php +++ b/src/Cli/OptionParser.php @@ -21,13 +21,7 @@ */ class OptionParser { - /** - * Array of parsed arguments - * - * @var array - * @internal - */ - private static $args; + public static $args; /** * PARSE ARGUMENTS @@ -73,26 +67,24 @@ class OptionParser * @since August 21, 2009 * @see https://github.com/pwfisher/CommandLine.php * @see http://www.php.net/manual/en/features.commandline.php - * @usage $args = OptionParser::parse($argv); + * #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008 + * #78651 function getArgs($args) by B Crawford, 22-Oct-2007 + * @usage $args = CommandLine::parseArgs($_SERVER['argv']); + * @codeCoverageIgnore */ public static function parse($argv = null) { $argv = $argv ? $argv : $_SERVER['argv']; - array_shift($argv); $out = array(); - for ($i = 0, $j = count($argv); $i < $j; $i++) { $arg = $argv[$i]; - // --foo --bar=baz if (substr($arg, 0, 2) === '--') { $eqPos = strpos($arg, '='); - // --foo if ($eqPos === false) { $key = substr($arg, 2); - // --foo value if ($i + 1 < $j && $argv[$i + 1][0] !== '-') { $value = $argv[$i + 1]; @@ -134,36 +126,26 @@ public static function parse($argv = null) $out[] = $value; } } - self::$args = $out; - return $out; } /** - * Converts string argument value to strict boolean value - * - * @author Patrick Fisher - * @since August 21, 2009 - * @see https://github.com/pwfisher/CommandLine.php - * @see http://www.php.net/manual/en/features.commandline.php - * @usage $args = OptionParser::parse($argv); + * GET BOOLEAN + * @codeCoverageIgnore */ - public static function toBoolean($key, $default = false) + public static function getBoolean($key, $default = false) { if (!isset(self::$args[$key])) { return $default; } $value = self::$args[$key]; - if (is_bool($value)) { return $value; } - if (is_int($value)) { return (bool)$value; } - if (is_string($value)) { $value = strtolower($value); $map = array( @@ -182,7 +164,6 @@ public static function toBoolean($key, $default = false) return $map[$value]; } } - return $default; } } \ No newline at end of file diff --git a/src/DI/ServiceManager.php b/src/DI/ServiceManager.php index 0eb2cac..d479e14 100644 --- a/src/DI/ServiceManager.php +++ b/src/DI/ServiceManager.php @@ -86,6 +86,8 @@ public function hasService($name) return !empty($service); } catch (\Phalcon\DI\Exception $ex) { return false; + } catch (\Vegas\DI\Service\Exception $ex) { + return false; } } diff --git a/src/DI/ServiceProviderLoader.php b/src/DI/ServiceProviderLoader.php index 7a20df7..fd426d2 100644 --- a/src/DI/ServiceProviderLoader.php +++ b/src/DI/ServiceProviderLoader.php @@ -13,6 +13,7 @@ namespace Vegas\DI; use Phalcon\Config; +use Phalcon\DI\InjectionAwareInterface; use Phalcon\DiInterface; use Phalcon\Loader; use Vegas\Constants; @@ -22,35 +23,45 @@ * Class ServiceProviderLoader * @package Vegas\DI */ -class ServiceProviderLoader +class ServiceProviderLoader implements InjectionAwareInterface { + use InjectionAwareTrait; + /** * Name of file containing static list of services */ const SERVICES_STATIC_FILE = 'services.php'; + /** + * @param DiInterface $di + */ + public function __construct(DiInterface $di) + { + $this->setDI($di); + } + /** * Dumps services to source file * - * @param DiInterface $di + * @param string $inputDirectory + * @param string $outputDirectory * @return array */ - public static function dump(DiInterface $di) + public function dump($inputDirectory, $outputDirectory) { - $config = $di->get('config'); $servicesList = array(); //browses directory for searching service provider classes - $directoryIterator = new \DirectoryIterator($config->application->serviceDir); + $directoryIterator = new \DirectoryIterator($inputDirectory); foreach ($directoryIterator as $fileInfo) { if ($fileInfo->isDot()) continue; $servicesList[$fileInfo->getBasename('.php')] = $fileInfo->getPathname(); } //saves generated array to php source file - FileWriter::write( - $config->application->configDir . self::SERVICES_STATIC_FILE, - self::createFileContent($servicesList), + FileWriter::writeObject( + $outputDirectory . self::SERVICES_STATIC_FILE, + $servicesList, true ); @@ -58,36 +69,20 @@ public static function dump(DiInterface $di) return $servicesList; } - /** - * @param $servicesList - * @return string - * @internal - */ - private static function createFileContent($servicesList) - { - return 'get('config'); - $configDir = $config->application->configDir; - if (!file_exists($configDir . self::SERVICES_STATIC_FILE) - || $di->get('environment') != Constants::DEFAULT_ENV) { - $services = self::dump($di); + if (!file_exists($outputDirectory . self::SERVICES_STATIC_FILE) + || $this->di->get('environment') != Constants::DEFAULT_ENV) { + $services = self::dump($inputDirectory, $outputDirectory); } else { - $services = require($configDir . self::SERVICES_STATIC_FILE); + $services = require($outputDirectory . self::SERVICES_STATIC_FILE); } - self::setupServiceProvidersAutoloader($config, $services); + self::setupServiceProvidersAutoloader($inputDirectory, $services); //resolves services dependencies $dependencies = array(); @@ -121,18 +116,18 @@ public static function autoload(DiInterface $di) //registers ordered dependencies foreach ($dependencies as $serviceProviderName => $dependency) { - $servicesProviders[$serviceProviderName]->register($di); + $servicesProviders[$serviceProviderName]->register($this->di); } } /** * Registers classes that contains services providers * - * @param Config $config - * @param $services + * @param string $inputDirectory + * @param array $services * @internal */ - private static function setupServiceProvidersAutoloader(Config $config, $services) + private function setupServiceProvidersAutoloader($inputDirectory, array $services) { //creates the autoloader $loader = new Loader(); @@ -140,7 +135,7 @@ private static function setupServiceProvidersAutoloader(Config $config, $service //setup default path when is not defined foreach ($services as $className => $path) { if (!$path) { - $services[$className] = $config->application->serviceDir . sprintf('%s.php', $className); + $services[$className] = $inputDirectory . sprintf('%s.php', $className); } } $loader->registerClasses($services, true); diff --git a/src/Db/Decorator/ModelAbstract.php b/src/Db/Decorator/ModelAbstract.php index f41b1da..5edf54f 100644 --- a/src/Db/Decorator/ModelAbstract.php +++ b/src/Db/Decorator/ModelAbstract.php @@ -51,7 +51,7 @@ public function beforeUpdate() * @param $id * @return \Phalcon\Mvc\Model\ResultsetInterface */ - public function findById($id) + public static function findById($id) { return parent::findFirst(array( "conditions" => "id = ?1", diff --git a/src/Mvc/Bootstrap.php b/src/Mvc/Bootstrap.php index bf72552..e5b17b4 100644 --- a/src/Mvc/Bootstrap.php +++ b/src/Mvc/Bootstrap.php @@ -14,11 +14,30 @@ use Phalcon\Config; use Phalcon\DI\FactoryDefault; +use Phalcon\DI; use Phalcon\DiInterface; +use Phalcon\Mvc\Dispatcher; +use Vegas\Bootstrap\EnvironmentInitializerTrait; +use Vegas\Bootstrap\LoaderInitializerTrait; +use Vegas\Bootstrap\ModulesInitializerTrait; +use Vegas\Bootstrap\RoutesInitializerTrait; +use Vegas\Bootstrap\ServicesInitializerTrait; use Vegas\BootstrapInterface; +use Vegas\Mvc\Dispatcher\Events\ExceptionListener; +use Vegas\Mvc\Router; class Bootstrap implements BootstrapInterface { + use ModulesInitializerTrait; + + use ServicesInitializerTrait; + + use RoutesInitializerTrait; + + use EnvironmentInitializerTrait; + + use LoaderInitializerTrait; + /** * Dependency injection * @@ -81,6 +100,30 @@ public function getApplication() { return $this->application; } + + /** + * Registers default dispatcher + */ + protected function initDispatcher() + { + $this->di->set('dispatcher', function() { + $dispatcher = new Dispatcher(); + + /** + * @var \Phalcon\Events\Manager $eventsManager + */ + $eventsManager = $this->di->getShared('eventsManager'); + $eventsManager->attach( + 'dispatch:beforeException', + (new ExceptionListener())->beforeException() + ); + + $dispatcher->setEventsManager($eventsManager); + + return $dispatcher; + }); + } + /** * Executes all bootstrap initialization methods * This method can be overloaded to load own initialization method. @@ -88,17 +131,32 @@ public function getApplication() */ public function setup() { - // TODO: Implement setup() method. + $this->di->set('config', $this->config); + + $this->initEnvironment($this->config); + $this->initLoader($this->config); + $this->initModules($this->config); + $this->initRoutes($this->config); + $this->initServices($this->config); + $this->initDispatcher(); + + $this->application->setDI($this->di); + DI::setDefault($this->di); + + return $this; } /** - * Runs application + * Start handling MVC requests * - * @return mixed + * @param string $uri + * @return string */ - public function run() + public function run($uri = null) { - // TODO: Implement run() method. + return $this->application + ->handle($uri) + ->getContent(); } } \ No newline at end of file diff --git a/src/Mvc/Dispatcher/Events/ExceptionListener.php b/src/Mvc/Dispatcher/Events/ExceptionListener.php new file mode 100644 index 0000000..8332d5b --- /dev/null +++ b/src/Mvc/Dispatcher/Events/ExceptionListener.php @@ -0,0 +1,43 @@ + + * @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\Mvc\Dispatcher\Events; + +use Phalcon\Dispatcher; +use Phalcon\Events\Event; +use Vegas\Mvc\Dispatcher\ExceptionResolver; + +/** + * Class BeforeException + * @package Vegas\Mvc\Dispatcher\Events + */ +class ExceptionListener +{ + /** + * Event fired when exception is throwing + * + */ + public function beforeException() { + /** + * @param \Phalcon\Events\Event $event + * @param \Phalcon\Dispatcher $dispatcher + * @param \Exception $exception + * @return callable + */ + return function(Event $event, Dispatcher $dispatcher, \Exception $exception) { + $resolver = new ExceptionResolver(); + $resolver->setDI($dispatcher->getDI()); + $resolver->resolve($exception); + + return false; + }; + } +} \ No newline at end of file diff --git a/src/Mvc/Module/Exception/InvalidModulesListException.php b/src/Mvc/Module/Exception/InvalidModulesListException.php new file mode 100644 index 0000000..04f2dd6 --- /dev/null +++ b/src/Mvc/Module/Exception/InvalidModulesListException.php @@ -0,0 +1,25 @@ + + * @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\Mvc\Module\Exception; + +use Vegas\Mvc\Exception as MvcException; + +/** + * Class InvalidModulesListException + * @package Vegas\Mvc\Module\Exception + */ +class InvalidModulesListException extends MvcException +{ + protected $message = 'Invalid modules list'; +} + \ No newline at end of file diff --git a/src/Mvc/Module/Loader.php b/src/Mvc/Module/Loader.php index 56307bf..c5a3100 100644 --- a/src/Mvc/Module/Loader.php +++ b/src/Mvc/Module/Loader.php @@ -12,8 +12,8 @@ namespace Vegas\Mvc\Module; -use Phalcon\DiInterface; use Phalcon\Text; +use Vegas\DI\InjectionAwareTrait; use Vegas\Util\FileWriter; /** @@ -35,16 +35,17 @@ class Loader /** * Generates list of modules into source file * - * @param DiInterface $di + * @param string $inputDirectory + * @param string $outputDirectory + * @param bool $dumpVendorModules * @return array */ - public static function dump(DiInterface $di) + public function dump($inputDirectory, $outputDirectory, $dumpVendorModules = true) { $modulesList = []; //extracts list of modules from module directory - $config = $di->get('config'); - $directoryIterator = new \DirectoryIterator($config->application->moduleDir); + $directoryIterator = new \DirectoryIterator($inputDirectory); foreach ($directoryIterator as $moduleDir) { if ($moduleDir->isDot()) { continue; @@ -64,12 +65,14 @@ public static function dump(DiInterface $di) ]; } - self::dumpModulesFromVendor($modulesList); + if ($dumpVendorModules) { + $this->dumpModulesFromVendor($modulesList); + } //saves generated array to php source file - FileWriter::write( - $config->application->configDir . self::MODULE_STATIC_FILE, - self::createFileContent($modulesList), + FileWriter::writeObject( + $outputDirectory . self::MODULE_STATIC_FILE, + $modulesList, true ); @@ -82,7 +85,7 @@ public static function dump(DiInterface $di) * @param $modulesList * @return mixed */ - private static function dumpModulesFromVendor(array &$modulesList) + private function dumpModulesFromVendor(array &$modulesList) { if (!file_exists(APP_ROOT.'/composer.json')) { return $modulesList; @@ -127,16 +130,4 @@ private static function dumpModulesFromVendor(array &$modulesList) return $modulesList; } - - /** - * Creates file's content with list of modules - * - * @param $modulesList - * @return string - * @internal - */ - private static function createFileContent($modulesList) - { - return 'newInstance(); - $reflectionClass->getMethod('setDI')->invoke($authenticationPlugin, $di); - $eventsManager->attach($plugin['attach'], $authenticationPlugin); + $dispatcherPlugin = $reflectionClass->newInstance(); + if ($reflectionClass->hasMethod('setDI')) { + $reflectionClass->getMethod('setDI')->invoke($dispatcherPlugin, $di); + } + $eventsManager->attach($plugin['attach'], $dispatcherPlugin); } $dispatcher->setEventsManager($eventsManager); } diff --git a/src/Test/Bootstrap.php b/src/Test/Bootstrap.php index 40ab941..646b9f4 100644 --- a/src/Test/Bootstrap.php +++ b/src/Test/Bootstrap.php @@ -12,8 +12,182 @@ namespace Vegas\Test; -class Bootstrap +use Phalcon\Config; +use Phalcon\DI\FactoryDefault; +use Phalcon\DI; +use Phalcon\DiInterface; +use Phalcon\Mvc\Dispatcher; +use Vegas\Bootstrap\EnvironmentInitializerTrait; +use Vegas\Bootstrap\LoaderInitializerTrait; +use Vegas\Bootstrap\ModulesInitializerTrait; +use Vegas\Bootstrap\RoutesInitializerTrait; +use Vegas\Bootstrap\ServicesInitializerTrait; +use Vegas\BootstrapInterface; +use Vegas\Constants; +use Vegas\Mvc\Application; +use Vegas\Mvc\Dispatcher\Events\ExceptionListener; +use Vegas\Mvc\Router; +use Vegas\Test\Http\Request; + +/** + * @codeCoverageIgnore + */ +class Bootstrap implements BootstrapInterface { + use ModulesInitializerTrait; + + use LoaderInitializerTrait; + + use RoutesInitializerTrait; + + use ServicesInitializerTrait; + + /** + * Dependency injection + * + * @var DiInterface + */ + protected $di; + + /** + * MVC Application + * + * @var Application + */ + protected $application; + + /** + * Application config + * + * @var Config + */ + protected $config; + + /** + * Constructor + * Initializes MVC Application + * Initializes DI for Application + * + * @param Config $config + */ + public function __construct(Config $config) + { + $this->config = $config; + $this->di = new FactoryDefault(); + $this->application = new Application(); + } + + /** + * Sets Dependency Injector + * + * @param DiInterface $di + */ + public function setDi(DiInterface $di) + { + $this->di = $di; + } + + /** + * Returns Dependency Injector + * + * @return FactoryDefault|DiInterface + */ + public function getDI() + { + return $this->di; + } + + /** + * @return Application + */ + public function getApplication() + { + return $this->application; + } + + /** + * Initializes application environment + */ + protected function initEnvironment(Config $config) + { + $env = Constants::TEST_ENV; + + if (!defined('APPLICATION_ENV')) { + define('APPLICATION_ENV', $env); + } + + $this->getDI()->set('environment', function() use ($env) { + return $env; + }, true); + } + + /** + * Registers default dispatcher + */ + protected function initDispatcher() + { + $this->di->set('dispatcher', function() { + $dispatcher = new Dispatcher(); + + /** + * @var \Phalcon\Events\Manager $eventsManager + */ + $eventsManager = $this->di->getShared('eventsManager'); + $eventsManager->attach( + 'dispatch:beforeException', + (new ExceptionListener())->beforeException() + ); + + $dispatcher->setEventsManager($eventsManager); + + return $dispatcher; + }); + } + + /** + * + */ + protected function initRequest() + { + $this->getDI()->set('request', function() { + return new Request(); + }); + } + + /** + * Executes all bootstrap initialization methods + * This method can be overloaded to load own initialization method. + * @return mixed + */ + public function setup() + { + $this->di->set('config', $this->config); + + $this->initEnvironment($this->config); + $this->initLoader($this->config); + $this->initServices($this->config); + $this->initModules($this->config); + $this->initRoutes($this->config); + $this->initRequest(); + $this->initDispatcher(); + + $this->application->setDI($this->di); + DI::setDefault($this->di); + + return $this; + } + /** + * Runs application + * + * @param string $uri + * @return mixed|void + */ + public function run($uri = null) + { + return $this->application + ->handle($uri) + ->getContent(); + } } \ No newline at end of file diff --git a/src/Test/Http/Request.php b/src/Test/Http/Request.php index 988fa32..752eb1e 100644 --- a/src/Test/Http/Request.php +++ b/src/Test/Http/Request.php @@ -13,8 +13,7 @@ namespace Vegas\Test\Http; /** - * Class Request - * @package Vegas\Test\Http + * @codeCoverageIgnore */ class Request extends \Phalcon\Http\Request { diff --git a/src/Test/TestCase.php b/src/Test/TestCase.php index d2c1364..4bf3395 100644 --- a/src/Test/TestCase.php +++ b/src/Test/TestCase.php @@ -12,12 +12,24 @@ namespace Vegas\Test; +use Phalcon\DI; +use Vegas\Bootstrap\ModulesInitializerTrait; +use Vegas\Test\Bootstrap; + +/** + * @codeCoverageIgnore + */ class TestCase extends \PHPUnit_Framework_TestCase { + /** * @var \Bootstrap */ protected $bootstrap; + + /** + * @var \Phalcon\DI\FactoryDefault|\Phalcon\DiInterface + */ protected $di; /** @@ -30,28 +42,21 @@ class TestCase extends \PHPUnit_Framework_TestCase public function __construct($name = null, array $data = [], $dataName = '') { parent::__construct($name, $data, $dataName); + } - $loader = new \Phalcon\Loader(); - - $loader->registerNamespaces([ - 'Tests' => APP_ROOT . '/tests', - ], true); - - $loader->registerDirs([APP_ROOT. '/app'], true); - $loader->register(); - - $config = require APP_ROOT . '/tests/config.php'; - - $bootstrap = new \Bootstrap(new \Phalcon\Config($config)); + /** + * + */ + public function setUp() + { + $config = DI::getDefault()->get('config'); + $bootstrap = new Bootstrap($config); + $bootstrap->setup(); $this->di = $bootstrap->getDI(); - $this->di->set('request', function() { - return new \Vegas\Test\Http\Request(); - }, true); - - $bootstrap->setup(); $this->bootstrap = $bootstrap; + DI::setDefault($bootstrap->getDI()); } /** @@ -60,7 +65,7 @@ public function __construct($name = null, array $data = [], $dataName = '') * @param $uri * @return \Phalcon\Http\ResponseInterface */ - public function handle($uri) + public function handleUri($uri) { $response = $this->bootstrap->getApplication()->handle($uri); return $response; @@ -81,5 +86,13 @@ public function getDI() { return $this->di; } + + /** + * @return \Bootstrap|Bootstrap + */ + public function getBootstrap() + { + return $this->bootstrap; + } } \ No newline at end of file diff --git a/src/Util/FileWriter.php b/src/Util/FileWriter.php index cb30ea7..44ecba4 100644 --- a/src/Util/FileWriter.php +++ b/src/Util/FileWriter.php @@ -24,21 +24,37 @@ class FileWriter * Writes string content to a file * * @param $filePath - * @param $newContent + * @param $content * @param bool $compareContents Determines if new content should be compared with the * current file content. When contents are the same, then * new content will not be written to the file. * @return int Number of bytes that were written to the file */ - public static function write($filePath, $newContent, $compareContents = false) + public static function write($filePath, $content, $compareContents = false) { if ($compareContents) { - if (self::compareContents($filePath, $newContent)) { + if (self::compareContents($filePath, $content)) { return 0; } } - return file_put_contents($filePath, $newContent); + return file_put_contents($filePath, $content); + } + + /** + * Writes string representation of PHP object into plain file + * + * @param $filePath + * @param $object + * @param bool $compareContents Determines if new content should be compared with the + * current file content. When contents are the same, then + * new content will not be written to the file. + * @return int Number of bytes that were written to the file + */ + public static function writeObject($filePath, $object, $compareContents = false) + { + $content = 'di = DI::getDefault(); } - public function testInvalidTask() + public function testShouldChangeDI() + { + $cli = new Bootstrap($this->di->get('config')); + $this->assertInstanceOf('\Phalcon\DI\FactoryDefault\CLI', $cli->getDI()); + $cli->setDI($this->di); + $this->assertInstanceOf(get_class($this->di), $cli->getDI()); + } + + public function testShouldThrowExceptionAboutNotFoundTask() { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( @@ -80,7 +88,7 @@ public function testInvalidTask() } } - public function testValidAppTask() + public function testShouldThrowExceptionAboutMissingArguments() { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( @@ -95,7 +103,10 @@ public function testValidAppTask() } catch (\Exception $ex) { $this->assertInstanceOf('Vegas\Cli\Task\Exception\MissingRequiredArgumentException', $ex); } + } + public function testShouldThrowExeptionAboutInvalidArgument() + { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( 0 => 'cli/cli.php', @@ -110,7 +121,10 @@ public function testValidAppTask() } catch (\Exception $ex) { $this->assertTrue((bool)strstr($ex->getMessage(),'Invalid argument')); } + } + public function testShouldThrowExceptionAboutInvalidOption() + { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( 0 => 'cli/cli.php', @@ -127,7 +141,7 @@ public function testValidAppTask() } } - public function testValidHelp() + public function testShouldReturnTaskHelp() { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( @@ -147,7 +161,7 @@ public function testValidHelp() $this->assertEquals('G1swOzMybVRlc3QgYWN0aW9uG1swbQoKVXNhZ2U6ChtbMTszMG0gICBhcHA6Y3VzdG9tIHRlc3QgW29wdGlvbnNdG1swbQoKT3B0aW9uczobWzBtChtbMTszMm0gICAtLWZvbyAgICAgLWYgICAgICBGb28gb3B0aW9uLiBVc2FnZSBhcHA6Y3VzdG9tIHRlc3QgLWYgbnVtYmVyT2ZTdGgbWzBtCg==', $returnedValue); } - public function testValidValues() + public function testShouldExecuteApplicationTask() { $correctBase = 'G1sxOzM0bRtbMG0KG1sxOzM0bTEyMxtbMG0K'; @@ -222,7 +236,7 @@ public function testValidValues() $this->assertEquals($correctBase, $returnedValue); } - public function testValidModuleTask() + public function testShouldExecuteModuleTask() { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( diff --git a/tests/DI/ScaffoldingTest.php b/tests/DI/ScaffoldingTest.php index c98613b..696de04 100644 --- a/tests/DI/ScaffoldingTest.php +++ b/tests/DI/ScaffoldingTest.php @@ -11,6 +11,7 @@ */ namespace Vegas\Tests\DI; +use Vegas\Db\Exception\NoRequiredServiceException; use Vegas\DI\Scaffolding; class ScaffoldingTest extends \PHPUnit_Framework_TestCase @@ -32,6 +33,25 @@ protected function setUp() $this->record = $record; } + + public function testRequiredServicesVerification() + { + $di = \Phalcon\DI::getDefault(); + + $emptyDI = new \Phalcon\DI\FactoryDefault(); + \Phalcon\DI::setDefault($emptyDI); + + $exception = null; + try { + new Scaffolding(new Scaffolding\Adapter\Mongo()); + } catch (NoRequiredServiceException $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Exception\NoRequiredServiceException', $exception); + + //reverts DI + \Phalcon\DI::setDefault($di); + } public function testGetRecord() { diff --git a/tests/DI/Service/ComponentTest.php b/tests/DI/Service/ComponentTest.php index 8361fd7..fd66261 100644 --- a/tests/DI/Service/ComponentTest.php +++ b/tests/DI/Service/ComponentTest.php @@ -15,9 +15,9 @@ use Test\Components\Fake; use Vegas\DI\Service\Component\Renderer; use Vegas\Mvc\Application; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; use Vegas\Mvc\View; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class ComponentTest extends TestCase { @@ -39,8 +39,10 @@ public function setUp() return $view; }); - - $modules = ModuleLoader::dump($di); + $modules = (new ModuleLoader($di))->dump( + $di->get('config')->application->moduleDir, + $di->get('config')->application->configDir + ); $app = new Application(); $app->registerModules($modules); diff --git a/tests/DI/ServiceManagerTest.php b/tests/DI/ServiceManagerTest.php index bba5ea1..c3655c4 100644 --- a/tests/DI/ServiceManagerTest.php +++ b/tests/DI/ServiceManagerTest.php @@ -26,6 +26,18 @@ public function testIsInjectionAware() $this->assertEquals($di->get('serviceManager'), $sm); } + + public function testHasService() + { + $di = DI::getDefault(); + $sm = new ServiceManager(); + $sm->setDI($di); + + $this->assertTrue($sm->has('vegas\Tests\Stub:fakeService')); + $this->assertTrue($sm->hasService('vegas\Tests\Stub:fakeService')); + $this->assertFalse($sm->has('notExisting:mock')); + $this->assertFalse($sm->hasService('notExisting:mock')); + } public function testGetNotExistingService() { diff --git a/tests/DI/ServiceProviderLoaderTest.php b/tests/DI/ServiceProviderLoaderTest.php index bfe7e6f..116c887 100644 --- a/tests/DI/ServiceProviderLoaderTest.php +++ b/tests/DI/ServiceProviderLoaderTest.php @@ -20,7 +20,11 @@ class ServiceProviderLoaderTest extends \PHPUnit_Framework_TestCase { public function testDump() { - ServiceProviderLoader::dump(DI::getDefault()); + $serviceProviderLoader = new ServiceProviderLoader(DI::getDefault()); + $serviceProviderLoader->dump( + TESTS_ROOT_DIR . '/fixtures/app/services/', + TESTS_ROOT_DIR . '/fixtures/app/config/' + ); $this->assertTrue(file_exists(TESTS_ROOT_DIR . '/fixtures/app/config/services.php')); } @@ -30,7 +34,11 @@ public function testAutoload() $di->set('environment', function() { return Constants::DEV_ENV; }, true); - ServiceProviderLoader::autoload($di); + $serviceProviderLoader = new ServiceProviderLoader(DI::getDefault()); + $serviceProviderLoader->autoload( + TESTS_ROOT_DIR . '/fixtures/app/services/', + TESTS_ROOT_DIR . '/fixtures/app/config/' + ); $this->assertTrue($di->has('url')); $this->assertTrue($di->has('assets')); diff --git a/tests/Db/Adapter/Decorator/ModelAbstractTest.php b/tests/Db/Adapter/Decorator/ModelAbstractTest.php index ceb839b..7429452 100644 --- a/tests/Db/Adapter/Decorator/ModelAbstractTest.php +++ b/tests/Db/Adapter/Decorator/ModelAbstractTest.php @@ -74,6 +74,16 @@ public function testShouldCreateRecord() $this->assertTrue($fake->save()); } + public function testShouldFindRecordByItsId() + { + $fake = FakeModel::findFirst(); + + $this->assertSame( + $fake->toArray(), + FakeModel::findById($fake->getId())->toArray() + ); + } + public function testShouldUpdateRecord() { $fake = FakeModel::findFirst(); diff --git a/tests/Db/MappingTest.php b/tests/Db/MappingTest.php index ca78c63..d24275f 100644 --- a/tests/Db/MappingTest.php +++ b/tests/Db/MappingTest.php @@ -126,39 +126,43 @@ class MappingTest extends \PHPUnit_Framework_TestCase { public static function setUpBeforeClass() { - $data = array( - 'title' => 'Title test', - 'content' => 'Content test', - 'category_id' => rand(1000, 9999) + $di = \Phalcon\DI::getDefault(); + $di->get('db')->execute('DROP TABLE IF EXISTS fake_table '); + $di->get('db')->execute( + 'CREATE TABLE fake_table( + id int not null primary key auto_increment, + somedata varchar(250) null, + somecamel varchar(250) null + )' ); - - $fake = new FakeModel(); - $fake->writeAttributes($data); - $fake->save(); - - $fake = new Fake(); - $fake->writeAttributes($data); - $fake->save(); } public static function tearDownAfterClass() { + $di = \Phalcon\DI::getDefault(); + foreach (Fake::find() as $fake) { $fake->delete(); } - foreach (FakeModel::find() as $fake) { - $fake->delete(); - } + + $di->get('db')->execute('DROP TABLE IF EXISTS fake_table '); } public function testShouldAddMapperToMappingManager() { //define mappings $mappingManager = new MappingManager(); + + $this->assertInternalType('array', $mappingManager->getMappers()); + $this->assertEmpty($mappingManager->getMappers()); + $mappingManager->add(new Json()); $mappingManager->add(new Camelize()); $mappingManager->add(new UpperCase()); + $this->assertInternalType('array', $mappingManager->getMappers()); + $this->assertNotEmpty($mappingManager->getMappers()); + $this->assertNotEmpty(MappingManager::find('json')); $this->assertInstanceOf('\Vegas\Db\MappingInterface', MappingManager::find('json')); @@ -262,16 +266,6 @@ public function testShouldResolveModelMappings() $mappingManager->add(new Json()); $mappingManager->add(new Camelize()); - $di = DI::getDefault(); - $di->get('db')->execute('DROP TABLE IF EXISTS fake_table '); - $di->get('db')->execute( - 'CREATE TABLE fake_table( - id int not null primary key auto_increment, - somedata varchar(250) null, - somecamel varchar(250) null - )' - ); - $someData = json_encode(array(1,2,3,4,5,6)); $fake = new FakeModel(); $fake->somedata = $someData; diff --git a/tests/Mvc/ApplicationTest.php b/tests/Mvc/ApplicationTest.php index de4d2e1..30c4608 100644 --- a/tests/Mvc/ApplicationTest.php +++ b/tests/Mvc/ApplicationTest.php @@ -14,13 +14,18 @@ use Phalcon\DI; use Vegas\Mvc\Application; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; class ApplicationTest extends \PHPUnit_Framework_TestCase { public function testModuleRegister() { - $modules = ModuleLoader::dump(DI::getDefault()); + $moduleLoader = new ModuleLoader(DI::getDefault()); + $modules = $moduleLoader->dump( + TESTS_ROOT_DIR . '/fixtures/app/modules/', + TESTS_ROOT_DIR . '/fixtures/app/config/' + ); + $app = new Application(); $app->registerModules($modules); diff --git a/tests/Mvc/Controller/CrudTest.php b/tests/Mvc/Controller/CrudTest.php index 2d0b42f..8fd53ff 100644 --- a/tests/Mvc/Controller/CrudTest.php +++ b/tests/Mvc/Controller/CrudTest.php @@ -13,9 +13,9 @@ use Phalcon\DI; use Test\Forms\Fake; -use Test\Models\Fake As FakeModel; +use Test\Models\Fake as FakeModel; use Vegas\Mvc\Controller\Crud; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class CrudTest extends TestCase { @@ -26,24 +26,38 @@ public function setUp() parent::setUp(); $config = DI::getDefault()->get('config'); require_once $config->application->moduleDir . '/Test/forms/Fake.php'; + $this->prepareFakeObject(); + } + + private function prepareFakeObject() + { + $this->model = FakeModel::findFirst(array(array( + 'fake_field' => base64_encode(date('Y-m-d')) + ))); + + if (!$this->model) { + $this->model = new FakeModel(); + $this->model->fake_field = base64_encode(date('Y-m-d')); + $this->model->save(); + } } public function testNotConfiguredCrud() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->request()->setRequestMethod('GET'); - $content = $this->bootstrap->run('/test/brokencrud/new'); + $content = $response = $this->handleUri('/test/brokencrud/new')->getContent(); $this->assertContains('500', $content); $this->assertContains('CRUD is not configured.', $content); } public function testNew() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->request()->setRequestMethod('GET'); $form = new Fake(); - $content = $this->bootstrap->run('/test/crud/new'); + $content = $this->handleUri('/test/crud/new')->getContent(); $this->assertContains($form->get('fake_field')->render(['class' => ' form-control']), $content); $this->assertContains('', $content); @@ -51,7 +65,7 @@ public function testNew() public function testNotPostCreate() { - $content = $this->bootstrap->run('/test/crud/create'); + $content = $this->handleUri('/test/crud/create')->getContent(); $this->assertContains('500', $content); $this->assertContains('This is not a POST request!', $content); @@ -59,21 +73,28 @@ public function testNotPostCreate() public function testNotPostCreateResponse() { - $this->assertEquals('500 This is not a POST request!', $this->di->get('response')->getHeaders()->get('Status')); + $response = $this->handleUri('/test/crud/create'); + $this->assertEquals('500 This is not a POST request!', $response->getHeaders()->get('Status')); } public function testPostCreate() { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['fake_field'] = base64_encode(date('Y-m-d')); + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', base64_encode(date('Y-m-d'))); - $content = $this->bootstrap->run('/test/crud/create'); + $content = $this->handleUri('/test/crud/create')->getContent(); $this->assertNotEmpty($content); } public function testPostCreateResponse() { - $contentArray = json_decode($this->di->get('response')->getContent(), true); + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', base64_encode(date('Y-m-d'))); + $response = $this->handleUri('/test/crud/create'); + + $contentArray = json_decode($response->getContent(), true); $model = FakeModel::findById($contentArray['$id']); @@ -86,21 +107,20 @@ public function testPostCreateResponse() public function testPostCreateException() { - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['fake_field'] = ''; + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', ''); - $content = $this->bootstrap->run('/test/crud/create'); + $content = $this->handleUri('/test/crud/create')->getContent(); $this->assertContains('Field fake_field is required', $content); } public function testEdit() { - $this->prepareFakeObject(); - - $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->request()->setRequestMethod('GET'); $form = new Fake($this->model); - $content = $this->bootstrap->run('/test/crud/edit/'.$this->model->getId()); + $content = $this->handleUri('/test/crud/edit/'.$this->model->getId())->getContent(); $this->assertContains($form->get('fake_field')->render(['class' => ' form-control']), $content); $this->assertContains('', $content); @@ -108,9 +128,7 @@ public function testEdit() public function testNotPostUpdate() { - $this->prepareFakeObject(); - - $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); + $content = $this->handleUri('/test/crud/update/'.$this->model->getId())->getContent(); $this->assertContains('500', $content); $this->assertContains('This is not a POST request!', $content); @@ -118,24 +136,29 @@ public function testNotPostUpdate() public function testNotPostUpdateResponse() { - $this->assertContains('500', $this->di->get('response')->getHeaders()->get('Status')); - $this->assertContains('This is not a POST request!', $this->di->get('response')->getHeaders()->get('Status')); + $response = $this->handleUri('/test/crud/update/'.$this->model->getId()); + $this->assertContains('500', $response->getHeaders()->get('Status')); + $this->assertContains('This is not a POST request!', $response->getHeaders()->get('Status')); } public function testPostUpdate() { - $this->prepareFakeObject(); - - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['fake_field'] = base64_encode('foobar'); + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', base64_encode('foobar')); - $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); + $content = $this->handleUri('/test/crud/update/'.$this->model->getId())->getContent(); $this->assertEquals(json_encode($this->model->getId()), $content); } public function testPostUpdateResponse() { - $contentArray = json_decode($this->di->get('response')->getContent(), true); + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', base64_encode('foobar')); + + $response = $this->handleUri('/test/crud/update/'.$this->model->getId()); + $contentArray = json_decode($response->getContent(), true); $model = FakeModel::findById($contentArray['$id']); @@ -147,22 +170,19 @@ public function testPostUpdateResponse() public function testPostUpdateException() { - $this->prepareFakeObject(); - - $_SERVER['REQUEST_METHOD'] = 'POST'; - $_POST['fake_field'] = ''; + $this->request() + ->setRequestMethod('POST') + ->setPost('fake_field', ''); - $content = $this->bootstrap->run('/test/crud/update/'.$this->model->getId()); + $content = $this->handleUri('/test/crud/update/'.$this->model->getId())->getContent(); $this->assertContains('Field fake_field is required', $content); } public function testIndex() { - $this->prepareFakeObject(); - - $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->request()->setRequestMethod('GET'); - $content = $this->bootstrap->run('/test/crud/index/'); + $content = $this->handleUri('/test/crud/index/')->getContent(); $this->assertContains('Fake field index', $content); $this->assertContains($this->model->fake_field, $content); @@ -170,11 +190,9 @@ public function testIndex() public function testShow() { - $this->prepareFakeObject(); + $this->request()->setRequestMethod('GET'); - $_SERVER['REQUEST_METHOD'] = 'GET'; - - $content = $this->bootstrap->run('/test/crud/show/'.$this->model->getId()); + $content = $this->handleUri('/test/crud/show/'.$this->model->getId())->getContent(); $this->assertContains('Fake field', $content); $this->assertContains($this->model->fake_field, $content); @@ -184,10 +202,9 @@ public function testDelete() { $this->model = FakeModel::findFirst(); - $_SERVER['REQUEST_METHOD'] = 'GET'; - - $this->bootstrap->run('/test/crud/delete/'.$this->model->getId()); + $this->request()->setRequestMethod('GET'); + $this->handleUri('/test/crud/delete/'.$this->model->getId()); $this->model = FakeModel::findById($this->model->getId()); $this->assertFalse($this->model); @@ -195,24 +212,11 @@ public function testDelete() public function testDeleteException() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->request()->setRequestMethod('GET'); - $content = $this->bootstrap->run('/test/crud/delete/RanDoMn0t1D4sUR3'); + $content = $this->handleUri('/test/crud/delete/RanDoMn0t1D4sUR3')->getContent(); $this->assertContains('500', $content); $this->assertContains('Invalid object ID', $content); } - - private function prepareFakeObject() - { - $this->model = FakeModel::findFirst(array(array( - 'fake_field' => base64_encode(date('Y-m-d')) - ))); - - if (!$this->model) { - $this->model = new FakeModel(); - $this->model->fake_field = base64_encode(date('Y-m-d')); - $this->model->save(); - } - } } \ No newline at end of file diff --git a/tests/Mvc/ControllerAbstractTest.php b/tests/Mvc/ControllerAbstractTest.php index 8e3a8fb..151dc0a 100644 --- a/tests/Mvc/ControllerAbstractTest.php +++ b/tests/Mvc/ControllerAbstractTest.php @@ -11,51 +11,35 @@ */ namespace Vegas\Tests\Mvc; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class ControllerAbstractTest extends TestCase { - public function testNoActionError() - { - $this->bootstrap->run('/test/front/no-action-like-this'); - } - public function testNoActionErrorResponse() { + $response = $this->handleUri('/test/front/no-action-like-this'); $this->assertEquals( '404 Action \'no-action-like-this\' was not found on handler \'Frontend\Fake\'', - $this->di->get('response')->getHeaders()->get('Status') + $response->getHeaders()->get('Status') ); } - public function test403Error() - { - $this->bootstrap->run('/test/front/error/403'); - } - public function test403ErrorResponse() { - $this->assertEquals('403 Message', $this->di->get('response')->getHeaders()->get('Status')); - } - - public function test404Error() - { - $this->bootstrap->run('/test/front/error/404'); + $response = $this->handleUri('/test/front/error/403'); + $this->assertEquals('403 Message', $response->getHeaders()->get('Status')); } public function test404ErrorResponse() { - $this->assertEquals('404 Message', $this->di->get('response')->getHeaders()->get('Status')); - } - - public function test500Error() - { - $this->bootstrap->run('/test/front/error/500'); + $response = $this->handleUri('/test/front/error/404'); + $this->assertEquals('404 Message', $response->getHeaders()->get('Status')); } public function test500ErrorResponse() { - $this->assertEquals('500 Message', $this->di->get('response')->getHeaders()->get('Status')); + $response = $this->handleUri('/test/front/error/500'); + $this->assertEquals('500 Message', $response->getHeaders()->get('Status')); } public function testJson() @@ -67,4 +51,9 @@ public function testEmptyJson() { $this->assertEquals('[]', $this->bootstrap->run('/test/front/emptyjson')); } -} + + public function testTranslatorAlias() + { + $this->assertEquals('test', $this->bootstrap->run('/testfoo/front/translate/test')); + } +} \ No newline at end of file diff --git a/tests/Mvc/Module/LoaderTest.php b/tests/Mvc/Module/LoaderTest.php index 12b5e12..1485881 100644 --- a/tests/Mvc/Module/LoaderTest.php +++ b/tests/Mvc/Module/LoaderTest.php @@ -13,13 +13,17 @@ namespace Vegas\Tests\Mvc\Module; use Phalcon\DI; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; class LoaderTest extends \PHPUnit_Framework_TestCase { public function testShouldLoadAllModulesFromApplication() { - $modules = ModuleLoader::dump(DI::getDefault()); + $moduleLoader = new ModuleLoader(DI::getDefault()); + $modules = $moduleLoader->dump( + TESTS_ROOT_DIR . '/fixtures/app/modules/', + TESTS_ROOT_DIR . '/fixtures/app/config/' + ); $this->assertInternalType('array', $modules); diff --git a/tests/Mvc/ModuleAbstractTest.php b/tests/Mvc/ModuleAbstractTest.php index 1e3d5dd..7ee6de8 100644 --- a/tests/Mvc/ModuleAbstractTest.php +++ b/tests/Mvc/ModuleAbstractTest.php @@ -14,7 +14,7 @@ use Phalcon\DI; use Vegas\Mvc\Controller\Crud; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class ModuleAbstractTest extends TestCase { diff --git a/tests/Mvc/Router/Route/NotFoundRouteTest.php b/tests/Mvc/Router/Route/NotFoundRouteTest.php index 5b28342..5b066b1 100644 --- a/tests/Mvc/Router/Route/NotFoundRouteTest.php +++ b/tests/Mvc/Router/Route/NotFoundRouteTest.php @@ -13,7 +13,6 @@ namespace Vegas\Tests\Mvc\Router\Route; use Phalcon\DI; -use Vegas\Mvc\Router\Route\BaseRoute; use Vegas\Mvc\Router\Route; class NotFoundRouteTest extends \PHPUnit_Framework_TestCase diff --git a/tests/Mvc/Router/Route/RestRouteTest.php b/tests/Mvc/Router/Route/RestRouteTest.php index d351e4a..d6b2c6a 100644 --- a/tests/Mvc/Router/Route/RestRouteTest.php +++ b/tests/Mvc/Router/Route/RestRouteTest.php @@ -13,9 +13,7 @@ namespace Vegas\Tests\Mvc\Router\Route; use Phalcon\DI; -use Phalcon\Mvc\Router\RouteInterface; use Vegas\Http\Method; -use Vegas\Mvc\Router\Route\BaseRoute; use Vegas\Mvc\Router\Route; class RestRouteTest extends \PHPUnit_Framework_TestCase diff --git a/tests/Mvc/Router/Route/StaticRouteTest.php b/tests/Mvc/Router/Route/StaticRouteTest.php index 9ee209b..408aff7 100644 --- a/tests/Mvc/Router/Route/StaticRouteTest.php +++ b/tests/Mvc/Router/Route/StaticRouteTest.php @@ -13,7 +13,6 @@ namespace Vegas\Tests\Mvc\Router\Route; use Phalcon\DI; -use Vegas\Mvc\Router\Route\BaseRoute; use Vegas\Mvc\Router\Route; class StaticRouteTest extends \PHPUnit_Framework_TestCase diff --git a/tests/Mvc/RouterTest.php b/tests/Mvc/RouterTest.php index 3e7011e..5c00526 100644 --- a/tests/Mvc/RouterTest.php +++ b/tests/Mvc/RouterTest.php @@ -14,7 +14,7 @@ use Phalcon\DI; use Vegas\Http\Method; -use Vegas\Mvc\Module\Loader As ModuleLoader; +use Vegas\Mvc\Module\Loader as ModuleLoader; use Vegas\Mvc\Router; class RouterTest extends \PHPUnit_Framework_TestCase @@ -215,7 +215,11 @@ public function testShouldAddModuleRoutes() $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); - $modules = ModuleLoader::dump(DI::getDefault()); + $moduleLoader = new ModuleLoader(DI::getDefault()); + $modules = $moduleLoader->dump( + TESTS_ROOT_DIR . '/fixtures/app/modules/', + TESTS_ROOT_DIR . '/fixtures/app/config/' + ); foreach ($modules as $module) { $router->addModuleRoutes($module); } diff --git a/tests/Mvc/View/Engine/VoltTest.php b/tests/Mvc/View/Engine/VoltTest.php index 96e09ab..d878097 100644 --- a/tests/Mvc/View/Engine/VoltTest.php +++ b/tests/Mvc/View/Engine/VoltTest.php @@ -13,7 +13,7 @@ use Phalcon\DI; use Vegas\Mvc\View; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class VoltTest extends TestCase { diff --git a/tests/Mvc/ViewTest.php b/tests/Mvc/ViewTest.php index 6013f99..96f5053 100644 --- a/tests/Mvc/ViewTest.php +++ b/tests/Mvc/ViewTest.php @@ -13,7 +13,7 @@ namespace Vegas\Tests\Mvc; use Vegas\Mvc\View; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class ViewTest extends TestCase { @@ -50,19 +50,18 @@ public function testViewOptions() public function testPathsResolving() { - $configView = $this->di->get('config')->application->view->toArray(); + $configView = $this->getDI()->get('config')->application->view->toArray(); if (!file_exists($configView['cacheDir'])) { mkdir($configView['cacheDir'], 0777); } else { chmod($configView['cacheDir'], 0777); } - $content = function($params) { + $getContent = function($params) { $this->setUp(); - $route = $this->bootstrap->getDI()->get('router')->getRouteByName('test'); + $route = $this->getDI()->get('router')->getRouteByName('test'); $url = rtrim(str_replace(array(':action', ':params'), $params, $route->getPattern()), DIRECTORY_SEPARATOR); - $this->bootstrap->run($url); - return $this->bootstrap->getDI()->get('response')->getContent(); + return $this->handleUri($url)->getContent(); }; //compares output rendered by dispatcher @@ -72,26 +71,26 @@ public function testPathsResolving() //app/modules/Test/views/frontend/fake/test.volt => 3 //app/modules/Test/views/frontend/fake/partials/test.volt => 4 //output of dispatcher => 1234 - $response = $content(array('test', '')); + $response = $getContent(array('test', '')); $this->assertEquals('1234', $response); //tests rendering only layout with one partial //should return 12 regarding to upwards comments - $response = $content(array('testLayout', '')); + $response = $getContent(array('testLayout', '')); $this->assertEquals('12', $response); //test rendering only view with one partial //should return 34 regarding to upwards comments - $response = $content(array('testView', '')); + $response = $getContent(array('testView', '')); $this->assertEquals('34', $response); //test rendering only global partial //should return 2 regarding to upwards comments - $response = $content(array('testGlobal', '')); + $response = $getContent(array('testGlobal', '')); $this->assertEquals('2', $response); //extract volt engine - $view = $this->bootstrap->getDI()->get('view'); + $view = $this->getDI()->get('view'); ob_start(); $view->partial('test/sample'); @@ -123,24 +122,26 @@ public function testPathsResolving() $view->partial(APP_ROOT . '/test'); $this->assertEquals('OUTSIDER', ob_get_contents()); ob_end_clean(); + + //reverts view configuration + $this->getDI()->get('config')->application->view = $configView; } public function testPathsResolvingWithoutPartialsDirInConfig() { - $configView = $this->di->get('config')->application->view->toArray(); + $configView = $this->getDI()->get('config')->application->view->toArray(); if (!file_exists($configView['cacheDir'])) { mkdir($configView['cacheDir'], 0777); } else { chmod($configView['cacheDir'], 0777); } - $content = function($params) { + $getContent = function($params) { $this->setUp(); - $this->bootstrap->getDI()->get('config')->application->view->partialsDir = false; - $this->bootstrap->getDI()->get('config')->application->view->layout = 'main2'; - $route = $this->bootstrap->getDI()->get('router')->getRouteByName('testfoo'); + $this->getDI()->get('config')->application->view->partialsDir = false; + $this->getDI()->get('config')->application->view->layout = 'main2'; + $route = $this->getDI()->get('router')->getRouteByName('testfoo'); $url = rtrim(str_replace(array(':action', ':params'), $params, $route->getPattern()), DIRECTORY_SEPARATOR); - $this->bootstrap->run($url); - return $this->bootstrap->getDI()->get('response')->getContent(); + return $this->handleUri($url)->getContent(); }; //compares output rendered by dispatcher @@ -150,26 +151,26 @@ public function testPathsResolvingWithoutPartialsDirInConfig() //app/modules/Test/views/frontend/foo/test.volt => 3 //app/modules/Test/views/frontend/foo/partials/test.volt => 4 //output of dispatcher => 1234 - $response = $content(array('test', '')); + $response = $getContent(array('test', '')); $this->assertEquals('1234', $response); //tests rendering only layout with one partial //should return 12 regarding to upwards comments - $response = $content(array('testLayout', '')); + $response = $getContent(array('testLayout', '')); $this->assertEquals('12', $response); //test rendering only view with one partial //should return 34 regarding to upwards comments - $response = $content(array('testView', '')); + $response = $getContent(array('testView', '')); $this->assertEquals('34', $response); //test rendering only local partial //should return 4 regarding to upwards comments - $response = $content(array('testLocal', '')); + $response = $getContent(array('testLocal', '')); $this->assertEquals('4', $response); //extract volt engine - $view = $this->bootstrap->getDI()->get('view'); + $view = $this->getDI()->get('view'); ob_start(); $view->partial('../../../layouts/partials/test/sample'); @@ -201,23 +202,25 @@ public function testPathsResolvingWithoutPartialsDirInConfig() $view->partial(APP_ROOT . '/test'); $this->assertEquals('OUTSIDER', ob_get_contents()); ob_end_clean(); + + //reverts view configuration + $this->getDI()->get('config')->application->view = $configView; } public function testShortNamespacePathsResolving() { - $configView = $this->di->get('config')->application->view->toArray(); + $configView = $this->getDI()->get('config')->application->view->toArray(); if (!file_exists($configView['cacheDir'])) { mkdir($configView['cacheDir'], 0777); } else { chmod($configView['cacheDir'], 0777); } - $content = function($params) { + $getContent = function($params) { $this->setUp(); - $route = $this->bootstrap->getDI()->get('router')->getRouteByName('testshort'); + $route = $this->getDI()->get('router')->getRouteByName('testshort'); $url = rtrim(str_replace(array(':action', ':params'), $params, $route->getPattern()), DIRECTORY_SEPARATOR); - $this->bootstrap->run($url); - return $this->bootstrap->getDI()->get('response')->getContent(); + return $this->handleUri($url)->getContent(); }; //compares output rendered by dispatcher @@ -227,21 +230,21 @@ public function testShortNamespacePathsResolving() //app/modules/Test/views/fake/test.volt => 3 //app/modules/Test/views/fake/partials/test.volt => 4 //output of dispatcher => 1234 - $response = $content(array('test', '')); + $response = $getContent(array('test', '')); $this->assertEquals('12shortPartialShort', $response); //tests rendering only layout with one partial //should return 12 regarding to upwards comments - $response = $content(array('testLayout', '')); + $response = $getContent(array('testLayout', '')); $this->assertEquals('12', $response); //test rendering only view with one partial - //should return 34 regarding to upwards comments - $response = $content(array('testView', '')); + //should return shortPartialShort regarding to upwards comments + $response = $getContent(array('testView', '')); $this->assertEquals('shortPartialShort', $response); //extract volt engine - $view = $this->bootstrap->getDI()->get('view'); + $view = $this->getDI()->get('view'); ob_start(); $view->partial('test/sample'); diff --git a/tests/Tag/PaginationTest.php b/tests/Tag/PaginationTest.php index ea4ea9a..f752e7a 100644 --- a/tests/Tag/PaginationTest.php +++ b/tests/Tag/PaginationTest.php @@ -12,9 +12,8 @@ namespace Vegas\Tests\Tag; use Vegas\Paginator\Adapter\Mongo; -use Vegas\Paginator\Page; use Vegas\Tag\Pagination; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; use Vegas\Tests\Stub\Models\FakeModel; class PaginationTest extends TestCase diff --git a/tests/Task/AssetsTest.php b/tests/Task/AssetsTest.php index dd3c760..d206a95 100644 --- a/tests/Task/AssetsTest.php +++ b/tests/Task/AssetsTest.php @@ -11,7 +11,6 @@ */ namespace Vegas\Tests\Task; -use Vegas\Task\AssetsTask; use Vegas\Tests\Cli\TestCase; class AssetsTest extends TestCase diff --git a/tests/Task/CacheTest.php b/tests/Task/CacheTest.php index 357d98c..0f2f7c6 100644 --- a/tests/Task/CacheTest.php +++ b/tests/Task/CacheTest.php @@ -11,7 +11,6 @@ */ namespace Vegas\Tests\Task; -use Vegas\Task\AssetsTask; use Vegas\Tests\Cli\TestCase; class CacheTest extends TestCase diff --git a/tests/Util/FileWriterTest.php b/tests/Util/FileWriterTest.php index 09f59bb..f0e8e92 100644 --- a/tests/Util/FileWriterTest.php +++ b/tests/Util/FileWriterTest.php @@ -14,7 +14,7 @@ use Vegas\Util\FileWriter; -class FileWriterTest extends \Vegas\Tests\App\TestCase +class FileWriterTest extends \PHPUnit_Framework_TestCase { public function testShouldWriteContentToFile() { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5ba82f5..35ce7ac 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,19 +4,28 @@ include __DIR__ . "/../vendor/autoload.php"; define('TESTS_ROOT_DIR', dirname(__FILE__)); +define('APP_ROOT', TESTS_ROOT_DIR . '/fixtures'); -$configArray = require_once dirname(__FILE__) . '/fixtures/app/config/config.php'; +$configArray = require_once TESTS_ROOT_DIR . '/config.php'; + +$_SERVER['HTTP_HOST'] = 'vegas.dev'; +$_SERVER['REQUEST_URI'] = '/'; $config = new \Phalcon\Config($configArray); $di = new Phalcon\DI\FactoryDefault(); -$di->set('config', $config); +$di->set('config', $config); +$di->set('collectionManager', function() use ($di) { + return new \Phalcon\Mvc\Collection\Manager(); +}, true); $di->set('mongo', function() use ($config) { $mongo = new \MongoClient(); return $mongo->selectDb($config->mongo->db); }, true); - +$di->set('modelManager', function() use ($di) { + return new \Phalcon\Mvc\Model\Manager(); +}, true); $di->set('db', function() use ($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->db->toArray()); }, true); diff --git a/tests/fixtures/app/config/config.php b/tests/config.sample.php similarity index 88% rename from tests/fixtures/app/config/config.php rename to tests/config.sample.php index 3275e4f..6fecbd0 100644 --- a/tests/fixtures/app/config/config.php +++ b/tests/config.sample.php @@ -1,12 +1,10 @@ array( 'environment' => \Vegas\Constants::TEST_ENV, 'serviceDir' => APP_ROOT . '/app/services/', - 'configDir' => dirname(__FILE__) . DIRECTORY_SEPARATOR, + 'configDir' => APP_ROOT . '/app/config/', 'libraryDir' => dirname(APP_ROOT) . DIRECTORY_SEPARATOR . '/lib/', 'pluginDir' => APP_ROOT . '/app/plugins/', 'moduleDir' => APP_ROOT . '/app/modules/', @@ -29,6 +27,7 @@ ), 'db' => array( + "adapter" => 'mysql', "host" => "localhost", "dbname" => "vegas_test", "port" => 3306, diff --git a/tests/fixtures/app/config/routes.php b/tests/fixtures/app/config/routes.php new file mode 100644 index 0000000..84c583b --- /dev/null +++ b/tests/fixtures/app/config/routes.php @@ -0,0 +1,11 @@ + [ + 'route' => '/', + 'paths' => [ + 'module' => 'Example', + 'controller' => 'Frontend\Home', + 'action' => 'index' + ] + ] +]; \ No newline at end of file diff --git a/tests/fixtures/app/modules/Example/controllers/frontend/HomeController.php b/tests/fixtures/app/modules/Example/controllers/frontend/HomeController.php new file mode 100644 index 0000000..9e078a3 --- /dev/null +++ b/tests/fixtures/app/modules/Example/controllers/frontend/HomeController.php @@ -0,0 +1,23 @@ + + * @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 Example\Controllers\Frontend; + +use Vegas\Mvc\ControllerAbstract; + +class HomeController extends ControllerAbstract +{ + public function indexAction() + { + + } +} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php b/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php index 35e038f..3abc4d8 100644 --- a/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php +++ b/tests/fixtures/app/modules/Test/controllers/frontend/FooController.php @@ -53,4 +53,12 @@ public function errorAction($code) $throwName = 'throw'.$code; $this->$throwName('Message'); } + + public function translateAction($str) + { + $this->view->disable(); + return $this->response->setContent( + $this->_($str) + ); + } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/services/Fake.php b/tests/fixtures/app/modules/Test/services/Fake.php index a602b9d..fd2576e 100644 --- a/tests/fixtures/app/modules/Test/services/Fake.php +++ b/tests/fixtures/app/modules/Test/services/Fake.php @@ -12,7 +12,6 @@ namespace Test\Services; -use Phalcon\DI\InjectionAwareInterface; use Vegas\DI\InjectionAwareTrait; class Fake extends \Vegas\DI\Service\ComponentAbstract diff --git a/tests/fixtures/app/plugins/Foo.php b/tests/fixtures/app/plugins/Foo.php index 40479a1..6263802 100644 --- a/tests/fixtures/app/plugins/Foo.php +++ b/tests/fixtures/app/plugins/Foo.php @@ -26,6 +26,6 @@ class Foo */ public function beforeDispatch(Event $event, Dispatcher $dispatcher) { - return false; + return true; } } \ No newline at end of file diff --git a/tests/fixtures/app/services/FlashSessionServiceProvider.php b/tests/fixtures/app/services/FlashSessionServiceProvider.php index ec8e898..f02c34e 100644 --- a/tests/fixtures/app/services/FlashSessionServiceProvider.php +++ b/tests/fixtures/app/services/FlashSessionServiceProvider.php @@ -11,8 +11,8 @@ */ use Phalcon\DiInterface; -use Vegas\DI\ServiceProviderInterface; use Vegas\DI\Scaffolding; +use Vegas\DI\ServiceProviderInterface; /** * Class ScaffoldingServiceProvider diff --git a/tests/fixtures/app/services/I18nServiceProvider.php b/tests/fixtures/app/services/I18nServiceProvider.php index 0ee8768..b0082ae 100644 --- a/tests/fixtures/app/services/I18nServiceProvider.php +++ b/tests/fixtures/app/services/I18nServiceProvider.php @@ -11,9 +11,9 @@ */ use Phalcon\DiInterface; -use Vegas\DI\ServiceProviderInterface; use Phalcon\Mvc\Url as UrlResolver; -use \Vegas\Session\Adapter\Files as SessionAdapter; +use Vegas\DI\ServiceProviderInterface; +use Vegas\Session\Adapter\Files as SessionAdapter; class I18nServiceProvider implements ServiceProviderInterface { diff --git a/tests/fixtures/app/services/ScaffoldingServiceProvider.php b/tests/fixtures/app/services/ScaffoldingServiceProvider.php index 8f0b620..78647f4 100644 --- a/tests/fixtures/app/services/ScaffoldingServiceProvider.php +++ b/tests/fixtures/app/services/ScaffoldingServiceProvider.php @@ -11,8 +11,8 @@ */ use Phalcon\DiInterface; -use Vegas\DI\ServiceProviderInterface; use Vegas\DI\Scaffolding; +use Vegas\DI\ServiceProviderInterface; /** * Class ScaffoldingServiceProvider From 87abf5ace060797679867d558b674a8a24049217 Mon Sep 17 00:00:00 2001 From: Arek Date: Fri, 12 Dec 2014 14:01:42 +0100 Subject: [PATCH 26/35] added clearfix for default crud form-groups --- src/Mvc/Controller/Crud/views/edit.volt | 4 ++-- src/Mvc/Controller/Crud/views/new.volt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Mvc/Controller/Crud/views/edit.volt b/src/Mvc/Controller/Crud/views/edit.volt index 3118976..b6abe96 100755 --- a/src/Mvc/Controller/Crud/views/edit.volt +++ b/src/Mvc/Controller/Crud/views/edit.volt @@ -19,7 +19,7 @@ {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} {% set hasErrors = form.hasMessagesFor(element.getName()) %} -
+
{% if element.getLabel() %} {% endif %} @@ -34,7 +34,7 @@
{% endfor %} -
+
Cancel
diff --git a/src/Mvc/Controller/Crud/views/new.volt b/src/Mvc/Controller/Crud/views/new.volt index 017dcd4..8648fc3 100755 --- a/src/Mvc/Controller/Crud/views/new.volt +++ b/src/Mvc/Controller/Crud/views/new.volt @@ -19,7 +19,7 @@ {% do element.setAttribute('class', element.getAttribute('class')~' form-control') %} {% set hasErrors = form.hasMessagesFor(element.getName()) %} -
+
{% if element.getLabel() %} {% endif %} @@ -34,7 +34,7 @@
{% endfor %} -
+
Cancel
From 1cee11cfcfef0fefb63472b9626bed415fb8a6ba Mon Sep 17 00:00:00 2001 From: slawek Date: Fri, 12 Dec 2014 17:21:20 +0100 Subject: [PATCH 27/35] I18n service fix; Updated config; --- tests/config.sample.php | 31 +++++++++++-------- .../app/services/I18nServiceProvider.php | 2 +- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/config.sample.php b/tests/config.sample.php index 6fecbd0..a1d9ae9 100644 --- a/tests/config.sample.php +++ b/tests/config.sample.php @@ -1,6 +1,6 @@ array( +return [ + 'application' => [ 'environment' => \Vegas\Constants::TEST_ENV, 'serviceDir' => APP_ROOT . '/app/services/', @@ -11,30 +11,35 @@ 'taskDir' => APP_ROOT . '/app/tasks/', 'baseUri' => '/', 'language' => 'nl_NL', - 'view' => array( + 'view' => [ 'cacheDir' => APP_ROOT . '/cache/', 'layout' => 'main', 'layoutsDir' => APP_ROOT . '/app/layouts/', 'partialsDir' => APP_ROOT . '/app/layouts/partials/', 'compileAlways' => true - ) - ), + ] + ], - 'plugins' => array(), + 'plugins' => [ + 'foo' => [ + 'class' => 'Foo', + 'attach' => 'beforeDispatch' + ] + ], - 'mongo' => array( + 'mongo' => [ 'db' => 'vegas_test', - ), + ], - 'db' => array( + 'db' => [ "adapter" => 'mysql', "host" => "localhost", "dbname" => "vegas_test", "port" => 3306, "username" => "root", 'password'=> 'root', - "options" => array( + "options" => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' - ) - ) -); \ No newline at end of file + ] + ] +]; \ No newline at end of file diff --git a/tests/fixtures/app/services/I18nServiceProvider.php b/tests/fixtures/app/services/I18nServiceProvider.php index b0082ae..9f093ae 100644 --- a/tests/fixtures/app/services/I18nServiceProvider.php +++ b/tests/fixtures/app/services/I18nServiceProvider.php @@ -26,7 +26,7 @@ public function register(DiInterface $di) { $config = $di->get('config'); $di->set('i18n', function() use ($config) { - return new \Phalcon\Translate\Adapter\Gettext(array( + return new \Vegas\Translate\Adapter\Gettext(array( 'locale' => $config->application->language, 'file' => 'messages', 'directory' => APP_ROOT.'/lang' From 06c170569b84713b4984f6d007a0c1ad2c07692d Mon Sep 17 00:00:00 2001 From: slawek Date: Fri, 12 Dec 2014 18:02:21 +0100 Subject: [PATCH 28/35] Added volt tests; --- src/DI/ServiceManager.php | 2 - src/Mvc/View/Engine/Volt.php | 18 ++++++--- tests/Mvc/View/Engine/VoltTest.php | 39 +++++++++++++++++++ .../Test/views/frontend/fake/toString.volt | 1 + 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/app/modules/Test/views/frontend/fake/toString.volt diff --git a/src/DI/ServiceManager.php b/src/DI/ServiceManager.php index d479e14..5fca59f 100644 --- a/src/DI/ServiceManager.php +++ b/src/DI/ServiceManager.php @@ -84,8 +84,6 @@ public function hasService($name) try { $service = $this->getService($name); return !empty($service); - } catch (\Phalcon\DI\Exception $ex) { - return false; } catch (\Vegas\DI\Service\Exception $ex) { return false; } diff --git a/src/Mvc/View/Engine/Volt.php b/src/Mvc/View/Engine/Volt.php index 570923e..86a4cea 100644 --- a/src/Mvc/View/Engine/Volt.php +++ b/src/Mvc/View/Engine/Volt.php @@ -13,7 +13,9 @@ namespace Vegas\Mvc\View\Engine; use Vegas\Mvc\View\Engine\Volt\Exception\InvalidFilterException; +use Vegas\Mvc\View\Engine\Volt\Exception\InvalidHelperException; use Vegas\Mvc\View\Engine\Volt\Exception\UnknownFilterException; +use Vegas\Mvc\View\Engine\Volt\Exception\UnknownHelperException; use Vegas\Mvc\View\Engine\Volt\VoltFilterAbstract; use Vegas\Mvc\View\Engine\Volt\VoltHelperAbstract; @@ -47,7 +49,8 @@ public function setExtension($extension) * Registers a new filter in the compiler * * @param $filterName - * @throws Volt\Exception\UnknownFilterException + * @throws InvalidFilterException + * @throws UnknownFilterException */ public function registerFilter($filterName) { @@ -58,8 +61,10 @@ public function registerFilter($filterName) throw new InvalidFilterException(); } $this->getCompiler()->addFilter($filterName, $filterInstance->getFilter()); - } catch (\Exception $e) { + } catch (\ReflectionException $e) { throw new UnknownFilterException(sprintf('Filter \'%s\' does not exist', $filterName)); + } catch (\Exception $e) { + throw new InvalidFilterException(sprintf('Invalid filter \'%s\'', $filterName)); } } @@ -67,7 +72,8 @@ public function registerFilter($filterName) * Registers a new helper in the compiler * * @param $helperName - * @throws Volt\Exception\UnknownFilterException + * @throws InvalidHelperException + * @throws UnknownHelperException */ public function registerHelper($helperName) { @@ -75,11 +81,13 @@ public function registerHelper($helperName) try { $helperInstance = $this->getClassInstance($className); if (!$helperInstance instanceof VoltHelperAbstract) { - throw new InvalidFilterException(); + throw new InvalidHelperException(); } $this->getCompiler()->addFunction($helperName, $helperInstance->getHelper()); + } catch (\ReflectionException $e) { + throw new UnknownHelperException(sprintf('Helper \'%s\' does not exist', $helperName)); } catch (\Exception $e) { - throw new UnknownFilterException(sprintf('Helper \'%s\' does not exist', $helperName)); + throw new InvalidHelperException(sprintf('Invalid helper \'%s\'', $helperName)); } } diff --git a/tests/Mvc/View/Engine/VoltTest.php b/tests/Mvc/View/Engine/VoltTest.php index d878097..b19a6d0 100644 --- a/tests/Mvc/View/Engine/VoltTest.php +++ b/tests/Mvc/View/Engine/VoltTest.php @@ -34,4 +34,43 @@ public function testHelpers() $this->assertEquals('getDI()))->render($page,array()); ?>', $compiler->compileString('{{ pagination(page) }}')); $this->assertEquals('getDI()))->render($page,array(\'class\' => \'test\')); ?>', $compiler->compileString('{{ pagination(page,["class": "test"]) }}')); } + + public function testFilters() + { + $view = new View(); + + $engines = $view->getRegisteredEngines(); + + $volt = $engines['.volt']; + $volt = $volt($view, DI::getDefault()); + + $compiler = $volt->getCompiler(); + $this->assertEquals('', $compiler->compileString('{{ 1|toString }}')); + } + + public function testShouldThrowExceptionForUnknownFilter() + { + $view = new View(); + $volt = new View\Engine\Volt($view); + $exception = null; + try { + $volt->registerFilter('test'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Mvc\View\Engine\Volt\Exception\UnknownFilterException', $exception); + } + + public function testShouldThrowExceptionForUnknownHelper() + { + $view = new View(); + $volt = new View\Engine\Volt($view); + $exception = null; + try { + $volt->registerHelper('test'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Mvc\View\Engine\Volt\Exception\UnknownHelperException', $exception); + } } \ No newline at end of file diff --git a/tests/fixtures/app/modules/Test/views/frontend/fake/toString.volt b/tests/fixtures/app/modules/Test/views/frontend/fake/toString.volt new file mode 100644 index 0000000..db1750f --- /dev/null +++ b/tests/fixtures/app/modules/Test/views/frontend/fake/toString.volt @@ -0,0 +1 @@ +{{ fake|toString }} \ No newline at end of file From 5e04e0c7b793a46e735c149813f05d39dac68365 Mon Sep 17 00:00:00 2001 From: slawek Date: Mon, 15 Dec 2014 08:48:02 +0100 Subject: [PATCH 29/35] Refactoring; View tests; --- ...rsOuputTrait.php => ColorsOutputTrait.php} | 16 +++--- src/Cli/Console.php | 4 +- src/Cli/Task.php | 2 +- src/Mvc/View.php | 1 + tests/Mvc/ViewTest.php | 45 +++++++++++------ tests/fixtures/app/plugins/Foo.php | 4 +- .../app/services/ViewCacheServiceProvider.php | 49 +++++++++++++++++++ 7 files changed, 94 insertions(+), 27 deletions(-) rename src/Cli/{ColorsOuputTrait.php => ColorsOutputTrait.php} (88%) create mode 100644 tests/fixtures/app/services/ViewCacheServiceProvider.php diff --git a/src/Cli/ColorsOuputTrait.php b/src/Cli/ColorsOutputTrait.php similarity index 88% rename from src/Cli/ColorsOuputTrait.php rename to src/Cli/ColorsOutputTrait.php index 6b985d7..7bdb4ce 100644 --- a/src/Cli/ColorsOuputTrait.php +++ b/src/Cli/ColorsOutputTrait.php @@ -16,7 +16,7 @@ * Class ColorsOuputTrait * @package Vegas\Cli */ -trait ColorsOuputTrait +trait ColorsOutputTrait { /** * @var array @@ -68,21 +68,21 @@ public function __construct() * Returns colored string * * @param $string - * @param null $foreground_color - * @param null $background_color + * @param null $foregroundColor + * @param null $backgroundColor * @return string */ - public function getColoredString($string, $foreground_color = null, $background_color = null) { + public function getColoredString($string, $foregroundColor = null, $backgroundColor = null) { $colored_string = ""; // Check if given foreground color found - if (isset($this->foregroundColors[$foreground_color])) { - $colored_string .= "\033[" . $this->foregroundColors[$foreground_color] . "m"; + if (isset($this->foregroundColors[$foregroundColor])) { + $colored_string .= "\033[" . $this->foregroundColors[$foregroundColor] . "m"; } // Check if given background color found - if (isset($this->backgroundColors[$background_color])) { - $colored_string .= "\033[" . $this->backgroundColors[$background_color] . "m"; + if (isset($this->backgroundColors[$backgroundColor])) { + $colored_string .= "\033[" . $this->backgroundColors[$backgroundColor] . "m"; } // Add string and end coloring diff --git a/src/Cli/Console.php b/src/Cli/Console.php index 97dc521..de1dbb4 100644 --- a/src/Cli/Console.php +++ b/src/Cli/Console.php @@ -58,8 +58,10 @@ private function registerSharedData($modules) { $loader->registerNamespaces( array( + $name.'\Forms' => dirname($module['path']).'/forms/', $name.'\Models' => dirname($module['path']).'/models/', - $name.'\Services' => dirname($module['path']).'/services/' + $name.'\Services' => dirname($module['path']).'/services/', + $name.'\Components' => dirname($module['path']).'/components/', ), true ); } diff --git a/src/Cli/Task.php b/src/Cli/Task.php index a05ed92..ae9c2c5 100644 --- a/src/Cli/Task.php +++ b/src/Cli/Task.php @@ -30,7 +30,7 @@ abstract class Task extends \Phalcon\CLI\Task /** * Trait for coloring console output */ - use ColorsOuputTrait; + use ColorsOutputTrait; /** * Task's available options diff --git a/src/Mvc/View.php b/src/Mvc/View.php index 404bfdb..9c42d76 100644 --- a/src/Mvc/View.php +++ b/src/Mvc/View.php @@ -65,6 +65,7 @@ public function __construct($options = null, $viewDir = null) { 'compileAlways' => isset($options['compileAlways']) ? $options['compileAlways'] : false )); } + $volt->registerFilters(); $volt->registerHelpers(); $volt->setExtension('.volt'); diff --git a/tests/Mvc/ViewTest.php b/tests/Mvc/ViewTest.php index 96f5053..3a34bf5 100644 --- a/tests/Mvc/ViewTest.php +++ b/tests/Mvc/ViewTest.php @@ -40,7 +40,8 @@ public function testViewOptions() { $options = array( 'layout' => 'main.volt', - 'layoutsDir' => APP_ROOT.'/app/layouts/' + 'layoutsDir' => APP_ROOT.'/app/layouts/', + 'compileAlways' => true ); $view = new View($options, __DIR__); @@ -51,11 +52,6 @@ public function testViewOptions() public function testPathsResolving() { $configView = $this->getDI()->get('config')->application->view->toArray(); - if (!file_exists($configView['cacheDir'])) { - mkdir($configView['cacheDir'], 0777); - } else { - chmod($configView['cacheDir'], 0777); - } $getContent = function($params) { $this->setUp(); @@ -130,11 +126,7 @@ public function testPathsResolving() public function testPathsResolvingWithoutPartialsDirInConfig() { $configView = $this->getDI()->get('config')->application->view->toArray(); - if (!file_exists($configView['cacheDir'])) { - mkdir($configView['cacheDir'], 0777); - } else { - chmod($configView['cacheDir'], 0777); - } + $getContent = function($params) { $this->setUp(); $this->getDI()->get('config')->application->view->partialsDir = false; @@ -210,11 +202,6 @@ public function testPathsResolvingWithoutPartialsDirInConfig() public function testShortNamespacePathsResolving() { $configView = $this->getDI()->get('config')->application->view->toArray(); - if (!file_exists($configView['cacheDir'])) { - mkdir($configView['cacheDir'], 0777); - } else { - chmod($configView['cacheDir'], 0777); - } $getContent = function($params) { $this->setUp(); @@ -272,5 +259,31 @@ public function testShortNamespacePathsResolving() $this->assertEquals('OUTSIDER', ob_get_contents()); ob_end_clean(); } + + public function testViewCaching() + { + $this->getDI()->get('config')->application->view->compileAlways = false; + $configView = $this->getDI()->get('config')->application->view->toArray(); + + $view = new View($configView); + $this->getDI()->set('view', function() use ($view) { return $view; }); + + if (!file_exists($configView['cacheDir'])) { + mkdir($configView['cacheDir'], 0777); + } else { + chmod($configView['cacheDir'], 0777); + } + + $getContent = function() { + $this->setUp(); + $route = $this->getDI()->get('router')->getRouteByName('test'); + $url = rtrim(str_replace([':action', ':params'], ['test', ''], $route->getPattern()), DIRECTORY_SEPARATOR); + return $this->handleUri($url)->getContent(); + }; + $getContent(); + + $cacheFileKey = str_replace('/', '_', APP_ROOT) . '_app_modules_test_views_frontend_fake_test.volt.php'; + $this->assertFileExists($configView['cacheDir'] . $cacheFileKey); + } } \ No newline at end of file diff --git a/tests/fixtures/app/plugins/Foo.php b/tests/fixtures/app/plugins/Foo.php index 6263802..b1a4dd6 100644 --- a/tests/fixtures/app/plugins/Foo.php +++ b/tests/fixtures/app/plugins/Foo.php @@ -17,8 +17,10 @@ * * @package App\Plugins */ -class Foo +class Foo implements \Phalcon\DI\InjectionAwareInterface { + use \Vegas\DI\InjectionAwareTrait; + /** * @param Event $event * @param Dispatcher $dispatcher diff --git a/tests/fixtures/app/services/ViewCacheServiceProvider.php b/tests/fixtures/app/services/ViewCacheServiceProvider.php new file mode 100644 index 0000000..fbcf0b9 --- /dev/null +++ b/tests/fixtures/app/services/ViewCacheServiceProvider.php @@ -0,0 +1,49 @@ + + * @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. + */ + +class ViewCacheServiceProvider implements \Vegas\DI\ServiceProviderInterface { + + const SERVICE_NAME = 'viewCache'; + + + /** + * {@inheritdoc} + */ + public function register(\Phalcon\DiInterface $di) + { + $config = $di->get('config'); + //Set the views cache service + $di->set(self::SERVICE_NAME, function() use ($config) { + + //Cache data for one day by default + $frontCache = new Phalcon\Cache\Frontend\Output([ + "lifetime" => 1 + ]); + + //File backend settings + $cache = new Phalcon\Cache\Backend\File($frontCache, [ + "cacheDir" => $config->application->view->cacheDir + ]); + + return $cache; + }); + } + + /** + * {@inheritdoc} + */ + public function getDependencies() + { + return []; + } +} + \ No newline at end of file From 6fcb0b49d83c8f293ecbe3590c422d48c5ea9937 Mon Sep 17 00:00:00 2001 From: slawek Date: Tue, 16 Dec 2014 13:59:31 +0100 Subject: [PATCH 30/35] DbRef tests; --- src/Db/Adapter/Mongo/DbRef.php | 62 ++++++ .../Exception/CannotResolveModelException.php | 30 +++ .../Exception/InvalidReferenceException.php | 22 ++ src/Db/Adapter/Mongo/Mapper.php | 195 ++++++++++++++++++ src/Db/Adapter/Mongo/RefResolverTrait.php | 79 +++++++ src/Db/Decorator/CollectionAbstract.php | 4 + .../Helper/ReadNestedAttributeTrait.php | 82 ++++++++ tests/Db/Adapter/Mongo/DbRefTest.php | 101 +++++++++ tests/Db/Adapter/Mongo/MapperTest.php | 37 ++++ .../Db/Adapter/Mongo/RefResolverTraitTest.php | 99 +++++++++ .../Decorator/CollectionAbstractTest.php | 2 +- .../Decorator/ModelAbstractTest.php | 2 +- tests/fixtures/app/modules/Ref/Module.php | 22 ++ .../fixtures/app/modules/Ref/models/Child.php | 27 +++ .../app/modules/Ref/models/Parental.php | 23 +++ .../services/MongoMapperServiceProvider.php | 42 ++++ 16 files changed, 827 insertions(+), 2 deletions(-) create mode 100644 src/Db/Adapter/Mongo/DbRef.php create mode 100644 src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php create mode 100644 src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php create mode 100644 src/Db/Adapter/Mongo/Mapper.php create mode 100644 src/Db/Adapter/Mongo/RefResolverTrait.php create mode 100644 src/Db/Decorator/Helper/ReadNestedAttributeTrait.php create mode 100644 tests/Db/Adapter/Mongo/DbRefTest.php create mode 100644 tests/Db/Adapter/Mongo/MapperTest.php create mode 100644 tests/Db/Adapter/Mongo/RefResolverTraitTest.php rename tests/Db/{Adapter => }/Decorator/CollectionAbstractTest.php (97%) rename tests/Db/{Adapter => }/Decorator/ModelAbstractTest.php (98%) create mode 100644 tests/fixtures/app/modules/Ref/Module.php create mode 100644 tests/fixtures/app/modules/Ref/models/Child.php create mode 100644 tests/fixtures/app/modules/Ref/models/Parental.php create mode 100644 tests/fixtures/app/services/MongoMapperServiceProvider.php diff --git a/src/Db/Adapter/Mongo/DbRef.php b/src/Db/Adapter/Mongo/DbRef.php new file mode 100644 index 0000000..34c4d29 --- /dev/null +++ b/src/Db/Adapter/Mongo/DbRef.php @@ -0,0 +1,62 @@ + + * @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\Db\Adapter\Mongo; + +use Phalcon\Db\Adapter\Mongo\Collection; +use Phalcon\Db\Adapter\Mongo\Document; + +/** + * Class DbRef + * Allows to create MongoDBRef directly from \Phalcon\Mvc\Collection object + * + * $user = \User\Models\User::findFirst(); + * $doc = new \Content\Models\Article(); + * + * //create only from entity + * $doc->creator = \Vegas\Db\Adapter\Mongo\DbRef::create($user); + * //create from collection name and entity + * $doc->creator = \Vegas\Db\Adapter\Mongo\DbRef::create('User', $user); + * //create from collection name and ID + * $doc->creator = \Vegas\Db\Adapter\Mongo\DbRef::create('User', $user->getId()); + * + * + * @package Goplato\Db\Adapter\Mongo + * @return array + */ +class DbRef extends \MongoDBRef +{ + + /** + * Creates a new database reference + * + * @param string|\Phalcon\Mvc\Collection $collection + * @param mixed|\MongoId|\Phalcon\Mvc\Collection $id + * @param string $database + * @return array + */ + public static function create($collection, $id = null, $database = null) + { + if ($collection instanceof \Phalcon\Mvc\Collection) { + $id = $collection->getId(); + $collection = $collection->getSource(); + } + if ($id instanceof \Phalcon\Mvc\Collection) { + $id = $id->getId(); + } + if (!$id instanceof \MongoId && $id !== null) { + $id = new \MongoId($id); + } + + return parent::create($collection, $id, $database); + } +} \ No newline at end of file diff --git a/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php b/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php new file mode 100644 index 0000000..31bb9e3 --- /dev/null +++ b/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php @@ -0,0 +1,30 @@ + + * @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\Db\Adapter\Mongo\Exception; + +/** + * Class CannotResolveModelException + * @package Vegas\Db\Adapter\Mongo\Exception + */ +class CannotResolveModelException extends \Vegas\Db\Exception +{ + protected $message = 'Cannot resolve model for \'%s\' collection'; + + /** + * @param string $collectionName + */ + public function __construct($collectionName) + { + $this->message = sprintf($this->message, $collectionName); + } +} \ No newline at end of file diff --git a/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php b/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php new file mode 100644 index 0000000..908c9e7 --- /dev/null +++ b/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php @@ -0,0 +1,22 @@ + + * @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\Db\Adapter\Mongo\Exception; + +/** + * Class InvalidReferenceException + * @package Vegas\Db\Adapter\Mongo\Exception + */ +class InvalidReferenceException extends \Vegas\Db\Exception +{ + protected $message = 'Object is not in valid database reference format'; +} \ No newline at end of file diff --git a/src/Db/Adapter/Mongo/Mapper.php b/src/Db/Adapter/Mongo/Mapper.php new file mode 100644 index 0000000..cea3644 --- /dev/null +++ b/src/Db/Adapter/Mongo/Mapper.php @@ -0,0 +1,195 @@ + + * @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\Db\Adapter\Mongo; + +use Vegas\Db\Adapter\Mongo\Exception\CannotResolveModelException; +use Vegas\Util\FileWriter; + +/** + * Class Mapper + * @package Vegas\Db\Adapter\Mongo + */ +class Mapper +{ + /** + * Collections mapped to model classes + * Array key is a name of collection + * Array value is a full model class name + * @var array + */ + protected $map = []; + + /** + * Container for already instantiated classes + * @var array + */ + protected $modelInstances = []; + + /** + * @param string $inputDirectory Path to directory containing model classes + * @param string $modelClassPattern Regex pattern to find model classes + */ + public function __construct($inputDirectory, $modelClassPattern = '/(.*)models\/(.*)\.php/i') + { + $this->inputDirectory = $inputDirectory; + $this->modelClassPattern = '/(.*)models\/(.*)\.php/i'; + } + + /** + * Setups Mongo collections map + * If cache file already exists, then map is read directly from this file + * + * @param string $cacheFilePath Path to cache file. If null then cache is skipped + * @return $this + */ + public function create($cacheFilePath = null) + { + $map = []; + if ($cacheFilePath) { + $map = $this->resolveCachedMap($cacheFilePath); + } + if (!is_array($map)) { + $map = $this->createMap(); + if ($cacheFilePath) { + $this->cacheMap($cacheFilePath, $map); + } + } + + $this->map = $map; + + return $map; + } + + /** + * Resolves model class name for given collection name + * + * @param string $collectionName + * @throws CannotResolveModelException + * @return object + */ + public function resolveModel($collectionName) + { + if (!array_key_exists($collectionName, $this->map)) { + throw new CannotResolveModelException($collectionName); + } + $modelClassName = $this->map[$collectionName]; + try { + if (!array_key_exists($modelClassName, $this->modelInstances)) { + $reflectionClass = new \ReflectionClass($modelClassName); + $modelInstance = $reflectionClass->newInstance(); + $this->modelInstances[$modelClassName] = $modelInstance; + } + return $this->modelInstances[$modelClassName]; + } catch (\Exception $e) { + throw new CannotResolveModelException($collectionName); + } + } + + /** + * Creates map by traversing input directory + * + * @return array + */ + private function createMap() + { + $map = []; + //browse directories recursively + $directoryIterator = new \RecursiveDirectoryIterator($this->inputDirectory); + $iterator = new \RecursiveIteratorIterator($directoryIterator); + foreach ($iterator as $file) { + if ($file->getFileName() == '.' || $file->getFileName() == '..') { + continue; + } + if (!preg_match($this->modelClassPattern, $file->getPathname())) { + continue; + } + //resolves name of class + $className = $this->resolveFileClassName($file); + try { + $classInstance = $this->instantiateClass($className); + if ($classInstance instanceof \Phalcon\Mvc\Collection) { + $map[$classInstance->getSource()] = $className; + } + } catch (\Exception $e) { + continue; + } + } + + return $map; + } + + /** + * Returns map from cache + * + * @param string $cacheFilePath Path to cache file + * @return bool|mixed + */ + private function resolveCachedMap($cacheFilePath) + { + $cachedContent = false; + if (file_exists($cacheFilePath)) { + $cachedContent = require $cacheFilePath; + } + + return $cachedContent; + } + + /** + * Saves generated map to file + * + * @param string $cacheFilePath Path to cache file + * @param array $map Generated map + * @return int + */ + private function cacheMap($cacheFilePath, $map) + { + return FileWriter::writeObject($cacheFilePath, $map, true); + } + + /** + * Creates an instance from given class name + * + * @param string $className + * @return object + */ + protected function instantiateClass($className) + { + $reflectionClass = new \ReflectionClass($className); + return $reflectionClass->newInstance(); + } + + /** + * Resolves name of class inside given file + * + * @param \SplFileInfo $fileInfo + * @return string + */ + protected function resolveFileClassName(\SplFileInfo $fileInfo) + { + $relativeFilePath = str_replace( + $this->inputDirectory, + '', + $fileInfo->getPath()) . DIRECTORY_SEPARATOR . + $fileInfo->getBasename('.' . $fileInfo->getExtension() + ); + //converts file path to namespace + //DIRECTORY_SEPARATOR will be converted to namespace separator => \ + //each directory name will be converted to first upper case + $splitPath = explode(DIRECTORY_SEPARATOR, $relativeFilePath); + $namespace = implode('\\', array_map(function($item) { + return ucfirst($item); + }, $splitPath)); + return $namespace; + } + +} \ No newline at end of file diff --git a/src/Db/Adapter/Mongo/RefResolverTrait.php b/src/Db/Adapter/Mongo/RefResolverTrait.php new file mode 100644 index 0000000..33db5f7 --- /dev/null +++ b/src/Db/Adapter/Mongo/RefResolverTrait.php @@ -0,0 +1,79 @@ + + * @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\Db\Adapter\Mongo; +use Vegas\Db\Adapter\Mongo\Exception\InvalidReferenceException; + +/** + * Class RefResolverTrait + * @package Vegas\Db\Adapter\Mongo + */ +trait RefResolverTrait +{ + /** + * Returns corresponding object indicated by MongoDBRef + * + * @param $fieldName + * @return mixed + * @throws InvalidReferenceException + */ + public function readRef($fieldName) + { + $oRef = $this->readNestedAttribute($fieldName); + if (!\MongoDBRef::isRef($oRef)) { + throw new InvalidReferenceException(); + } + if (isset($this->dbRefs) && isset($this->dbRefs[$fieldName])) { + $modelInstance = $this->instantiateModel($this->dbRefs[$fieldName]); + } else if ($this->getDI()->has('mongoMapper')) { + $modelInstance = $this->getDI()->get('mongoMapper')->resolveModel($oRef['$ref']); + } else { + return $oRef; + } + return forward_static_call(array($modelInstance, 'findById'), $oRef['$id']); + } + + /** + * Creates an instance from given model class name + * + * @param $modelName + * @return object + */ + protected function instantiateModel($modelName) + { + $reflectionClass = new \ReflectionClass($modelName); + return $reflectionClass->newInstance(); + } + + /** + * Returns dependency injector + * + * @return \Phalcon\DiInterface + */ + abstract public function getDI(); + + /** + * Reads attribute value by its name + * + * @param $attribute + * @return mixed + */ + abstract public function readAttribute($attribute); + + /** + * Reads nested object + * + * @param $attribute + * @return mixed + */ + abstract public function readNestedAttribute($attribute); +} \ No newline at end of file diff --git a/src/Db/Decorator/CollectionAbstract.php b/src/Db/Decorator/CollectionAbstract.php index ba39b77..f9ae606 100644 --- a/src/Db/Decorator/CollectionAbstract.php +++ b/src/Db/Decorator/CollectionAbstract.php @@ -12,7 +12,9 @@ namespace Vegas\Db\Decorator; use Phalcon\Mvc\Collection; +use Vegas\Db\Adapter\Mongo\RefResolverTrait; use Vegas\Db\Decorator\Helper\MappingHelperTrait; +use Vegas\Db\Decorator\Helper\ReadNestedAttributeTrait; use Vegas\Db\Decorator\Helper\SlugTrait; use Vegas\Db\Decorator\Helper\WriteAttributesTrait; use Vegas\Db\MappingResolverTrait; @@ -27,6 +29,8 @@ abstract class CollectionAbstract extends Collection use MappingHelperTrait; use SlugTrait; use WriteAttributesTrait; + use ReadNestedAttributeTrait; + use RefResolverTrait; /** * Event fired when record is being created diff --git a/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php new file mode 100644 index 0000000..51a82f7 --- /dev/null +++ b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php @@ -0,0 +1,82 @@ + + * @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\Db\Decorator\Helper; + +/** + * Class ReadNestedAttributeTrait + * @package Vegas\Db\Decorator\Helper + */ +trait ReadNestedAttributeTrait +{ + /** + * Reads nested object + * Usage + * + * //reading object + * $coll = new Collection(); + * $coll->a = new \stdClass(); + * $coll->a->b = new \stdClass(); + * $coll->a->b->c = 'val'; + * + * $coll->readNestedAttribute('a.b.c');//returns 'val' + * + * //reading array + * $coll = new Collection(); + * $coll->a = [ + * 'b' => [ + * 'c' => 'val' + * ] + * ]; + * $coll->readNestedAttribute('a.b.c');//returns 'val' + * + * + * @param $attributeName + * @return mixed + */ + public function readNestedAttribute($attributeName) + { + $keys = explode('.', $attributeName); + return $this->traverseObject($this->readAttribute($keys[0]), array_splice($keys, 1)); + } + + /** + * Recursive traversing object based on properties from array + * + * @param $obj + * @param $keys + * @return mixed + */ + private function traverseObject($obj, $keys) + { + if (empty($keys)) { + return $obj; + } + $key = current($keys); + + if (is_array($obj) && isset($obj[$key])) { + return $this->traverseObject($obj[$key], array_slice($keys, 1)); + } else if (is_object($obj) && isset($obj->{$key})) { + return $this->traverseObject($obj->{$key}, array_slice($keys, 1)); + } else { + return null; + } + } + + /** + * Returns attribute value by its name + * + * @param $attribute + * @return mixed + */ + abstract public function readAttribute($attribute); +} \ No newline at end of file diff --git a/tests/Db/Adapter/Mongo/DbRefTest.php b/tests/Db/Adapter/Mongo/DbRefTest.php new file mode 100644 index 0000000..120d3da --- /dev/null +++ b/tests/Db/Adapter/Mongo/DbRefTest.php @@ -0,0 +1,101 @@ + + * @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\Db\Adapter\Mongo; + +use Ref\Models\Child; +use Ref\Models\Parental; +use Vegas\Db\Adapter\Mongo\DbRef; + +class DbRefTest extends \Vegas\Test\TestCase +{ + public function tearDown() + { + parent::setUp(); + foreach (Parental::find() as $p) { + $p->delete(); + } + foreach (Child::find() as $c) { + $c->delete(); + } + } + + public function testShouldCreateMongoDBRefFromEntity() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create($parent); + $child->save(); + + $this->assertEquals((string)$child->readRef('parent')->getId(), (string)$parent->getId()); + } + + public function testShouldCreateMongoDBRefFromCollectionAndEntity() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create('Parental', $parent); + $child->save(); + + $this->assertEquals((string)$child->readRef('parent')->getId(), (string)$parent->getId()); + } + + public function testShouldCreateMongoDBRefFromCollectionAndMongoId() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create('Parental', $parent->getId()); + $child->save(); + + $this->assertEquals((string)$child->readRef('parent')->getId(), (string)$parent->getId()); + } + + public function testShouldCreateMongoDBRefFromCollectionAndStringId() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create('Parental', (string)$parent->getId()); + $child->save(); + + $this->assertEquals((string)$child->readRef('parent')->getId(), (string)$parent->getId()); + } + + public function testShouldThrowExceptionForInvalidId() + { + $exception = null; + try { + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create('Parental', 'fake'); + $child->save(); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\MongoException', $exception); + } +} \ No newline at end of file diff --git a/tests/Db/Adapter/Mongo/MapperTest.php b/tests/Db/Adapter/Mongo/MapperTest.php new file mode 100644 index 0000000..bddda9a --- /dev/null +++ b/tests/Db/Adapter/Mongo/MapperTest.php @@ -0,0 +1,37 @@ + + * @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\Db\Adapter\Mongo; + + +use Phalcon\DI; + +class MapperTest extends \Vegas\Test\TestCase +{ + + public function testShouldCreateMapAndSaveToFile() + { + $tmpFilePath = APP_ROOT . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'mongo.map.php'; + $mapper = new Mapper(APP_ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'modules'); + $map = $mapper->create($tmpFilePath); + + $this->assertFileExists($tmpFilePath); + + $this->assertInternalType('array', $map); + $this->assertNotEmpty($map); + + $this->assertArrayHasKey('Child', $map); + $this->assertTrue(in_array('\Ref\Models\Child', $map)); + + } +} \ No newline at end of file diff --git a/tests/Db/Adapter/Mongo/RefResolverTraitTest.php b/tests/Db/Adapter/Mongo/RefResolverTraitTest.php new file mode 100644 index 0000000..4847a9f --- /dev/null +++ b/tests/Db/Adapter/Mongo/RefResolverTraitTest.php @@ -0,0 +1,99 @@ + + * @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\Db\Adapter\Mongo; + + +use Ref\Models\Child; +use Ref\Models\Parental; +use Vegas\Db\Adapter\Mongo\DbRef; + +class RefResolverTraitTest extends \Vegas\Test\TestCase +{ + public function testShouldFindCorrespondingRecordFromMapping() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create($parent); + $child->save(); + + $correspondingRecord = $child->readRef('parent'); + $this->assertInstanceOf('Ref\Models\Parental', $correspondingRecord); + $this->assertEquals((string) $parent->getId(), (string) $correspondingRecord->getId()); + } + + public function testShouldFindCorrespondingRecordFromPropertyMap() + { + $grandParent = new Parental(); + $grandParent ->name = 'Grand parent'; + $grandParent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->grandParent = DbRef::create($grandParent); + $child->save(); + + $correspondingRecord = $child->readRef('grandParent'); + $this->assertInstanceOf('Ref\Models\Parental', $correspondingRecord); + $this->assertEquals((string) $grandParent->getId(), (string) $correspondingRecord->getId()); + } + + public function testShouldReturnOriginValueWhenNoResolved() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create($parent); + $child->save(); + + $this->getDI()->remove('mongoMapper'); + $this->assertEquals(DbRef::create($parent)['$id'], $child->readRef('parent')['$id']); + $this->assertEquals(DbRef::create($parent)['$ref'], $child->readRef('parent')['$ref']); + } + + public function testShouldThrowExceptionForInvalidMongoDBRef() + { + $parent = new Parental(); + $parent->name = 'Parent'; + $parent->save(); + + $child = new Child(); + $child->name = 'Child'; + $child->parent = DbRef::create($parent); + $child->save(); + + $exception = null; + try { + $child->readRef('name'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Adapter\Mongo\Exception\InvalidReferenceException', $exception); + + $exception = null; + try { + $child->readRef('fake.nested'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Adapter\Mongo\Exception\InvalidReferenceException', $exception); + } + +} \ No newline at end of file diff --git a/tests/Db/Adapter/Decorator/CollectionAbstractTest.php b/tests/Db/Decorator/CollectionAbstractTest.php similarity index 97% rename from tests/Db/Adapter/Decorator/CollectionAbstractTest.php rename to tests/Db/Decorator/CollectionAbstractTest.php index 1f3b25f..7aec57b 100644 --- a/tests/Db/Adapter/Decorator/CollectionAbstractTest.php +++ b/tests/Db/Decorator/CollectionAbstractTest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Vegas\Tests\Db\Adapter\Decorator; +namespace Vegas\Tests\Db\Decorator; use Phalcon\Utils\Slug; use Vegas\Db\Decorator\CollectionAbstract; diff --git a/tests/Db/Adapter/Decorator/ModelAbstractTest.php b/tests/Db/Decorator/ModelAbstractTest.php similarity index 98% rename from tests/Db/Adapter/Decorator/ModelAbstractTest.php rename to tests/Db/Decorator/ModelAbstractTest.php index 7429452..025807c 100644 --- a/tests/Db/Adapter/Decorator/ModelAbstractTest.php +++ b/tests/Db/Decorator/ModelAbstractTest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Vegas\Tests\Db\Adapter\Decorator; +namespace Vegas\Tests\Db\Decorator; use Phalcon\DI; use Phalcon\Utils\Slug; diff --git a/tests/fixtures/app/modules/Ref/Module.php b/tests/fixtures/app/modules/Ref/Module.php new file mode 100644 index 0000000..78b77ed --- /dev/null +++ b/tests/fixtures/app/modules/Ref/Module.php @@ -0,0 +1,22 @@ + + * @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 Ref; + +class Module extends \Vegas\Mvc\ModuleAbstract +{ + public function __construct() + { + $this->namespace = __NAMESPACE__; + $this->dir = __DIR__; + } +} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Ref/models/Child.php b/tests/fixtures/app/modules/Ref/models/Child.php new file mode 100644 index 0000000..e95dcb1 --- /dev/null +++ b/tests/fixtures/app/modules/Ref/models/Child.php @@ -0,0 +1,27 @@ + + * @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 Ref\Models; + +use Vegas\Db\Decorator\CollectionAbstract; + +class Child extends CollectionAbstract +{ + protected $dbRefs = [ + 'grandParent' => '\Ref\Models\Parental' + ]; + + public function getSource() + { + return 'Child'; + } +} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Ref/models/Parental.php b/tests/fixtures/app/modules/Ref/models/Parental.php new file mode 100644 index 0000000..1d621dc --- /dev/null +++ b/tests/fixtures/app/modules/Ref/models/Parental.php @@ -0,0 +1,23 @@ + + * @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 Ref\Models; + +use Vegas\Db\Decorator\CollectionAbstract; + +class Parental extends CollectionAbstract +{ + public function getSource() + { + return 'Parental'; + } +} \ No newline at end of file diff --git a/tests/fixtures/app/services/MongoMapperServiceProvider.php b/tests/fixtures/app/services/MongoMapperServiceProvider.php new file mode 100644 index 0000000..573d893 --- /dev/null +++ b/tests/fixtures/app/services/MongoMapperServiceProvider.php @@ -0,0 +1,42 @@ + + * @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. + */ + +use Vegas\DI\ServiceProviderInterface; +use Phalcon\DiInterface; + +class MongoMapperServiceProvider implements ServiceProviderInterface +{ + const SERVICE_NAME = 'mongoMapper'; + + /** + * {@inheritdoc} + */ + public function register(DiInterface $di) + { + $di->set(self::SERVICE_NAME, function() use ($di) { + $mapper = new \Vegas\Db\Adapter\Mongo\Mapper(APP_ROOT . '/app/modules/'); + $mapper->create(APP_ROOT . '/app/config/mongo.map.php'); + return $mapper; + }, true); + } + + /** + * {@inheritdoc} + */ + public function getDependencies() + { + return [ + CollectionManagerServiceProvider::SERVICE_NAME, + MongoMapperServiceProvider::SERVICE_NAME + ]; + } +} \ No newline at end of file From ce4f68c2d9150af3a97ab932b9686950cead9cb3 Mon Sep 17 00:00:00 2001 From: slawek Date: Tue, 16 Dec 2014 19:25:48 +0100 Subject: [PATCH 31/35] Added Db helpers tests, Mongo mapper tests, Scaffolding mongo adapter tests; --- src/Db/Adapter/Mongo/Mapper.php | 3 + .../Helper/ReadNestedAttributeTrait.php | 5 +- tests/DI/Scaffolding/Adapter/MongoTest.php | 30 ++++++++ tests/Db/Adapter/Mongo/MapperTest.php | 76 +++++++++++++++++-- .../Helper/ReadNestedAttributeTraitTest.php | 75 ++++++++++++++++++ tests/Db/Decorator/Helper/SlugTraitTest.php | 28 +++++++ .../Helper/WriteAttributesTraitTest.php | 38 ++++++++++ 7 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 tests/DI/Scaffolding/Adapter/MongoTest.php create mode 100644 tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php create mode 100644 tests/Db/Decorator/Helper/SlugTraitTest.php create mode 100644 tests/Db/Decorator/Helper/WriteAttributesTraitTest.php diff --git a/src/Db/Adapter/Mongo/Mapper.php b/src/Db/Adapter/Mongo/Mapper.php index cea3644..4d98dc9 100644 --- a/src/Db/Adapter/Mongo/Mapper.php +++ b/src/Db/Adapter/Mongo/Mapper.php @@ -153,6 +153,9 @@ private function resolveCachedMap($cacheFilePath) */ private function cacheMap($cacheFilePath, $map) { + if (!file_exists(dirname($cacheFilePath))) { + mkdir(dirname($cacheFilePath), 0777, true); + } return FileWriter::writeObject($cacheFilePath, $map, true); } diff --git a/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php index 51a82f7..e8084bb 100644 --- a/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php +++ b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php @@ -41,10 +41,13 @@ trait ReadNestedAttributeTrait * * * @param $attributeName - * @return mixed + * @return mixed|null */ public function readNestedAttribute($attributeName) { + if (!$attributeName) { + return null; + } $keys = explode('.', $attributeName); return $this->traverseObject($this->readAttribute($keys[0]), array_splice($keys, 1)); } diff --git a/tests/DI/Scaffolding/Adapter/MongoTest.php b/tests/DI/Scaffolding/Adapter/MongoTest.php new file mode 100644 index 0000000..3af9f48 --- /dev/null +++ b/tests/DI/Scaffolding/Adapter/MongoTest.php @@ -0,0 +1,30 @@ + + * @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; + +class MongoTest extends \Vegas\Test\TestCase +{ + + public function testShouldSetupOwnCollectionManager() + { + $collectionManager = $this->getDI()->get('collectionManager'); + + $this->getDI()->remove('collectionManager'); + new \Vegas\DI\Scaffolding\Adapter\Mongo(); + $this->assertInstanceOf('\Phalcon\Mvc\Collection\Manager', $this->getDI()->get('collectionManager')); + + + $this->getDI()->set('collectionManager', $collectionManager, true); + } +} + \ No newline at end of file diff --git a/tests/Db/Adapter/Mongo/MapperTest.php b/tests/Db/Adapter/Mongo/MapperTest.php index bddda9a..6a2b72a 100644 --- a/tests/Db/Adapter/Mongo/MapperTest.php +++ b/tests/Db/Adapter/Mongo/MapperTest.php @@ -9,23 +9,48 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace Vegas\Db\Adapter\Mongo; - use Phalcon\DI; class MapperTest extends \Vegas\Test\TestCase { + private $tmpFilePath; + + private function getTmpFilePath() + { + return APP_ROOT . DIRECTORY_SEPARATOR . 'tmp' + . DIRECTORY_SEPARATOR . 'mongo_map' + . DIRECTORY_SEPARATOR . uniqid() . '.php'; + } + + public function setUp() + { + parent::setUp(); + + $this->tmpFilePath = $this->getTmpFilePath(); + if (file_exists($this->tmpFilePath)) { + unlink($this->tmpFilePath); + } + if (file_exists(dirname($this->tmpFilePath))) { + rmdir(dirname($this->tmpFilePath)); + } + } + + public function tearDown() + { + if (file_exists($this->tmpFilePath)) { + unlink($this->tmpFilePath); + } + } public function testShouldCreateMapAndSaveToFile() { - $tmpFilePath = APP_ROOT . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'mongo.map.php'; $mapper = new Mapper(APP_ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'modules'); - $map = $mapper->create($tmpFilePath); + $map = $mapper->create($this->tmpFilePath); - $this->assertFileExists($tmpFilePath); + $this->assertFileExists($this->tmpFilePath); $this->assertInternalType('array', $map); $this->assertNotEmpty($map); @@ -33,5 +58,46 @@ public function testShouldCreateMapAndSaveToFile() $this->assertArrayHasKey('Child', $map); $this->assertTrue(in_array('\Ref\Models\Child', $map)); + $cachedMap = $mapper->create($this->tmpFilePath); + $this->assertSame($cachedMap, $map); + } + + public function testShouldResolveModelFromMap() + { + $mapper = new Mapper(APP_ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'modules'); + $map = $mapper->create($this->tmpFilePath); + + $model = $mapper->resolveModel(array_keys($map)[0]); + $this->assertNotNull($model); + $this->assertInstanceOf(array_values($map)[0], $model); + } + + public function testShouldThrowExceptionForNotResolvedModel() + { + $mapper = new Mapper(APP_ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'modules'); + $map = $mapper->create($this->tmpFilePath); + + $exception = null; + try { + $mapper->resolveModel('fakecollection'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Adapter\Mongo\Exception\CannotResolveModelException', $exception); + + //inject fake model class name + $map['fakecollection'] = '\Fake\Models\NotExisting'; + $reflectionObject = new \ReflectionObject($mapper); + $mapProperty = $reflectionObject->getProperty('map'); + $mapProperty->setAccessible(true); + $mapProperty->setValue($mapper, $map); + + $exception = null; + try { + $mapper->resolveModel('fakecollection'); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Adapter\Mongo\Exception\CannotResolveModelException', $exception); } } \ No newline at end of file diff --git a/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php b/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php new file mode 100644 index 0000000..0f5956c --- /dev/null +++ b/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php @@ -0,0 +1,75 @@ + + * @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\Db\Decorator\Helper; + +use Vegas\Db\Decorator\CollectionAbstract; + +class NestedAttrTestModel extends CollectionAbstract {} + + +class ReadNestedAttributeTraitTest extends \PHPUnit_Framework_TestCase +{ + public function testShouldReadNestedAttributesFromArray() + { + $test = new NestedAttrTestModel(); + $test->a = [ + 'b' => [ + 'c' => 'test' + ] + ]; + + $this->assertInternalType('array', $test->a); + $this->assertEquals('test', $test->readNestedAttribute('a.b.c')); + $this->assertSame(['c' => 'test'], $test->readNestedAttribute('a.b')); + $this->assertSame(['b' => ['c' => 'test']], $test->readNestedAttribute('a')); + } + + public function testShouldReadNestedAttributesFromObject() + { + $test = new NestedAttrTestModel(); + + $a = new \stdClass(); + $a->b = new \stdClass(); + $a->b->c = 'test'; + $test->a = $a; + + $this->assertInternalType('object', $test->a); + $this->assertEquals('test', $test->readNestedAttribute('a.b.c')); + $this->assertSame($a->b, $test->readNestedAttribute('a.b')); + $this->assertSame($a, $test->readNestedAttribute('a')); + } + + public function testShouldReturnNullForInvalidNestedAttrName() + { + $test = new NestedAttrTestModel(); + + $a = new \stdClass(); + $a->b = new \stdClass(); + $a->b->c = 'test'; + $test->a = $a; + $this->assertNull($test->readNestedAttribute('a.b.c.d')); + + $test->a = [ + 'b' => [ + 'c' => 'test' + ] + ]; + $this->assertNull($test->readNestedAttribute('a.b.c.d')); + + $this->assertNull($test->readNestedAttribute('')); + $this->assertNull($test->readNestedAttribute(false)); + $this->assertNull($test->readNestedAttribute(0)); + $this->assertNull($test->readNestedAttribute(1)); + } +} + \ No newline at end of file diff --git a/tests/Db/Decorator/Helper/SlugTraitTest.php b/tests/Db/Decorator/Helper/SlugTraitTest.php new file mode 100644 index 0000000..3cd353e --- /dev/null +++ b/tests/Db/Decorator/Helper/SlugTraitTest.php @@ -0,0 +1,28 @@ + + * @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\Db\Decorator\Helper; + +class SlugTestModel extends \Vegas\Db\Decorator\CollectionAbstract {} + +class SlugTraitTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldGenerateSlug() + { + $test = new SlugTestModel(); + $test->generateSlug('Lorem ipsum'); + + $this->assertEquals(\Phalcon\Utils\Slug::generate('Lorem ipsum'), $test->slug); + } +} + \ No newline at end of file diff --git a/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php b/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php new file mode 100644 index 0000000..a5e1700 --- /dev/null +++ b/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php @@ -0,0 +1,38 @@ + + * @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\Db\Decorator\Helper; + +use Vegas\Db\Decorator\CollectionAbstract; + +class AttrTestModel extends CollectionAbstract {} + + +class WriteAttributesTraitTest extends \PHPUnit_Framework_TestCase +{ + public function testShouldWriteArrayOfAttributes() + { + $attrs = [ + 'test' => 1, + 'test2' => 2, + 'test3' => 3 + ]; + + $test = new AttrTestModel(); + $test->writeAttributes($attrs); + + $this->assertEquals(1, $test->readAttribute('test')); + $this->assertEquals(2, $test->readAttribute('test2')); + $this->assertEquals(3, $test->readAttribute('test3')); + } +} + \ No newline at end of file From 5281e006e8ae9f183cbb30f15222fe7c97ded89b Mon Sep 17 00:00:00 2001 From: slawek Date: Tue, 16 Dec 2014 20:02:13 +0100 Subject: [PATCH 32/35] Bootstrap tests + little refactoring; Removed Http\Response\Json; CLI refactoring; --- src/Bootstrap/EnvironmentInitializerTrait.php | 4 +- src/Bootstrap/LoaderInitializerTrait.php | 2 +- src/Bootstrap/ModulesInitializerTrait.php | 2 +- src/Bootstrap/RoutesInitializerTrait.php | 2 +- src/Bootstrap/ServicesInitializerTrait.php | 2 +- src/Cli/ColorsOutputTrait.php | 20 ---- src/Cli/EventsListener/TaskListener.php | 3 +- src/Cli/{Task.php => TaskAbstract.php} | 39 ++++--- src/Http/Response/Json.php | 104 ------------------ src/Mvc/ControllerAbstract.php | 3 +- src/Task/AssetsTask.php | 5 +- src/Task/CacheTask.php | 5 +- tests/App/TestCase.php | 44 -------- .../EnvironmentInitializerTraitTest.php | 68 ++++++++++++ tests/Http/Response/JsonTest.php | 42 ------- .../app/modules/Foo/tasks/FooTask.php | 2 +- .../app/modules/Test/tasks/FooTask.php | 4 +- tests/fixtures/app/tasks/CustomTask.php | 4 +- 18 files changed, 113 insertions(+), 242 deletions(-) rename src/Cli/{Task.php => TaskAbstract.php} (91%) delete mode 100644 src/Http/Response/Json.php delete mode 100644 tests/App/TestCase.php create mode 100644 tests/Bootstrap/EnvironmentInitializerTraitTest.php delete mode 100644 tests/Http/Response/JsonTest.php diff --git a/src/Bootstrap/EnvironmentInitializerTrait.php b/src/Bootstrap/EnvironmentInitializerTrait.php index b034f21..47204a0 100644 --- a/src/Bootstrap/EnvironmentInitializerTrait.php +++ b/src/Bootstrap/EnvironmentInitializerTrait.php @@ -15,9 +15,9 @@ trait EnvironmentInitializerTrait /** * Initializes application environment */ - protected function initEnvironment(Config $config) + public function initEnvironment(Config $config) { - if (isset($config->application->environment)) { + if (isset($config->application) && isset($config->application->environment)) { $env = $config->application->environment; } else { $env = Constants::DEFAULT_ENV; diff --git a/src/Bootstrap/LoaderInitializerTrait.php b/src/Bootstrap/LoaderInitializerTrait.php index c87b5a2..ffccffb 100644 --- a/src/Bootstrap/LoaderInitializerTrait.php +++ b/src/Bootstrap/LoaderInitializerTrait.php @@ -22,7 +22,7 @@ trait LoaderInitializerTrait * Initializes loader * Registers library and plugin directory */ - protected function initLoader(Config $config) + public function initLoader(Config $config) { $loader = new Loader(); $loader->registerDirs( diff --git a/src/Bootstrap/ModulesInitializerTrait.php b/src/Bootstrap/ModulesInitializerTrait.php index 1ef67e1..9801d13 100644 --- a/src/Bootstrap/ModulesInitializerTrait.php +++ b/src/Bootstrap/ModulesInitializerTrait.php @@ -27,7 +27,7 @@ trait ModulesInitializerTrait /** * Initializes application modules */ - protected function initModules(Config $config) + public function initModules(Config $config) { $moduleLoader = new ModuleLoader($this->getDI()); //registers modules defined in modules.php file diff --git a/src/Bootstrap/RoutesInitializerTrait.php b/src/Bootstrap/RoutesInitializerTrait.php index ea01d4b..f236047 100644 --- a/src/Bootstrap/RoutesInitializerTrait.php +++ b/src/Bootstrap/RoutesInitializerTrait.php @@ -18,7 +18,7 @@ trait RoutesInitializerTrait /** * Initializes routing */ - protected function initRoutes(Config $config) + public function initRoutes(Config $config) { //setups router $routerAdapter = new Standard($this->getDI()); diff --git a/src/Bootstrap/ServicesInitializerTrait.php b/src/Bootstrap/ServicesInitializerTrait.php index 010c5fc..426b139 100644 --- a/src/Bootstrap/ServicesInitializerTrait.php +++ b/src/Bootstrap/ServicesInitializerTrait.php @@ -20,7 +20,7 @@ trait ServicesInitializerTrait /** * Initializes services */ - protected function initServices(Config $config) + public function initServices(Config $config) { $serviceProviderLoader = new ServiceProviderLoader($this->getDI()); $serviceProviderLoader->autoload( diff --git a/src/Cli/ColorsOutputTrait.php b/src/Cli/ColorsOutputTrait.php index 7bdb4ce..6a0bd35 100644 --- a/src/Cli/ColorsOutputTrait.php +++ b/src/Cli/ColorsOutputTrait.php @@ -90,24 +90,4 @@ public function getColoredString($string, $foregroundColor = null, $backgroundCo return $colored_string; } - - /** - * Returns all foreground color names - * - * @return array - */ - public function getForegroundColors() - { - return array_keys($this->foregroundColors); - } - - /** - * Returns all background color names - * - * @return array - */ - public function getBackgroundColors() - { - return array_keys($this->backgroundColors); - } } \ No newline at end of file diff --git a/src/Cli/EventsListener/TaskListener.php b/src/Cli/EventsListener/TaskListener.php index 9368a39..990b91c 100644 --- a/src/Cli/EventsListener/TaskListener.php +++ b/src/Cli/EventsListener/TaskListener.php @@ -17,6 +17,7 @@ use Phalcon\Events\Event; use Vegas\Cli\OptionParser; use Vegas\Cli\Task; +use Vegas\Cli\TaskAbstract; /** * Class TaskListener @@ -51,7 +52,7 @@ public function beforeHandleTask($argv) */ public function afterHandleTask() { - return function(Event $event, Console $console, Task $task) { + return function(Event $event, Console $console, TaskAbstract $task) { echo $task->getOutput(); }; } diff --git a/src/Cli/Task.php b/src/Cli/TaskAbstract.php similarity index 91% rename from src/Cli/Task.php rename to src/Cli/TaskAbstract.php index ae9c2c5..9956ec2 100644 --- a/src/Cli/Task.php +++ b/src/Cli/TaskAbstract.php @@ -22,7 +22,7 @@ * Class Task * @package Vegas\Cli */ -abstract class Task extends \Phalcon\CLI\Task +abstract class TaskAbstract extends \Phalcon\CLI\Task { const HELP_OPTION = 'help'; const HELP_SHORTOPTION = 'h'; @@ -37,7 +37,7 @@ abstract class Task extends \Phalcon\CLI\Task * * @return mixed */ - abstract public function setOptions(); + abstract public function setupOptions(); /** * Task output buffer @@ -93,7 +93,7 @@ public function beforeExecuteRoute() $this->actionName = $this->dispatcher->getActionName(); $this->taskName = $this->dispatcher->getTaskName(); - $this->setOptions(); + $this->setupOptions(); //if -h or --help option was typed in command line then show only help $this->args = $this->dispatcher->getParam('args'); @@ -105,7 +105,7 @@ public function beforeExecuteRoute() try { $this->validate($this->args); } catch (InvalidArgumentException $ex) { - $this->putError(strtr(':command: Invalid argument `:argument` for option `:option`', array( + $this->throwError(strtr(':command: Invalid argument `:argument` for option `:option`', array( ':command' => sprintf('%s %s', $this->dispatcher->getParam('activeTask'), $this->dispatcher->getParam('activeAction')), ':option' => $ex->getOption(), ':argument' => $ex->getArgument() @@ -113,7 +113,7 @@ public function beforeExecuteRoute() $this->renderActionHelp(); } catch (InvalidOptionException $ex) { - $this->putError(strtr(':command: Invalid option `:option`', array( + $this->throwError(strtr(':command: Invalid option `:option`', array( ':command' => sprintf('%s %s', $this->dispatcher->getParam('activeTask'), $this->dispatcher->getParam('activeAction')), ':option' => $ex->getOption() ))); @@ -127,7 +127,7 @@ public function beforeExecuteRoute() /** * Action executed by default when no action was specified in execution */ - final public function mainAction() + public function mainAction() { $this->renderTaskHelp(); } @@ -137,11 +137,11 @@ final public function mainAction() * * @param $args * @return bool - * @internal */ private function containHelpOption($args) { - return array_key_exists(self::HELP_OPTION, $args) || array_key_exists(self::HELP_SHORTOPTION, $args); + return array_key_exists(self::HELP_OPTION, $args) + || array_key_exists(self::HELP_SHORTOPTION, $args); } /** @@ -149,7 +149,6 @@ private function containHelpOption($args) * * @param $str * @return $this - * @internal */ private function appendLine($str) { @@ -171,7 +170,7 @@ protected function getArgs() * Return argument from indicated index * * @param $index - * @return null + * @return null|mixed */ protected function getArg($index) { @@ -245,7 +244,7 @@ public function putText($str) } /** - * Puts warning into buffer + * Puts warning message into buffer * * @param $str */ @@ -255,7 +254,7 @@ public function putWarn($str) } /** - * Puts success into buffer + * Puts success message into buffer * * @param $str */ @@ -265,14 +264,13 @@ public function putSuccess($str) } /** - * Puts error into buffer + * Puts error message into buffer * * @param $str - * @throws \Exception */ public function putError($str) { - throw new \Exception($this->getColoredString($str, 'red', 'dark_gray')); + $this->appendLine($this->getColoredString($str, 'red')); } /** @@ -285,6 +283,17 @@ public function putObject($object) $this->appendLine($this->getColoredString(print_r($object, true), 'black', 'light_gray')); } + /** + * Throws CLI error + * + * @param $str + * @throws \Exception + */ + public function throwError($str) + { + throw new \Exception($this->getColoredString($str, 'red', 'dark_gray')); + } + /** * Renders help for task action */ diff --git a/src/Http/Response/Json.php b/src/Http/Response/Json.php deleted file mode 100644 index dec7bf2..0000000 --- a/src/Http/Response/Json.php +++ /dev/null @@ -1,104 +0,0 @@ - - * @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\Http\Response; - -/** - * Class Json - * @package Vegas\Http\Response - */ -class Json implements \JsonSerializable -{ - /** - * Determines if request was succeed or failed - * - * @var bool - * @internal - */ - private $isSuccess = false; - - /** - * Contains data - * - * @var string - * @internal - */ - private $data = ''; - - /** - * Response message - * - * @var string - * @internal - */ - private $message = ''; - - /** - * Determines if request was succeed - * - * @return $this - */ - public function success() - { - $this->isSuccess = true; - return $this; - } - - /** - * Determines if request was failed - * - * @return $this - */ - public function fail() - { - $this->isSuccess = false; - return $this; - } - - /** - * Sets request response message - * - * @param $message - * @return $this - */ - public function setMessage($message) - { - $this->message = $message; - return $this; - } - - /** - * Sets request data - * - * @param $data - * @return $this - */ - public function setData($data) - { - $this->data = $data; - return $this; - } - - /** - * Serializes array to JSON format - * - * @return array|mixed - */ - public function jsonSerialize() - { - return array( - 'success' => $this->isSuccess, - 'data' => $this->data, - 'message' => $this->message - ); - } -} \ No newline at end of file diff --git a/src/Mvc/ControllerAbstract.php b/src/Mvc/ControllerAbstract.php index 07325e5..e5cc07f 100644 --- a/src/Mvc/ControllerAbstract.php +++ b/src/Mvc/ControllerAbstract.php @@ -11,6 +11,7 @@ */ namespace Vegas\Mvc; +use Phalcon\Http\ResponseInterface; use Phalcon\Mvc\Controller; use Vegas\Exception; use Vegas\Mvc\View; @@ -42,7 +43,7 @@ public function initialize() * Renders JSON response * Disables view * - * @param \Vegas\Http\Response\Json|array $data + * @param array|\Phalcon\Http\ResponseInterface $data * @return null|\Phalcon\Http\ResponseInterface */ protected function jsonResponse($data = array()) diff --git a/src/Task/AssetsTask.php b/src/Task/AssetsTask.php index 2a7db32..5e41c55 100644 --- a/src/Task/AssetsTask.php +++ b/src/Task/AssetsTask.php @@ -15,12 +15,13 @@ use Vegas\Cli\Task\Action; use Vegas\Cli\Task\Option; use Vegas\Cli\Task; +use Vegas\Cli\TaskAbstract; /** * Class AssetsTask * @package Vegas\Task */ -class AssetsTask extends Task +class AssetsTask extends TaskAbstract { /** * Publishes assets provided by vegas-libraries installed via composer @@ -111,7 +112,7 @@ private function copyRecursive($source, $dest, $permissions = 0755) * * @return mixed */ - public function setOptions() + public function setupOptions() { $action = new Action('publish', 'Publish all assets'); diff --git a/src/Task/CacheTask.php b/src/Task/CacheTask.php index 636b7b3..714e106 100644 --- a/src/Task/CacheTask.php +++ b/src/Task/CacheTask.php @@ -15,12 +15,13 @@ use Phalcon\DI; use Vegas\Cli\Task\Action; use Vegas\Cli\Task; +use Vegas\Cli\TaskAbstract; /** * Class CacheTask * @package Vegas\Task */ -class CacheTask extends Task +class CacheTask extends TaskAbstract { /** * Cleans application cache @@ -69,7 +70,7 @@ private function removeFilesFromDir($dir) * * @return mixed */ - public function setOptions() + public function setupOptions() { $action = new Action('clean', 'Clean cache'); $this->addTaskAction($action); diff --git a/tests/App/TestCase.php b/tests/App/TestCase.php deleted file mode 100644 index f331144..0000000 --- a/tests/App/TestCase.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @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\App; - -use Phalcon\DI; -use Vegas\Mvc\Application; -use Vegas\Mvc\Controller\Crud; -use Vegas\Mvc\Module\Loader as ModuleLoader; - -class TestCase extends \PHPUnit_Framework_TestCase -{ - protected $bootstrap; - protected $di; - - public function setUp() - { - $_SERVER['HTTP_HOST'] = 'vegas.dev'; - $_SERVER['REQUEST_URI'] = '/'; - - $this->di = DI::getDefault(); - $modules = ModuleLoader::dump($this->di); - - $app = new Application(); - $app->registerModules($modules); - - require_once TESTS_ROOT_DIR . '/fixtures/app/Bootstrap.php'; - $config = require TESTS_ROOT_DIR . '/fixtures/app/config/config.php'; - - $config = new \Phalcon\Config($config); - $bootstrap = new \Bootstrap($config); - $bootstrap->setup(); - - $this->bootstrap = $bootstrap; - } -} diff --git a/tests/Bootstrap/EnvironmentInitializerTraitTest.php b/tests/Bootstrap/EnvironmentInitializerTraitTest.php new file mode 100644 index 0000000..d0dd805 --- /dev/null +++ b/tests/Bootstrap/EnvironmentInitializerTraitTest.php @@ -0,0 +1,68 @@ + + * @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\Bootstrap; + +class EnvironmentInitializerTraitTest extends \PHPUnit_Framework_TestCase +{ + + public function testShouldSetEnvFromConfig() + { + $config = [ + 'application' => [ + 'environment' => \Vegas\Constants::DEV_ENV + ] + ]; + + $di = \Phalcon\DI::getDefault(); + + $trait = $this->getMockForTrait('\Vegas\Bootstrap\EnvironmentInitializerTrait'); + $trait->expects($this->any()) + ->method('getDI') + ->will($this->returnValue($di)); + + $trait->initEnvironment(new \Phalcon\Config($config)); + + $this->assertEquals($di->get('environment'), $config['application']['environment']); + } + + public function testShouldSetDefaultEnv() + { + $config = []; + + $di = \Phalcon\DI::getDefault(); + + $trait = $this->getMockForTrait('\Vegas\Bootstrap\EnvironmentInitializerTrait'); + $trait->expects($this->any()) + ->method('getDI') + ->will($this->returnValue($di)); + + $trait->initEnvironment(new \Phalcon\Config($config)); + + $this->assertEquals($di->get('environment'), \Vegas\Constants::DEFAULT_ENV); + } + + public function testShouldSetDefineValue() + { + $di = \Phalcon\DI::getDefault(); + + $trait = $this->getMockForTrait('\Vegas\Bootstrap\EnvironmentInitializerTrait'); + $trait->expects($this->any()) + ->method('getDI') + ->will($this->returnValue($di)); + + $trait->initEnvironment(new \Phalcon\Config([])); + + $this->assertTrue(defined('APPLICATION_ENV')); + } +} + \ No newline at end of file diff --git a/tests/Http/Response/JsonTest.php b/tests/Http/Response/JsonTest.php deleted file mode 100644 index 18b20dc..0000000 --- a/tests/Http/Response/JsonTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @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\Http\Response; - -use Vegas\Http\Response\Json; - -class JsonTest extends \PHPUnit_Framework_TestCase -{ - - public function testResponseJsonSerialize() - { - $response = new Json(); - $response->success()->setData(array('test' => 1))->setMessage('Test message'); - $responseContentJson = json_encode($response); - - $testResponseArray = array( - 'success' => true, - 'data' => array( - 'test' => 1 - ), - 'message' => 'Test message' - ); - $testResponseJson = json_encode($testResponseArray); - $this->assertEquals($testResponseJson, $responseContentJson); - - $response->fail(); - $responseContentJson = json_encode($response); - $testResponseArray['success'] = false; - $testResponseJson = json_encode($testResponseArray); - $this->assertEquals($testResponseJson, $responseContentJson); - } -} \ No newline at end of file diff --git a/tests/fixtures/app/modules/Foo/tasks/FooTask.php b/tests/fixtures/app/modules/Foo/tasks/FooTask.php index 68582e7..eb225e6 100644 --- a/tests/fixtures/app/modules/Foo/tasks/FooTask.php +++ b/tests/fixtures/app/modules/Foo/tasks/FooTask.php @@ -24,7 +24,7 @@ public function testAction() * * @return mixed */ - public function setOptions() + public function setupOptions() { // TODO: Implement setOptions() method. } diff --git a/tests/fixtures/app/modules/Test/tasks/FooTask.php b/tests/fixtures/app/modules/Test/tasks/FooTask.php index 5b7cf6c..9f1e6ae 100644 --- a/tests/fixtures/app/modules/Test/tasks/FooTask.php +++ b/tests/fixtures/app/modules/Test/tasks/FooTask.php @@ -12,7 +12,7 @@ namespace Test\Tasks; -class FooTask extends \Vegas\Cli\Task +class FooTask extends \Vegas\Cli\TaskAbstract { public function testAction() { @@ -24,7 +24,7 @@ public function testAction() * * @return mixed */ - public function setOptions() + public function setupOptions() { // TODO: Implement setOptions() method. } diff --git a/tests/fixtures/app/tasks/CustomTask.php b/tests/fixtures/app/tasks/CustomTask.php index aa7b01e..5faded4 100644 --- a/tests/fixtures/app/tasks/CustomTask.php +++ b/tests/fixtures/app/tasks/CustomTask.php @@ -12,10 +12,10 @@ use Vegas\Cli\Task\Option; -class CustomTask extends \Vegas\Cli\Task +class CustomTask extends \Vegas\Cli\TaskAbstract { - public function setOptions() + public function setupOptions() { $action = new \Vegas\Cli\Task\Action('test', 'Test action'); From b30455073f7eed89c4217bb04d7ecef399188c52 Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 17 Dec 2014 13:10:56 +0100 Subject: [PATCH 33/35] Cli refactoring + tests; Added DateTime util; Rewritten DateTime db mapping; --- src/Cli/Bootstrap.php | 8 +- src/Cli/EventsListener/TaskListener.php | 2 +- src/Cli/Loader.php | 11 +- src/Cli/Task/Option.php | 3 +- src/Cli/TaskAbstract.php | 67 +++---- src/Db/Mapping/DateTime.php | 11 +- src/Db/MappingManager.php | 2 +- src/Mvc/Bootstrap.php | 2 +- src/Task/AssetsTask.php | 2 +- src/Task/CacheTask.php | 2 +- src/Test/Bootstrap.php | 4 +- src/Util/DateTime.php | 83 ++++++++- tests/Cli/BootstrapTest.php | 174 ++++++++++++++++-- tests/Cli/TestCase.php | 19 ++ tests/DI/Scaffolding/Adapter/MongoTest.php | 17 ++ tests/Db/MappingTest.php | 38 ++++ tests/Mvc/View/Engine/VoltTest.php | 3 +- tests/Util/DateTimeTest.php | 45 ++++- tests/fixtures/app/tasks/CustomTask.php | 43 +++++ .../lib/Vegas/Fake/Nested/Task/FakeTask.php | 2 +- .../fixtures/lib/Vegas/Fake/Task/FakeTask.php | 22 ++- 21 files changed, 464 insertions(+), 96 deletions(-) diff --git a/src/Cli/Bootstrap.php b/src/Cli/Bootstrap.php index 16c087b..dbf6707 100644 --- a/src/Cli/Bootstrap.php +++ b/src/Cli/Bootstrap.php @@ -94,7 +94,7 @@ public function getApplication() /** * Initializes application modules */ - protected function initModules(Config $config) + public function initModules(Config $config) { $this->baseInitModule($config); @@ -125,7 +125,7 @@ public function setArguments($args) /** * Setups CLI events manager */ - protected function initEventsManager() + public function initEventsManager() { $taskListener = new TaskListener(); @@ -163,8 +163,8 @@ public function setup() */ public function run() { - $argumentParser = new Loader(); - $arguments = $argumentParser->parseArguments($this->application, $this->arguments); + $taskLoader = new Loader(); + $arguments = $taskLoader->parseArguments($this->application, $this->arguments); $this->application->handle($arguments); } diff --git a/src/Cli/EventsListener/TaskListener.php b/src/Cli/EventsListener/TaskListener.php index 990b91c..7ee1226 100644 --- a/src/Cli/EventsListener/TaskListener.php +++ b/src/Cli/EventsListener/TaskListener.php @@ -40,7 +40,7 @@ public function beforeHandleTask($argv) $dispatcher->setParams(array( 'activeTask' => isset($parsedOptions[0]) ? $parsedOptions[0] : false, 'activeAction' => isset($parsedOptions[1]) ? $parsedOptions[1] : false, - 'args' => count($parsedOptions) > 2 ? array_slice($parsedOptions, 2) : array() + 'args' => count($parsedOptions) > 2 ? array_slice($parsedOptions, 2) : [] )); }; } diff --git a/src/Cli/Loader.php b/src/Cli/Loader.php index b08a3ed..3e0f729 100644 --- a/src/Cli/Loader.php +++ b/src/Cli/Loader.php @@ -104,15 +104,12 @@ protected function lookupTaskClass($arguments) * @internal */ private function toNamespace($str) { - $string_parts = preg_split('/_+/', $str); + $stringParts = preg_split('/_+/', $str); - if (!is_array($string_parts) || (sizeof($string_parts) < 1)){ - throw new CliException("Unable to split the input string"); + foreach($stringParts as $key => $stringPart){ + $stringParts[$key] = ucfirst(strtolower($stringPart)); } - foreach($string_parts as $key => $string_part){ - $string_parts[$key] = ucfirst(strtolower($string_part)); - } - return implode('\\', $string_parts) . '\\'; + return implode('\\', $stringParts) . '\\'; } /** diff --git a/src/Cli/Task/Option.php b/src/Cli/Task/Option.php index d579a8a..5e966d3 100644 --- a/src/Cli/Task/Option.php +++ b/src/Cli/Task/Option.php @@ -115,7 +115,8 @@ public function setValidator(callable $validator) */ public function matchParam($paramName) { - return (0 == strcasecmp($this->name, $paramName)) || (0 == strcasecmp($this->shortName, $paramName)); + return (0 == strcasecmp($this->name, $paramName)) + || (0 == strcasecmp($this->shortName, $paramName)); } /** diff --git a/src/Cli/TaskAbstract.php b/src/Cli/TaskAbstract.php index 9956ec2..52d015b 100644 --- a/src/Cli/TaskAbstract.php +++ b/src/Cli/TaskAbstract.php @@ -15,8 +15,7 @@ use Vegas\Cli\Task\Action; use Vegas\Cli\Task\Exception\InvalidArgumentException; use Vegas\Cli\Task\Exception\InvalidOptionException; -use Vegas\Cli\Task\Exception\MissingArgumentException; -use Vegas\Cli\Task\Option; +use Vegas\Cli\Task\Exception\MissingRequiredArgumentException; /** * Class Task @@ -110,22 +109,18 @@ public function beforeExecuteRoute() ':option' => $ex->getOption(), ':argument' => $ex->getArgument() ))); - - $this->renderActionHelp(); } catch (InvalidOptionException $ex) { $this->throwError(strtr(':command: Invalid option `:option`', array( ':command' => sprintf('%s %s', $this->dispatcher->getParam('activeTask'), $this->dispatcher->getParam('activeAction')), ':option' => $ex->getOption() ))); - - $this->renderActionHelp(); } return true; } /** - * Action executed by default when no action was specified in execution + * Action executed by default when no action was specified in command line */ public function mainAction() { @@ -187,7 +182,7 @@ protected function getArg($index) * * @param $name * @param null $default - * @throws Task\Exception\MissingArgumentException + * @throws MissingRequiredArgumentException * @return mixed */ protected function getOption($name, $default = null) @@ -198,15 +193,7 @@ protected function getOption($name, $default = null) $matchedOption = $option; } } - if ($matchedOption instanceof Option) { - $value = $matchedOption->getValue($this->args, $default); - - if ($matchedOption->isRequired() && empty($value)) { - throw new MissingArgumentException($name); - } - } else { - throw new MissingArgumentException($name); - } + $value = $matchedOption->getValue($this->args, $default); return $value; } @@ -248,7 +235,7 @@ public function putText($str) * * @param $str */ - public function putWarn($str) + public function putWarning($str) { $this->appendLine($this->getColoredString($str, 'yellow')); } @@ -299,31 +286,27 @@ public function throwError($str) */ protected function renderActionHelp() { - if (!isset($this->actions[$this->actionName])) { - $this->appendLine('No help available'); - } else { - $action = $this->actions[$this->actionName]; - //puts name of action - $this->appendLine($this->getColoredString($action->getDescription(), 'green')); - $this->appendLine(''); - - //puts usage hint - $this->appendLine('Usage:'); + $action = $this->actions[$this->actionName]; + //puts name of action + $this->appendLine($this->getColoredString($action->getDescription(), 'green')); + $this->appendLine(''); + + //puts usage hint + $this->appendLine('Usage:'); + $this->appendLine($this->getColoredString(sprintf( + ' %s %s [options]', + $this->dispatcher->getParam('activeTask'), + $this->dispatcher->getParam('activeAction') + ), 'dark_gray')); + $this->appendLine(''); + + //puts available options + $this->appendLine($this->getColoredString('Options:', 'gray')); + foreach ($action->getOptions() as $option) { $this->appendLine($this->getColoredString(sprintf( - ' %s %s [options]', - $this->dispatcher->getParam('activeTask'), - $this->dispatcher->getParam('activeAction') - ), 'dark_gray')); - $this->appendLine(''); - - //puts available options - $this->appendLine($this->getColoredString('Options:', 'gray')); - foreach ($action->getOptions() as $option) { - $this->appendLine($this->getColoredString(sprintf( - ' --%s -%s %s', - $option->getName(), $option->getShortName(), $option->getDescription() - ), 'light_green')); - } + ' --%s -%s %s', + $option->getName(), $option->getShortName(), $option->getDescription() + ), 'light_green')); } } diff --git a/src/Db/Mapping/DateTime.php b/src/Db/Mapping/DateTime.php index 9bae3e1..9e5781d 100644 --- a/src/Db/Mapping/DateTime.php +++ b/src/Db/Mapping/DateTime.php @@ -17,7 +17,7 @@ /** * Class DateTime * - * Simple mapper for decoding JSON value + * Simple mapper for DateTime object * * @package Vegas\Db\Mapping */ @@ -36,10 +36,13 @@ public function getName() */ public function resolve(& $value) { - if (is_integer($value) && strlen($value) > 0) { - $dateTime = new \DateTime(); + if (is_numeric($value) && strlen($value) > 0) { + $dateTime = new \Vegas\Util\DateTime(); $dateTime->setTimestamp($value); - $value = $dateTime->format('Y-m-d H:i:s'); + $value = $dateTime; + } else if (\Vegas\Util\DateTime::isValid($value)) { + $dateTime = new \Vegas\Util\DateTime($value); + $value = $dateTime; } return $value; diff --git a/src/Db/MappingManager.php b/src/Db/MappingManager.php index 41d846a..3c11810 100644 --- a/src/Db/MappingManager.php +++ b/src/Db/MappingManager.php @@ -32,7 +32,7 @@ class MappingManager /** * Adds new mapping class * - * @param $mappingClass Object or full class name is acceptable + * @param mixed $mappingClass Object or full class name is acceptable * @return $this * @throws Exception\InvalidMappingClassException */ diff --git a/src/Mvc/Bootstrap.php b/src/Mvc/Bootstrap.php index e5b17b4..c199cf2 100644 --- a/src/Mvc/Bootstrap.php +++ b/src/Mvc/Bootstrap.php @@ -104,7 +104,7 @@ public function getApplication() /** * Registers default dispatcher */ - protected function initDispatcher() + public function initDispatcher() { $this->di->set('dispatcher', function() { $dispatcher = new Dispatcher(); diff --git a/src/Task/AssetsTask.php b/src/Task/AssetsTask.php index 5e41c55..1f8b204 100644 --- a/src/Task/AssetsTask.php +++ b/src/Task/AssetsTask.php @@ -78,7 +78,7 @@ private function copyRecursive($source, $dest, $permissions = 0755) // Simple copy for a file if (is_file($source)) { if (is_file($dest)) { - $this->putWarn("Cannot copy $source. File already exists."); + $this->putWarning("Cannot copy $source. File already exists."); return false; } else { return copy($source, $dest); diff --git a/src/Task/CacheTask.php b/src/Task/CacheTask.php index 714e106..ba7e0b7 100644 --- a/src/Task/CacheTask.php +++ b/src/Task/CacheTask.php @@ -58,7 +58,7 @@ private function removeFilesFromDir($dir) } if (!unlink($dir.DIRECTORY_SEPARATOR.$entry)) { - $this->putWarn("Can not remove: ".$dir.DIRECTORY_SEPARATOR.$entry); + $this->putWarning("Can not remove: ".$dir.DIRECTORY_SEPARATOR.$entry); } } closedir($handle); diff --git a/src/Test/Bootstrap.php b/src/Test/Bootstrap.php index 646b9f4..d535263 100644 --- a/src/Test/Bootstrap.php +++ b/src/Test/Bootstrap.php @@ -124,7 +124,7 @@ protected function initEnvironment(Config $config) /** * Registers default dispatcher */ - protected function initDispatcher() + public function initDispatcher() { $this->di->set('dispatcher', function() { $dispatcher = new Dispatcher(); @@ -147,7 +147,7 @@ protected function initDispatcher() /** * */ - protected function initRequest() + public function initRequest() { $this->getDI()->set('request', function() { return new Request(); diff --git a/src/Util/DateTime.php b/src/Util/DateTime.php index b06a03a..f87ef9e 100644 --- a/src/Util/DateTime.php +++ b/src/Util/DateTime.php @@ -9,12 +9,87 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace Vegas\Util; - -class DateTime +class DateTime extends \DateTime implements \JsonSerializable { + /** + * Default format used for the whole class + * @var string + */ + public static $globalDefaultFormat = 'Y-m-d H:i:s'; + + /** + * Default format used for this object + * @var string + */ + private $defaultFormat; + + /** + * Prints the object as string using default format available. + * @return string + */ + public function __toString() + { + if (is_string($this->defaultFormat)) { + return $this->format($this->defaultFormat); + } else if (is_string(self::$globalDefaultFormat)) { + return $this->format(self::$globalDefaultFormat); + } + return ''; + } + + /** + * Sets default format + * + * @param string $defaultFormat + * @return $this + */ + public function setDefaultFormat($defaultFormat) + { + $this->defaultFormat = $defaultFormat; + return $this; + } + + /** + * Default \DateTime method creates parent classtype object. + * This implementation overrides this behavior. + * {@inheritdoc} + */ + public static function createFromFormat($format, $time, $object = NULL) + { + $date = $object ? \DateTime::createFromFormat($format, $time, $object) + : \DateTime::createFromFormat($format, $time); + + return $date !== false ? + new self($date->format('Y-m-d H:i:s'), $date->getTimezone()) + : false; + } + + /** + * @return mixed|string + */ + public function jsonSerialize() + { + return $this->format(self::ISO8601); + } -} \ No newline at end of file + /** + * Validates date + * + * @param mixed $value + * @return bool + */ + public static function isValid($value) + { + if ($value instanceof \DateTime) + return true; + try { + new static($value); + return true; + } catch (\Exception $e) { + return false; + } + } +} \ No newline at end of file diff --git a/tests/Cli/BootstrapTest.php b/tests/Cli/BootstrapTest.php index 514737a..5622db2 100644 --- a/tests/Cli/BootstrapTest.php +++ b/tests/Cli/BootstrapTest.php @@ -105,7 +105,7 @@ public function testShouldThrowExceptionAboutMissingArguments() } } - public function testShouldThrowExeptionAboutInvalidArgument() + public function testShouldThrowExceptionAboutInvalidArgument() { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( @@ -154,17 +154,33 @@ public function testShouldReturnTaskHelp() ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals('G1swOzMybVRlc3QgYWN0aW9uG1swbQoKVXNhZ2U6ChtbMTszMG0gICBhcHA6Y3VzdG9tIHRlc3QgW29wdGlvbnNdG1swbQoKT3B0aW9uczobWzBtChtbMTszMm0gICAtLWZvbyAgICAgLWYgICAgICBGb28gb3B0aW9uLiBVc2FnZSBhcHA6Y3VzdG9tIHRlc3QgLWYgbnVtYmVyT2ZTdGgbWzBtCg==', $returnedValue); + $this->assertContains('Usage', $returnedValue); + $this->assertContains('Options', $returnedValue); } - public function testShouldExecuteApplicationTask() + public function testShouldReturnTaskHelpWhenNoActionSpecified() { - $correctBase = 'G1sxOzM0bRtbMG0KG1sxOzM0bTEyMxtbMG0K'; + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'app:custom' + )); + + ob_start(); + + $cli->setup()->run(); + $returnedValue = ob_get_contents(); + ob_end_clean(); + + $this->assertContains('Available actions', $returnedValue); + } + public function testShouldExecuteApplicationTask() + { $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( 0 => 'cli/cli.php', @@ -177,29 +193,29 @@ public function testShouldExecuteApplicationTask() ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals($correctBase, $returnedValue); + $this->assertContains('123', $returnedValue); $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( 0 => 'cli/cli.php', 1 => 'app:custom', 2 => 'test', - 3 => '--f', + 3 => '--foo', 4 => 123 )); ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals($correctBase, $returnedValue); + $this->assertContains('123', $returnedValue); $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( @@ -212,28 +228,45 @@ public function testShouldExecuteApplicationTask() ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals($correctBase, $returnedValue); + $this->assertContains('123', $returnedValue); $cli = new Bootstrap($this->di->get('config')); $cli->setArguments(array( 0 => 'cli/cli.php', 1 => 'app:custom', 2 => 'test', - 3 => '--f=123' + 3 => '--foo=123' + )); + + ob_start(); + + $cli->setup()->run(); + $returnedValue = ob_get_contents(); + + ob_end_clean(); + + $this->assertContains('123', $returnedValue); + + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'app:custom', + 2 => 'testArg', + 3 => '999' )); ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals($correctBase, $returnedValue); + $this->assertContains('999', $returnedValue); } public function testShouldExecuteModuleTask() @@ -248,10 +281,115 @@ public function testShouldExecuteModuleTask() ob_start(); $cli->setup()->run(); - $returnedValue = base64_encode(ob_get_contents()); + $returnedValue = ob_get_contents(); + + ob_end_clean(); + + $this->assertContains('FOO', $returnedValue); + } + + public function testShouldReturnTextInResponse() + { + $colorsOutput = $this->getMockForTrait('\Vegas\Cli\ColorsOutputTrait'); + + $runTask = function($action) { + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'app:custom', + 2 => $action + )); + + ob_start(); + $cli->setup()->run(); + $returnedValue = ob_get_contents(); + + ob_end_clean(); + + return $returnedValue; + }; + $this->assertContains($colorsOutput->getColoredString('Error message', 'red'), $runTask('testError')); + $this->assertContains($colorsOutput->getColoredString('Warning message', 'yellow'), $runTask('testWarning')); + $this->assertContains($colorsOutput->getColoredString('Success message', 'green'), $runTask('testSuccess')); + $this->assertContains($colorsOutput->getColoredString('Some text', 'light_blue'), $runTask('testText')); + $this->assertContains(print_r(['key' => 'value'], true), $runTask('testObject')); + } + + public function testShouldLoadLibraryTask() + { + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'vegas:cache', + 2 => 'clean' + )); + + ob_start(); + $cli->setup()->run(); + $returnedValue = ob_get_contents(); + + ob_end_clean(); + + $this->assertContains('Cleaning cache', $returnedValue); + + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'vegas:fake:fake', + 2 => 'test' + )); + + ob_start(); + $cli->setup()->run(); + $returnedValue = ob_get_contents(); + + ob_end_clean(); + + $this->assertContains('Vegas\Fake\Task\FakeTask', $returnedValue); + + $cli = new Bootstrap($this->di->get('config')); + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'vegas:fake_nested:fake', + 2 => 'test' + )); + + ob_start(); + $cli->setup()->run(); + $returnedValue = ob_get_contents(); ob_end_clean(); - $this->assertEquals('Rk9P', $returnedValue); + $this->assertContains('Vegas\Fake\Nested\Task\FakeTask', $returnedValue); + } + + public function testShouldThrowExceptionAboutNotExistingModuleTask() + { + $cli = new Bootstrap($this->di->get('config')); + $cli->setup(); + + //remove Test module + $application = $cli->getApplication(); + $modules = $application->getModules(); + + $modulesWithoutTest = array_merge([], $modules); + unset($modulesWithoutTest['Test']); + $application->registerModules($modulesWithoutTest); + + $cli->setArguments(array( + 0 => 'cli/cli.php', + 1 => 'app:test:foo', + 2 => 'test' + )); + + $exception = null; + try { + $cli->run(); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('Vegas\Cli\Exception\TaskNotFoundException', $exception); + + $application->registerModules($modules); } -} +} \ No newline at end of file diff --git a/tests/Cli/TestCase.php b/tests/Cli/TestCase.php index 647a5be..97db69e 100644 --- a/tests/Cli/TestCase.php +++ b/tests/Cli/TestCase.php @@ -26,6 +26,9 @@ class TestCase extends \PHPUnit_Framework_TestCase */ protected $di; + /** + * + */ public function setUp() { $this->di = DI::getDefault(); @@ -53,4 +56,20 @@ protected function runCliAction($command) return $result; } + + /** + * @return Bootstrap + */ + public function getBootstrap() + { + return $this->bootstrap; + } + + /** + * @return DI + */ + public function getDI() + { + return $this->di; + } } diff --git a/tests/DI/Scaffolding/Adapter/MongoTest.php b/tests/DI/Scaffolding/Adapter/MongoTest.php index 3af9f48..e50ecf8 100644 --- a/tests/DI/Scaffolding/Adapter/MongoTest.php +++ b/tests/DI/Scaffolding/Adapter/MongoTest.php @@ -26,5 +26,22 @@ public function testShouldSetupOwnCollectionManager() $this->getDI()->set('collectionManager', $collectionManager, true); } + + public function testShouldThrowExceptionAboutMissingRequiredService() + { + $mongo = $this->getDI()->get('mongo'); + + $this->getDI()->remove('mongo'); + + $exception = null; + try { + new \Vegas\DI\Scaffolding\Adapter\Mongo(); + } catch (\Exception $e) { + $exception = $e; + } + $this->assertInstanceOf('\Vegas\Db\Exception\NoRequiredServiceException', $exception); + + $this->getDI()->set('mongo', $mongo); + } } \ No newline at end of file diff --git a/tests/Db/MappingTest.php b/tests/Db/MappingTest.php index d24275f..1fe8326 100644 --- a/tests/Db/MappingTest.php +++ b/tests/Db/MappingTest.php @@ -20,6 +20,7 @@ use Vegas\Db\Mapping\Json; use Vegas\Db\MappingInterface; use Vegas\Db\MappingManager; +use Vegas\Util\DateTime; class Fake extends CollectionAbstract { @@ -50,6 +51,18 @@ public function getSource() ); } +class FakeDate extends CollectionAbstract +{ + public function getSource() + { + return 'fake_date'; + } + + protected $mappings = [ + 'createdAt' => 'dateTime' + ]; +} + class UpperCase implements MappingInterface { /** @@ -299,4 +312,29 @@ public function testShouldResolveModelMappings() $fakeRecord->clearMappings(); $this->assertEquals($nonCamelText, $fakeRecord->readMapped('somecamel')); } + + public function testShouldResolveDateTime() + { + $mappingManager = new MappingManager(); + $mappingManager->add(new \Vegas\Db\Mapping\DateTime()); + + $now = new \DateTime('now'); + + $fake = new FakeDate(); + $fake->createdAt = time(); + + $this->assertEquals($now->format(DateTime::$globalDefaultFormat), $fake->readMapped('createdAt')); + $this->assertInstanceOf('\Vegas\Util\DateTime', $fake->readMapped('createdAt')); + $this->assertSame($fake->createdAt, (int)$fake->readMapped('createdAt')->format('U')); + + $fake->createdAt = $now->format('m/d/Y'); + $this->assertInstanceOf('\Vegas\Util\DateTime', $fake->readMapped('createdAt')); + $this->assertEquals($now->format('m/d/Y'), $fake->readMapped('createdAt')->format('m/d/Y')); + $this->assertSame($fake->createdAt, $fake->readMapped('createdAt')->format('m/d/Y')); + + $fake->createdAt = $now->format('d/m/Y'); + $this->assertNotInstanceOf('\Vegas\Util\DateTime', $fake->readMapped('createdAt')); + $this->assertEquals($now->format('d/m/Y'), $fake->readMapped('createdAt')); + $this->assertInternalType('string', $fake->readMapped('createdAt')); + } } \ No newline at end of file diff --git a/tests/Mvc/View/Engine/VoltTest.php b/tests/Mvc/View/Engine/VoltTest.php index fc28e40..a2fde1a 100644 --- a/tests/Mvc/View/Engine/VoltTest.php +++ b/tests/Mvc/View/Engine/VoltTest.php @@ -9,11 +9,12 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace Vegas\Tests\Mvc\View\Engine; use Phalcon\DI; use Vegas\Mvc\View; -use Vegas\Tests\App\TestCase; +use Vegas\Test\TestCase; class VoltTest extends TestCase { diff --git a/tests/Util/DateTimeTest.php b/tests/Util/DateTimeTest.php index 424987c..f029ca6 100644 --- a/tests/Util/DateTimeTest.php +++ b/tests/Util/DateTimeTest.php @@ -9,12 +9,49 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace Vegas\Tests\Util; - -class DateTimeTest +class DateTimeTest extends \PHPUnit_Framework_TestCase { -} \ No newline at end of file + public function testShouldCreatedValidDateTimeObjectFromDateString() + { + $now = new \DateTime('now'); + $dateTime = \Vegas\Util\DateTime::createFromFormat('Y-m-d H:i:s', $now->format('Y-m-d H:i:s')); + $this->assertInstanceOf('\DateTime', $dateTime); + + $this->assertEquals($now->format(\Vegas\Util\DateTime::$globalDefaultFormat), (string) $dateTime); + \Vegas\Util\DateTime::$globalDefaultFormat = 'Y-m-d H:i'; + $this->assertEquals($now->format(\Vegas\Util\DateTime::$globalDefaultFormat), (string) $dateTime); + $dateTime->setDefaultFormat('Y-m-d'); + $this->assertNotEquals($now->format(\Vegas\Util\DateTime::$globalDefaultFormat), (string) $dateTime); + + $dateTime->setDefaultFormat(false); + \Vegas\Util\DateTime::$globalDefaultFormat = false; + $this->assertEmpty((string)$dateTime); + } + + public function testShouldNotCreateValidDateTimeFromInvalidDateString() + { + $dateTime = \Vegas\Util\DateTime::createFromFormat('Y-m-d H:i:s', 'Invalid date'); + $this->assertFalse($dateTime); + } + + public function testShouldValidateGivenDateString() + { + $this->assertFalse(\Vegas\Util\DateTime::isValid('Invalid date')); + $this->assertFalse(\Vegas\Util\DateTime::isValid(time())); + $this->assertFalse(\Vegas\Util\DateTime::isValid((new \DateTime())->format('d/m/Y'))); + $this->assertTrue(\Vegas\Util\DateTime::isValid((new \DateTime())->format('Y-m-d H:i:s'))); + $this->assertTrue(\Vegas\Util\DateTime::isValid((new \DateTime())->format('m/d/Y'))); + } + + public function testShouldSerializeDateTimeObjectToJson() + { + $now = new \DateTime('now'); + $dateTime = \Vegas\Util\DateTime::createFromFormat('Y-m-d H:i:s', $now->format('Y-m-d H:i:s')); + + $this->assertEquals(json_encode($now->format(\DateTime::ISO8601)), json_encode($dateTime)); + } +} \ No newline at end of file diff --git a/tests/fixtures/app/tasks/CustomTask.php b/tests/fixtures/app/tasks/CustomTask.php index 5faded4..6b804a5 100644 --- a/tests/fixtures/app/tasks/CustomTask.php +++ b/tests/fixtures/app/tasks/CustomTask.php @@ -27,6 +27,18 @@ public function setupOptions() }); $foo->setRequired(true); $action->addOption($foo); + + $this->addTaskAction($action); + + $this->addTaskAction(new \Vegas\Cli\Task\Action('testError', 'Test error')); + $this->addTaskAction(new \Vegas\Cli\Task\Action('testWarning', 'Test warning')); + $this->addTaskAction(new \Vegas\Cli\Task\Action('testSuccess', 'Test success')); + $this->addTaskAction(new \Vegas\Cli\Task\Action('testObject', 'Test object')); + $this->addTaskAction(new \Vegas\Cli\Task\Action('testText', 'Test text')); + + $action = new \Vegas\Cli\Task\Action('testArg', 'Test arguments list'); + $option = new Option('arg', 'a', 'Arg option. Usage app:custom:test 999'); + $action->addOption($option); $this->addTaskAction($action); } @@ -34,5 +46,36 @@ public function testAction() { $this->putText($this->getArg(0)); $this->putText($this->getOption('f')); + $this->putObject($this->getArgs()); + } + + public function testErrorAction() + { + $this->putError('Error message'); + } + + public function testWarningAction() + { + $this->putWarning('Warning message'); + } + + public function testSuccessAction() + { + $this->putSuccess('Success message'); + } + + public function testObjectAction() + { + $this->putObject(['key' => 'value']); + } + + public function testTextAction() + { + $this->putText('Some text'); + } + + public function testArgAction() + { + $this->putText($this->getArg(0)); } } \ No newline at end of file diff --git a/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php b/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php index 38c1013..ded2f41 100644 --- a/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php +++ b/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Vegas\Fake\Task; +namespace Vegas\Fake\Nested\Task; use Vegas\Cli\Task\Action; diff --git a/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php b/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php index b29ac2b..38c1013 100644 --- a/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php +++ b/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php @@ -9,9 +9,25 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - -class FakeTask +namespace Vegas\Fake\Task; + +use Vegas\Cli\Task\Action; + +class FakeTask extends \Vegas\Cli\TaskAbstract { + /** + * Task must implement this method to set available options + * + * @return mixed + */ + public function setupOptions() + { + $this->addTaskAction(new Action('test', 't', 'Test action')); + } -} \ No newline at end of file + public function testAction() + { + $this->putText(__CLASS__); + } +} \ No newline at end of file From 8c0ee75a0547d2a4da940db3b9cfd583fa83ccfe Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 17 Dec 2014 14:02:44 +0100 Subject: [PATCH 34/35] Updated @author tag --- src/Bootstrap/LoaderInitializerTrait.php | 2 +- src/Bootstrap/ModulesInitializerTrait.php | 2 +- src/Bootstrap/ServicesInitializerTrait.php | 2 +- src/Db/Adapter/Mongo/DbRef.php | 2 +- src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php | 2 +- src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php | 2 +- src/Db/Adapter/Mongo/Mapper.php | 2 +- src/Db/Adapter/Mongo/RefResolverTrait.php | 2 +- src/Db/Decorator/Helper/ReadNestedAttributeTrait.php | 2 +- src/Mvc/Module/Exception/InvalidModulesListException.php | 2 +- src/Util/DateTime.php | 2 +- tests/Bootstrap/EnvironmentInitializerTraitTest.php | 2 +- tests/DI/Scaffolding/Adapter/MongoTest.php | 2 +- tests/Db/Adapter/Mongo/DbRefTest.php | 2 +- tests/Db/Adapter/Mongo/MapperTest.php | 2 +- tests/Db/Adapter/Mongo/RefResolverTraitTest.php | 2 +- tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php | 2 +- tests/Db/Decorator/Helper/SlugTraitTest.php | 2 +- tests/Db/Decorator/Helper/WriteAttributesTraitTest.php | 2 +- tests/Util/DateTimeTest.php | 2 +- tests/fixtures/app/modules/Ref/models/Child.php | 2 +- tests/fixtures/app/modules/Ref/models/Parental.php | 2 +- tests/fixtures/app/services/MongoMapperServiceProvider.php | 2 +- tests/fixtures/app/services/ViewCacheServiceProvider.php | 2 +- tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php | 2 +- tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php | 2 +- 26 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Bootstrap/LoaderInitializerTrait.php b/src/Bootstrap/LoaderInitializerTrait.php index ffccffb..f47726d 100644 --- a/src/Bootstrap/LoaderInitializerTrait.php +++ b/src/Bootstrap/LoaderInitializerTrait.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Bootstrap/ModulesInitializerTrait.php b/src/Bootstrap/ModulesInitializerTrait.php index 9801d13..00122b5 100644 --- a/src/Bootstrap/ModulesInitializerTrait.php +++ b/src/Bootstrap/ModulesInitializerTrait.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Bootstrap/ServicesInitializerTrait.php b/src/Bootstrap/ServicesInitializerTrait.php index 426b139..ff435df 100644 --- a/src/Bootstrap/ServicesInitializerTrait.php +++ b/src/Bootstrap/ServicesInitializerTrait.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Adapter/Mongo/DbRef.php b/src/Db/Adapter/Mongo/DbRef.php index 34c4d29..c169c67 100644 --- a/src/Db/Adapter/Mongo/DbRef.php +++ b/src/Db/Adapter/Mongo/DbRef.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php b/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php index 31bb9e3..97a503f 100644 --- a/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php +++ b/src/Db/Adapter/Mongo/Exception/CannotResolveModelException.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php b/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php index 908c9e7..521f785 100644 --- a/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php +++ b/src/Db/Adapter/Mongo/Exception/InvalidReferenceException.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Adapter/Mongo/Mapper.php b/src/Db/Adapter/Mongo/Mapper.php index 4d98dc9..948f2cb 100644 --- a/src/Db/Adapter/Mongo/Mapper.php +++ b/src/Db/Adapter/Mongo/Mapper.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Adapter/Mongo/RefResolverTrait.php b/src/Db/Adapter/Mongo/RefResolverTrait.php index 33db5f7..04b55be 100644 --- a/src/Db/Adapter/Mongo/RefResolverTrait.php +++ b/src/Db/Adapter/Mongo/RefResolverTrait.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php index e8084bb..b9829a8 100644 --- a/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php +++ b/src/Db/Decorator/Helper/ReadNestedAttributeTrait.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Mvc/Module/Exception/InvalidModulesListException.php b/src/Mvc/Module/Exception/InvalidModulesListException.php index 04f2dd6..f19b7c3 100644 --- a/src/Mvc/Module/Exception/InvalidModulesListException.php +++ b/src/Mvc/Module/Exception/InvalidModulesListException.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/src/Util/DateTime.php b/src/Util/DateTime.php index f87ef9e..5055cff 100644 --- a/src/Util/DateTime.php +++ b/src/Util/DateTime.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Bootstrap/EnvironmentInitializerTraitTest.php b/tests/Bootstrap/EnvironmentInitializerTraitTest.php index d0dd805..ebf3690 100644 --- a/tests/Bootstrap/EnvironmentInitializerTraitTest.php +++ b/tests/Bootstrap/EnvironmentInitializerTraitTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/DI/Scaffolding/Adapter/MongoTest.php b/tests/DI/Scaffolding/Adapter/MongoTest.php index e50ecf8..31ffcf3 100644 --- a/tests/DI/Scaffolding/Adapter/MongoTest.php +++ b/tests/DI/Scaffolding/Adapter/MongoTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Adapter/Mongo/DbRefTest.php b/tests/Db/Adapter/Mongo/DbRefTest.php index 120d3da..5a8cd89 100644 --- a/tests/Db/Adapter/Mongo/DbRefTest.php +++ b/tests/Db/Adapter/Mongo/DbRefTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Adapter/Mongo/MapperTest.php b/tests/Db/Adapter/Mongo/MapperTest.php index 6a2b72a..37f453e 100644 --- a/tests/Db/Adapter/Mongo/MapperTest.php +++ b/tests/Db/Adapter/Mongo/MapperTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Adapter/Mongo/RefResolverTraitTest.php b/tests/Db/Adapter/Mongo/RefResolverTraitTest.php index 4847a9f..66a78a3 100644 --- a/tests/Db/Adapter/Mongo/RefResolverTraitTest.php +++ b/tests/Db/Adapter/Mongo/RefResolverTraitTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php b/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php index 0f5956c..618bfcf 100644 --- a/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php +++ b/tests/Db/Decorator/Helper/ReadNestedAttributeTraitTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Decorator/Helper/SlugTraitTest.php b/tests/Db/Decorator/Helper/SlugTraitTest.php index 3cd353e..661816d 100644 --- a/tests/Db/Decorator/Helper/SlugTraitTest.php +++ b/tests/Db/Decorator/Helper/SlugTraitTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php b/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php index a5e1700..11b9039 100644 --- a/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php +++ b/tests/Db/Decorator/Helper/WriteAttributesTraitTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/Util/DateTimeTest.php b/tests/Util/DateTimeTest.php index f029ca6..8559899 100644 --- a/tests/Util/DateTimeTest.php +++ b/tests/Util/DateTimeTest.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/app/modules/Ref/models/Child.php b/tests/fixtures/app/modules/Ref/models/Child.php index e95dcb1..3c7ec47 100644 --- a/tests/fixtures/app/modules/Ref/models/Child.php +++ b/tests/fixtures/app/modules/Ref/models/Child.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/app/modules/Ref/models/Parental.php b/tests/fixtures/app/modules/Ref/models/Parental.php index 1d621dc..9cfb29a 100644 --- a/tests/fixtures/app/modules/Ref/models/Parental.php +++ b/tests/fixtures/app/modules/Ref/models/Parental.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/app/services/MongoMapperServiceProvider.php b/tests/fixtures/app/services/MongoMapperServiceProvider.php index 573d893..e4a2c6b 100644 --- a/tests/fixtures/app/services/MongoMapperServiceProvider.php +++ b/tests/fixtures/app/services/MongoMapperServiceProvider.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/app/services/ViewCacheServiceProvider.php b/tests/fixtures/app/services/ViewCacheServiceProvider.php index fbcf0b9..1dee1c8 100644 --- a/tests/fixtures/app/services/ViewCacheServiceProvider.php +++ b/tests/fixtures/app/services/ViewCacheServiceProvider.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php b/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php index ded2f41..724155a 100644 --- a/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php +++ b/tests/fixtures/lib/Vegas/Fake/Nested/Task/FakeTask.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * diff --git a/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php b/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php index 38c1013..99aeb30 100644 --- a/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php +++ b/tests/fixtures/lib/Vegas/Fake/Task/FakeTask.php @@ -2,7 +2,7 @@ /** * This file is part of Vegas package * - * @author Slawomir Zytko + * @author Slawomir Zytko * @copyright Amsterdam Standard Sp. Z o.o. * @homepage http://vegas-cmf.github.io * From fa8872d0ba55fb1ad2002ce98ef3d6d922fa0702 Mon Sep 17 00:00:00 2001 From: slawek Date: Wed, 17 Dec 2014 16:55:04 +0100 Subject: [PATCH 35/35] Added Scaffolding Mysql adapter tests; Removed BeforeException class; Updated config.sample.php; Updated travis.yml --- .travis.yml | 2 + src/DI/Scaffolding/Adapter/Mysql.php | 22 +++- .../Exception/MissingScaffoldingException.php | 23 ++++ src/Mvc/Dispatcher/Events/BeforeException.php | 39 ------- src/Mvc/ModuleAbstract.php | 3 +- tests/DI/Scaffolding/Adapter/MysqlTest.php | 108 ++++++++++++++++++ tests/config.sample.php | 3 +- 7 files changed, 157 insertions(+), 43 deletions(-) create mode 100644 src/DI/Scaffolding/Exception/MissingScaffoldingException.php delete mode 100644 src/Mvc/Dispatcher/Events/BeforeException.php create mode 100644 tests/DI/Scaffolding/Adapter/MysqlTest.php diff --git a/.travis.yml b/.travis.yml index 64e928c..13b6e88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/DI/Scaffolding/Adapter/Mysql.php b/src/DI/Scaffolding/Adapter/Mysql.php index cde1921..b8d649d 100644 --- a/src/DI/Scaffolding/Adapter/Mysql.php +++ b/src/DI/Scaffolding/Adapter/Mysql.php @@ -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; @@ -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) { @@ -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 )); @@ -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; + } } diff --git a/src/DI/Scaffolding/Exception/MissingScaffoldingException.php b/src/DI/Scaffolding/Exception/MissingScaffoldingException.php new file mode 100644 index 0000000..9002ef1 --- /dev/null +++ b/src/DI/Scaffolding/Exception/MissingScaffoldingException.php @@ -0,0 +1,23 @@ + + * @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'; +} + \ No newline at end of file diff --git a/src/Mvc/Dispatcher/Events/BeforeException.php b/src/Mvc/Dispatcher/Events/BeforeException.php deleted file mode 100644 index 24a95c2..0000000 --- a/src/Mvc/Dispatcher/Events/BeforeException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @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\Mvc\Dispatcher\Events; - -use Phalcon\Dispatcher; -use Phalcon\Events\Event; -use Vegas\Mvc\Dispatcher\ExceptionResolver; - -/** - * Class BeforeException - * @package Vegas\Mvc\Dispatcher\Events - */ -class BeforeException -{ - /** - * Event fired when exception is throwing - * - * @return callable - */ - public static function getEvent() - { - return function(Event $event, Dispatcher $dispatcher, \Exception $exception) { - $resolver = new ExceptionResolver(); - $resolver->setDI($dispatcher->getDI()); - $resolver->resolve($exception); - - return false; - }; - } -} \ No newline at end of file diff --git a/src/Mvc/ModuleAbstract.php b/src/Mvc/ModuleAbstract.php index d1e8ad7..a209e32 100644 --- a/src/Mvc/ModuleAbstract.php +++ b/src/Mvc/ModuleAbstract.php @@ -11,6 +11,7 @@ */ namespace Vegas\Mvc; +use Phalcon\DI\InjectionAwareInterface; use Phalcon\Loader; use Phalcon\Mvc\Dispatcher; use Phalcon\Mvc\ModuleDefinitionInterface; @@ -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); diff --git a/tests/DI/Scaffolding/Adapter/MysqlTest.php b/tests/DI/Scaffolding/Adapter/MysqlTest.php new file mode 100644 index 0000000..44c6b93 --- /dev/null +++ b/tests/DI/Scaffolding/Adapter/MysqlTest.php @@ -0,0 +1,108 @@ + + * @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()); + } +} \ No newline at end of file diff --git a/tests/config.sample.php b/tests/config.sample.php index a1d9ae9..8445365 100644 --- a/tests/config.sample.php +++ b/tests/config.sample.php @@ -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/', @@ -37,7 +37,6 @@ "dbname" => "vegas_test", "port" => 3306, "username" => "root", - 'password'=> 'root', "options" => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ]