diff --git a/src/Rox/Gateway/MongoDb/AbstractGateway.php b/src/Rox/Gateway/MongoDb/AbstractGateway.php index e533f0e..acfcfab 100755 --- a/src/Rox/Gateway/MongoDb/AbstractGateway.php +++ b/src/Rox/Gateway/MongoDb/AbstractGateway.php @@ -8,132 +8,165 @@ /** * Use this class to implement collection especific methods - * @todo Review docs and transactions checking success and thow exceptions in negative cases + * + * TODO Review docs and transactions checking success and thow exceptions in negative cases * TODO implements a interface for common methods * @author Marcelo Araújo */ class AbstractGateway extends RoxGateway { - /** - * - * @param mixed $id - * @param string $module - * @param string $collection - * @return array - */ - public function getReference($id, $module, $collection){ - $className = "$module\Gateway\MongoDb\\$collection"; - $gateway = new $className($this->db); - return $gateway->findById($id); - } - /** - * - * @param array $documents - * @param int $id - * @return array|NULL - */ - public function getSubDocument($documents, $id) { - foreach ($documents as $document) { - if($document['_id'] == $id){ - return $document; - } - } - return null; - } + /** - * + * + * @param mixed $id + * @param string $module + * @param string $collection + * @return array + */ + public function getReference($id, $module, $collection) + { + $className = "$module\Gateway\MongoDb\\$collection"; + $gateway = new $className($this->db); + return $gateway->findById($id); + } + + /** + * + * @param array $documents + * @param int $id + * @return array|NULL + */ + public function getSubDocument($documents, $id) + { + foreach ($documents as $document) { + if ($document['_id'] == $id) { + return $document; + } + } + return null; + } + + /** + * * @param \MongoCursor $cursor * @return \PhlyMongo\HydratingMongoCursor */ - public function hydrateCollection(\MongoCursor $cursor){ - return new HydratingMongoCursor( - $cursor, - $this->hydrator, - $this->model - ); + public function hydrateCollection(\MongoCursor $cursor) + { + return new HydratingMongoCursor($cursor, $this->model->getHydrator(), $this->model); } + /** + * * @return \MongoCollection */ - public function getCollection(){ - return $this->db->{$this->name}; + public function getCollection() + { + return $this->db->{$this->name}; } + /** * Find all documents from especific colection + * * @return \Zend\Paginator\Paginator */ - public function findAll($criteria = [], $sort = null){ - $cursor = $this->db->{$this->name}->find($criteria); - if($sort){ - $cursor->sort($sort); - } - $adapter = new MongoPaginatorAdapter(new HydratingMongoCursor( - $cursor, - $this->hydrator, - $this->model - )); - return new Paginator($adapter); + public function findAll($criteria = [], $sort = null) + { + $cursor = $this->getCollection()->find($criteria); + if ($sort) { + $cursor->sort($sort); + } + $adapter = new MongoPaginatorAdapter(new HydratingMongoCursor($cursor, $this->model->getHydrator(), $this->model)); + return new Paginator($adapter); } + /** * * @param array $criteria * @return array */ - public function count($criteria = []){ - return $this->db->{$this->name}->count($criteria); + public function count($criteria = []) + { + return $this->getCollection()->count($criteria); } + /** - * + * * @param array $criteria * @return array */ - public function findCurrent($criteria = []){ - return $this->db->{$this->name}->findOne($criteria); + public function findCurrent($criteria = []) + { + return $this->getCollection()->findOne($criteria); } + /** + * * @param string $label * @return array An associative array with [value => label] format */ - public function getAssocArray($criteria = [], $label = 'name'){ - $assoc = []; - $data = $this->db->{$this->name}->find($criteria,['_id', $label])->sort([$label => 1]); - foreach ($data as $record){ - $assoc[$record['_id']->{'$id'}] = $record[$label]; - } - return $assoc; + public function getAssocArray($criteria = [], $label = 'name') + { + $assoc = []; + $data = $this->getCollection() + ->find($criteria, [ + '_id', + $label + ]) + ->sort([ + $label => 1 + ]); + foreach ($data as $record) { + $assoc[$record['_id']->{'$id'}] = $record[$label]; + } + return $assoc; } + /** * Find and return a document by its mongoId - * @param mixed $id of document + * + * @param mixed $id + * of document * @return array */ - public function findById($id){ - return $this->db->{$this->name}->findOne(['_id' => $this->getMongoId($id)]); + public function findById($id) + { + return $this->getCollection()->findOne([ + '_id' => $this->getMongoId($id) + ]); } + /** - * @param array $data with _id + * + * @param array $data + * with _id * @return mixed */ - public function update(array $data){ - $data = $this->filterData($data); - $id = $this->getMongoId($data['_id']); - unset($data['_id']); - return $this->getCollection()->update(['_id' => $id], ['$set' => $data]); + public function update(array $data) + { + $id = $this->getMongoId($data['_id']); + unset($data['_id']); + return $this->getCollection()->update([ + '_id' => $id + ], [ + '$set' => $data + ]); } + /** + * * @param array $data * @return mixed */ - public function save(array $data){ - $data = $this->filterData($data); - if(!$data['_id']){ - unset($data['_id']); - } else { - $data['_id'] = $this->getMongoId($data['_id']); - } - - $this->db->{$this->name}->save($data); - return $data; + public function save(array $data) + { + if (! $data['_id']) { + unset($data['_id']); + } else { + $data['_id'] = $this->getMongoId($data['_id']); + } + $this->getCollection()->save($data); + return $data; } /** @@ -153,11 +186,16 @@ public function getMongoId($id, $field = 'unknown') throw new \Exception("Parâmetro inválido: $field"); } } + /** + * * @param mixed $id * @return mixed */ - public function delete($id){ - return $this->db->{$this->name}->remove(['_id' => $this->getMongoId($id)]); + public function delete($id) + { + return $this->getCollection()->remove([ + '_id' => $this->getMongoId($id) + ]); } } \ No newline at end of file diff --git a/src/Rox/Gateway/RoxGateway.php b/src/Rox/Gateway/RoxGateway.php index 7d05fee..9cd8875 100755 --- a/src/Rox/Gateway/RoxGateway.php +++ b/src/Rox/Gateway/RoxGateway.php @@ -1,72 +1,51 @@ db = $db; - $this->reflectionClass = new \ReflectionClass(get_class($this)); - $this->name = $this->reflectionClass->getShortName(); - $this->setModel($model); - $this->setHydrator($hydrator); - - } - /** - * - * @param array $data - */ - public function filterData(array $data){ - $model = $this->hydrator->hydrate($data, $this->model); - return $this->hydrator->extract($model); - } - public function setModel($model){ - if($model){ - $this->model = $model; - } else { - $modelName = $this->getModelName(); - $namespace = $this->reflectionClass->getNamespaceName(); - $modelNamespace = substr($namespace, 0, strpos($namespace,"\\")); - $modelClassName = sprintf('\%s\Model\%s',$modelNamespace, $modelName); - $this->model = new $modelClassName; - } - } - public function getModelName() { - $inflector = new Inflector(); - $name = $this->reflectionClass->getShortName(); - return $inflector->singularize($name); - } - public function setHydrator($hydrator){ - if($hydrator){ - $this->hydrator = $hydrator; - } else { - $this->hydrator = new MagicMethods(); - } - } - /** - * Proxy for Rox\Model\AbstractModel::getInputFilter() - * @return Zend\InputFilter\InputFilter - */ - public function getInputFilter($fields = null) - { - return $this->model->getInputFilter($fields); - } +class RoxGateway +{ + + protected $model; + + protected $db; + + protected $name; + + protected $reflectionClass; + + /** + * + * @param mixed $db + * @param Rox\Model\AbstractModel $model + * @param Zend\Stdlib\Hydrator\HydratorInterface $hydrator + */ + public function __construct($db, AbstractModel $model = null) + { + $this->db = $db; + $this->reflectionClass = new \ReflectionClass(get_class($this)); + $this->name = $this->reflectionClass->getShortName(); + $this->setModel($model); + } + + public function setModel($model) + { + if ($model) { + $this->model = $model; + } else { + $modelName = $this->getModelName(); + $namespace = $this->reflectionClass->getNamespaceName(); + $modelNamespace = substr($namespace, 0, strpos($namespace, "\\")); + $modelClassName = sprintf('\%s\Model\%s', $modelNamespace, $modelName); + $this->model = new $modelClassName(); + } + } + + public function getModelName() + { + $inflector = new Inflector(); + $name = $this->reflectionClass->getShortName(); + return $inflector->singularize($name); + } } \ No newline at end of file diff --git a/src/Rox/Model/AbstractModel.php b/src/Rox/Model/AbstractModel.php index 03261c6..26a457d 100755 --- a/src/Rox/Model/AbstractModel.php +++ b/src/Rox/Model/AbstractModel.php @@ -16,7 +16,7 @@ abstract class AbstractModel { - const INT = 'Zend\I18n\Validator\Int'; + const INT = 'Zend\I18n\Validator\IsInt'; const ALPHA = 'Zend\I18n\Validator\Alpha'; @@ -89,6 +89,10 @@ public function getHydrator() return $this->hydrator; } + /** + * + * @param array $data + */ public function exchangeArray(array $data) { $this->hydrator->hydrate($data, $this); diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php new file mode 100755 index 0000000..f79ac4a --- /dev/null +++ b/tests/Bootstrap.php @@ -0,0 +1,98 @@ + array( + 'module_paths' => $zf2ModulePaths, + ), + 'modules' => array( + 'Rox' + ) + ); + + $serviceManager = new ServiceManager(new ServiceManagerConfig()); + $serviceManager->setService('ApplicationConfig', $config); + $serviceManager->get('ModuleManager')->loadModules(); + static::$serviceManager = $serviceManager; + } + + public static function chroot() + { + $rootPath = dirname(static::findParentPath('module')); + chdir($rootPath); + } + + public static function getServiceManager() + { + return static::$serviceManager; + } + + protected static function initAutoloader() + { + $vendorPath = static::findParentPath('vendor'); + + if (file_exists($vendorPath.'/autoload.php')) { + include $vendorPath.'/autoload.php'; + } + + if (! class_exists('Zend\Loader\AutoloaderFactory')) { + throw new RuntimeException( + 'Unable to load ZF2. Run `php composer.phar install`' + ); + } + + AutoloaderFactory::factory(array( + 'Zend\Loader\StandardAutoloader' => array( + 'autoregister_zf' => true, + 'namespaces' => array( + __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__, + ), + ), + )); + } + + protected static function findParentPath($path) + { + $dir = __DIR__; + $previousDir = '.'; + while (!is_dir($dir . '/' . $path)) { + $dir = dirname($dir); + if ($previousDir === $dir) { + return false; + } + $previousDir = $dir; + } + return $dir . '/' . $path; + } +} + +Bootstrap::init(); +Bootstrap::chroot(); \ No newline at end of file diff --git a/tests/Rox/Framework/TestCase.php b/tests/Rox/Framework/TestCase.php deleted file mode 100755 index 5ac9b55..0000000 --- a/tests/Rox/Framework/TestCase.php +++ /dev/null @@ -1,28 +0,0 @@ -assertInstanceOf('Zend\Di\LocatorInterface', $this->getLocator()); - } -} diff --git a/tests/RoxTest/Gateway/MongoDb/AbstractGatwayTest.php b/tests/RoxTest/Gateway/MongoDb/AbstractGatwayTest.php new file mode 100755 index 0000000..86ae6b9 --- /dev/null +++ b/tests/RoxTest/Gateway/MongoDb/AbstractGatwayTest.php @@ -0,0 +1,23 @@ +getMockForAbstractClass('Rox\Model\AbstractModel'); + $obj = $this->getMockForAbstractClass('Rox\Gateway\MongoDb\AbstractGateway',[new \MongoClient(), $modelMock]); + $this->assertInstanceOf('\MongoDB', $obj->getCollection()); + } +} diff --git a/tests/TestConfiguration.php.dist b/tests/TestConfiguration.php similarity index 93% rename from tests/TestConfiguration.php.dist rename to tests/TestConfiguration.php index 7f1d893..1419f67 100755 --- a/tests/TestConfiguration.php.dist +++ b/tests/TestConfiguration.php @@ -20,7 +20,7 @@ * Never commit plaintext passwords to the source code repository. */ -define('ZF2_PATH', realpath(__DIR__ . '/../../../vendor/zendframework/zendframework/library/')); +define('ZF2_PATH', realpath(__DIR__ . '/../../../zendframework/zendframework/library/')); /** * The bootstrap supports several more options, however most modules will diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100755 index 802b715..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,106 +0,0 @@ - array( - StandardAutoloader::AUTOREGISTER_ZF => true, - StandardAutoloader::ACT_AS_FALLBACK => false, - StandardAutoloader::LOAD_NS => $additionalNamespaces, - ) - ) -); - -// The module name is obtained using directory name or constant -$moduleName = pathinfo($rootPath, PATHINFO_BASENAME); -if (defined('MODULE_NAME')) { - $moduleName = MODULE_NAME; -} - -// A locator will be set to this class if available -$moduleTestCaseClassname = '\\'.$moduleName.'Test\\Framework\\TestCase'; - -// This module's path plus additionally defined paths are used $modulePaths -$modulePaths = array(dirname($rootPath)); -if (isset($additionalModulePaths)) { - $modulePaths = array_merge($modulePaths, $additionalModulePaths); -} - -// Load this module and defined dependencies -$modules = array($moduleName); -if (isset($moduleDependencies)) { - $modules = array_merge($modules, $moduleDependencies); -} - - -$listenerOptions = new Zend\ModuleManager\Listener\ListenerOptions(array('module_paths' => $modulePaths)); -$defaultListeners = new Zend\ModuleManager\Listener\DefaultListenerAggregate($listenerOptions); -$sharedEvents = new Zend\EventManager\SharedEventManager(); -$moduleManager = new \Zend\ModuleManager\ModuleManager($modules); -$moduleManager->getEventManager()->setSharedManager($sharedEvents); -$moduleManager->getEventManager()->attachAggregate($defaultListeners); -$moduleManager->loadModules(); - -if (method_exists($moduleTestCaseClassname, 'setLocator')) { - $config = $defaultListeners->getConfigListener()->getMergedConfig(); - - $di = new \Zend\Di\Di; - $di->instanceManager()->addTypePreference('Zend\Di\LocatorInterface', $di); - - if (isset($config['di'])) { - $diConfig = new \Zend\Di\Config($config['di']); - $diConfig->configure($di); - } - - $routerDiConfig = new \Zend\Di\Config( - array( - 'definition' => array( - 'class' => array( - 'Zend\Mvc\Router\RouteStackInterface' => array( - 'instantiator' => array( - 'Zend\Mvc\Router\Http\TreeRouteStack', - 'factory' - ), - ), - ), - ), - ) - ); - $routerDiConfig->configure($di); - - call_user_func_array($moduleTestCaseClassname.'::setLocator', array($di)); -} - -// When this is in global scope, PHPUnit catches exception: -// Exception: Zend\Stdlib\PriorityQueue::serialize() must return a string or NULL -unset($moduleManager, $sharedEvents); diff --git a/tests/phpunit.xml b/tests/phpunit.xml index ef934fa..7d3d8a7 100755 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,7 +1,7 @@ - + - ./ + ./RoxTest