Skip to content

Commit

Permalink
Addign Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hounddog committed Oct 24, 2012
1 parent e4fd292 commit 92b4ed7
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 52 deletions.
13 changes: 10 additions & 3 deletions module/Application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
},
),
);
}
Expand Down
16 changes: 8 additions & 8 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
),
'module' => array(
'type' => 'Literal',
'options' => array (
'options' => array (
'route' => '/module',
),
'may_terminate' => true,
Expand Down Expand Up @@ -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;
}
)
),
);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
57 changes: 22 additions & 35 deletions module/Application/src/Application/Service/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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;
}
}
37 changes: 37 additions & 0 deletions module/Application/test/ApplicationTest/Service/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ApplicationTest\Service;

use PHPUnit_Framework_TestCase;
use Application\Service\Repository;

class RepositoryTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
// your code here
}

public function tearDown()
{
// your code here
}

public function testGetAllRepositories()
{
$api = $this->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');


}
}
110 changes: 110 additions & 0 deletions module/Application/test/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
namespace ApplicationTest;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
protected static $serviceManager;

public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/TestConfig.php')) {
$testConfig = include __DIR__ . '/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
}

$zf2ModulePaths = array(dirname(dirname(__DIR__)));
if (($path = static::findParentPath('vendor'))) {
$zf2ModulePaths[] = $path;
}
if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
$zf2ModulePaths[] = $path;
}

if(isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath)) !== $zf2ModulePaths[0]) {
$zf2ModulePaths[] = $path;
}
}
}

$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

static::initAutoloader();

// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => 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();
12 changes: 12 additions & 0 deletions module/Application/test/TestConfig.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
return array(
'modules' => array(
'EdpGithub',
'Application',
),
'module_listener_options' => array(
'module_paths' => array(
'moduledev',
),
),
);
7 changes: 7 additions & 0 deletions module/Application/test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuites>
<testsuite name="Application Module Tests">
<directory>./ApplicationTest</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 0 additions & 3 deletions module/User/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public function getServiceConfig()
return $mapper;
},
),
'invokables' => array(
'application_service_repository' => 'Application\Service\Repository',
),
);
}

Expand Down

0 comments on commit 92b4ed7

Please sign in to comment.