diff --git a/module/Application/Module.php b/module/Application/Module.php index 9a7d6e01..c9f7b836 100644 --- a/module/Application/Module.php +++ b/module/Application/Module.php @@ -40,9 +40,6 @@ public function getAutoloaderConfig() public function getServiceConfig() { return array( - 'invokables' => array( - 'application_module_service' => 'Application\Service\Module', - ), 'factories' => array( 'application_module_mapper' => function ($sm) { $mapper = new Mapper\Module(); @@ -51,6 +48,16 @@ public function getServiceConfig() $mapper->setHydrator(new Mapper\ModuleHydrator()); return $mapper; }, + 'application_module_service' => function($sm) { + $service = new Service\Module; + $service->setApi($sm->get('edpgithub_api_factory')); + return $service; + }, + 'application_service_repository' => function($sm) { + $service = new Service\Repository; + $service->setApi($sm->get('edpgithub_api_factory')); + return $service; + }, ), ); } diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index e3ae5811..2245734f 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -22,7 +22,7 @@ ), 'module' => array( 'type' => 'Literal', - 'options' => array ( + 'options' => array ( 'route' => '/module', ), 'may_terminate' => true, @@ -130,14 +130,14 @@ 'userWidget' => 'Application\View\Helper\UserWidget', 'listModules' => 'Application\View\Helper\ListModules', ), - 'factories' => array( - 'flashMessenger' => function($sm) { + 'factories' => array( + 'flashMessenger' => function($sm) { $sm = $sm->getServiceLocator(); - $plugin = $sm->get('ControllerPluginManager')->get('flashMessenger'); + $plugin = $sm->get('ControllerPluginManager')->get('flashMessenger'); - $helper = new Application\View\Helper\FlashMessenger($plugin); - return $helper; - } - ) + $helper = new Application\View\Helper\FlashMessenger($plugin); + return $helper; + } + ) ), ); diff --git a/module/Application/src/Application/Controller/IndexController.php b/module/Application/src/Application/Controller/IndexController.php index 8fb42a49..dae6b2e5 100644 --- a/module/Application/src/Application/Controller/IndexController.php +++ b/module/Application/src/Application/Controller/IndexController.php @@ -29,19 +29,19 @@ public function githubAction() $repoList = array(); $service = $api->getService('Repo'); $memberRepositories = $service->listRepositories(null, 'member'); - + foreach($memberRepositories as $repo) { $repoList[$repo->getName()] = $repo; } $allRepositories = $service->listRepositories(null, 'all'); - + foreach($allRepositories as $repo) { if(!$repo->getFork()) { $repoList[$repo->getName()] = $repo; } } - + return array('repositories' => $repoList, 'api' => $api); } } diff --git a/module/Application/src/Application/Service/Repository.php b/module/Application/src/Application/Service/Repository.php index c4c58cc6..b44990ac 100644 --- a/module/Application/src/Application/Service/Repository.php +++ b/module/Application/src/Application/Service/Repository.php @@ -2,32 +2,36 @@ namespace Application\Service; -use Zend\ServiceManager\ServiceManagerAwareInterface; -use Zend\ServiceManager\ServiceManager; +use EdpGithub\ApiClient\ApiClient; -class Repository implements ServiceManagerAwareInterface +class Repository { - protected $serviceManager; - + /** + * @var array + */ protected $repository; + /** + * @var EdpGithub\ApiClient\ApiFactory + */ + protected $api; + /** * Get All Repositories from github for authenticated user - * @param string $type + * @param string $type * @return array */ public function getAllRepository($type) { if(!isset($this->repository[$type])) { - $sm = $this->getServiceManager(); - $api = $sm->get('edpgithub_api_factory'); - $service = $api->getService('Repo'); - + echo $this->api->getOauthToken(); + exit; + $service = $this->api->getService('Repo'); $params['per_page'] = 100; $params['page'] = 1; $this->repositories[$type] = $service->listRepositories(null, $type, $params); - if($api->getNext() && $api->getNext() != $api->getCurrent()) { - $params['page'] = $api->getNext(); + if($this->api->getNext() && $this->api->getNext() != $this->api->getCurrent()) { + $params['page'] = $this->api->getNext(); $this->getRepositoriesRecursive($this->repositories[$type], $params); } } @@ -36,37 +40,20 @@ public function getAllRepository($type) /** * Recursively fetch all pages for Repositories - * @param array $repos + * @param array $repos * @param string $params */ protected function getRepositoriesRecursive(&$repos, $params) { - $sm = $this->getServiceManager(); - $api = $sm->get('edpgithub_api_factory'); - $service = $api->getService('Repo'); + $service = $this->api->getService('Repo'); $repos = array_merge($repos, $service->listRepositories(null, 'owner', $params)); - if($api->getNext() && $api->getNext() != $params['page']) { - $params['page'] = $api->getNext(); + if($this->api->getNext() && $this->api->getNext() != $params['page']) { + $params['page'] = $this->api->getNext(); $this->getAllRepos($repos, $params); } } - - /** - * Get Service Manager - * @return ServiceManager - */ - public function getServiceManager() - { - return $this->serviceManager; - } - - /** - * Set ServiceManager - * @param ServiceManager $serviceManager - */ - public function setServiceManager(ServiceManager $serviceManager) + public function setApi(ApiClient $api) { - $this->serviceManager = $serviceManager; - return $this; + $this->api = $api; } } \ No newline at end of file diff --git a/module/Application/test/ApplicationTest/Service/RepositoryTest.php b/module/Application/test/ApplicationTest/Service/RepositoryTest.php new file mode 100644 index 00000000..b23bb09b --- /dev/null +++ b/module/Application/test/ApplicationTest/Service/RepositoryTest.php @@ -0,0 +1,37 @@ +getMock('EdpGithub\ApiClient\ApiClient'); + $repo = $this->getMock('EdpGithub\ApiClient\ApiFactory\Service\Repo'); + + $api->expects($this->once()) + ->method('getService') + ->with($this->equalTo('Repo')) + ->will($this->returnValue('test')); + + $service = new Repository(); + $service->setApi($api); + + $service->getAllRepository('member'); + + + } +} \ No newline at end of file diff --git a/module/Application/test/Bootstrap.php b/module/Application/test/Bootstrap.php new file mode 100644 index 00000000..e6284561 --- /dev/null +++ b/module/Application/test/Bootstrap.php @@ -0,0 +1,110 @@ + array( + 'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths), + ), + ); + + $config = ArrayUtils::merge($baseConfig, $testConfig); + + $serviceManager = new ServiceManager(new ServiceManagerConfig()); + $serviceManager->setService('ApplicationConfig', $config); + $serviceManager->get('ModuleManager')->loadModules(); + static::$serviceManager = $serviceManager; + } + + public static function getServiceManager() + { + return static::$serviceManager; + } + + protected static function initAutoloader() + { + $vendorPath = static::findParentPath('vendor'); + + if (is_readable($vendorPath . '/autoload.php')) { + $loader = include $vendorPath . '/autoload.php'; + } + + $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false)); + + if (!$zf2Path) { + throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'); + } + + if (isset($loader)) { + $loader->add('Zend', $zf2Path . '/Zend'); + } else { + include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; + 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(); \ No newline at end of file diff --git a/module/Application/test/TestConfig.php.dist b/module/Application/test/TestConfig.php.dist new file mode 100644 index 00000000..8ccfd407 --- /dev/null +++ b/module/Application/test/TestConfig.php.dist @@ -0,0 +1,12 @@ + array( + 'EdpGithub', + 'Application', + ), + 'module_listener_options' => array( + 'module_paths' => array( + 'moduledev', + ), + ), +); \ No newline at end of file diff --git a/module/Application/test/phpunit.xml b/module/Application/test/phpunit.xml new file mode 100644 index 00000000..9567438b --- /dev/null +++ b/module/Application/test/phpunit.xml @@ -0,0 +1,7 @@ + + + + ./ApplicationTest + + + \ No newline at end of file diff --git a/module/User/Module.php b/module/User/Module.php index f7286980..58fac2b6 100644 --- a/module/User/Module.php +++ b/module/User/Module.php @@ -38,9 +38,6 @@ public function getServiceConfig() return $mapper; }, ), - 'invokables' => array( - 'application_service_repository' => 'Application\Service\Repository', - ), ); }