Skip to content

Commit

Permalink
CCC-83, tests units and view
Browse files Browse the repository at this point in the history
  • Loading branch information
fozeek committed Dec 20, 2013
1 parent 6b3a689 commit 3bf89be
Show file tree
Hide file tree
Showing 18 changed files with 1,125 additions and 39 deletions.
1 change: 1 addition & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@
),
),
),
'autorize_user' => true,
);
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<phpunit bootstrap="./tests/Bootstrap.php">
<testsuites>
<testsuite name="playgroundgallery">
<directory>./tests/PlaygroundGallery</directory>
<directory>./tests/PlaygroundGalleryTest</directory>
</testsuite>
</testsuites>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ class GalleryAdminController extends AbstractActionController
*/
public function indexAction()
{
$user = $this->zfcUserAuthentication()->getIdentity();
$config = $this->getServiceLocator()->get('Config');
if(!array_key_exists('autorize_user', $config) || !$config['autorize_user']) {
$user = null;
}
else {
$user = $this->zfcUserAuthentication()->getIdentity();
}

$categories = $this->getCategoryService()->getCategoryMapper()->findBy(array('parent' => null));
$medias = $this->getMediaService()->getMediaMapper()->findAll();

Expand Down
1 change: 1 addition & 0 deletions src/PlaygroundGallery/Form/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function __construct ($name = null, ServiceManager $sm, Translator $trans
*/
private function getWebsites()
{
var_dump('hello');
$websites = $this->getServiceManager()->get('playgroundcore_website_service')->getWebsiteMapper()->findAll();
$websitesForm = array();
foreach ($websites as $website) {
Expand Down
30 changes: 29 additions & 1 deletion src/PlaygroundGallery/Form/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Media extends ProvidesEventsForm
{

protected $serviceManager;
protected $categoryService;

public function __construct ($name = null, ServiceManager $sm, Translator $translator)
{
Expand Down Expand Up @@ -127,7 +128,7 @@ public function __construct ($name = null, ServiceManager $sm, Translator $trans
}

private function getCategories() {
$categories = $this->getServiceManager()->get('playgroundgallery_category_service')->getCategoryMapper()->findBy(array('parent' => null));
$categories = $this->getCategoryService()->getCategoryMapper()->findBy(array('parent' => null));
$categoriesForm = array();
foreach ($categories as $category) {
$this->getChildrenCategories($category, $categoriesForm);
Expand Down Expand Up @@ -171,4 +172,31 @@ public function setServiceManager (ServiceManager $serviceManager)
return $this;
}

/**
* Retrieve category service instance
*
* @return CategoryService
*/
public function getCategoryService ()
{
if (null === $this->categoryService) {
$this->categoryService = $this->getServiceManager()->get('playgroundgallery_category_service');
}

return $this->categoryService;
}

/**
* Set service category instance
*
* @param ServiceManager $categoryService
* @return this
*/
public function setCategoryService ($categoryService)
{
$this->categoryService = $categoryService;

return $this;
}

}
37 changes: 3 additions & 34 deletions src/PlaygroundGallery/Service/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class Category extends EventProvider implements ServiceManagerAwareInterface
*/
protected $serviceManager;

/**
* @var UserServiceOptionsInterface
*/
protected $options;

/**
* @var categoryForm
*/
Expand All @@ -55,8 +50,9 @@ public function create(array $data)

$this->addCategoryParent($category, $data);


$category = $this->addWebsite($category, $data['websites']);
if(array_key_exists('websites', $data)) {
$category = $this->addWebsite($category, $data['websites']);
}

$form->bind($category);
$form->setData($data);
Expand Down Expand Up @@ -148,33 +144,6 @@ public function setCategoryMapper($categoryMapper)
return $this;
}

/**
* setOptions
* @param ModuleOptions $options
*
* @return PlaygroundGallery\Service\Category $this
*/
public function setOptions(ModuleOptions $options)
{
$this->options = $options;

return $this;
}

/**
* getOptions
*
* @return ModuleOptions $optins
*/
public function getOptions()
{
if (!$this->options instanceof ModuleOptions) {
$this->setOptions($this->getServiceManager()->get('playgroundgallery_module_options'));
}

return $this->options;
}

/**
* Retrieve service manager instance
*
Expand Down
3 changes: 1 addition & 2 deletions src/PlaygroundGallery/Service/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public function getMediaMapper()
if (null === $this->mediaMapper) {
$this->mediaMapper = $this->getServiceManager()->get('playgroundgallery_media_mapper');
}

return $this->mediaMapper;
}

Expand Down Expand Up @@ -216,7 +215,7 @@ public function getCategoryMapper()
*/
public function setCategoryMapper($categoryMapper)
{
$this->categoryMapper = $categoryMappers;
$this->categoryMapper = $categoryMapper;

return $this;
}
Expand Down
122 changes: 122 additions & 0 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
namespace PlaygroundGalleryTest;

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;
protected static $config;
protected static $bootstrap;

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();

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[] = $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;

static::$config = $config;

// disable FirePHP for Unit testing
//$firephp = \FirePHP::getInstance(true);
//$firephp->setEnabled(false);
}

public static function getServiceManager()
{
return static::$serviceManager;
}

public static function getConfig()
{
return static::$config;
}



protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');

if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
$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.');
}

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();

// let's avoid session_start(): Cannot send session cookie - headers already sent by
ob_start();
98 changes: 98 additions & 0 deletions tests/PlaygroundGalleryTest/Mapper/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace PlaygroundGalleryTest\Mapper;

use PlaygroundGalleryTest\Bootstrap;
use \PlaygroundGallery\Entity\Category as CategoryEntity;

class CategoryTest extends \PHPUnit_Framework_TestCase
{
/**
* Service Manager
* @var Zend\ServiceManager\ServiceManager
*/
protected $sm;

/**
* Doctrine Entity Manager
* @var Doctrine\ORM\EntityManager
*/
protected $em;

/**
* Category sample
* @var Array
*/
protected $categoryData;

public function setUp()
{
$this->sm = Bootstrap::getServiceManager();
$this->em = $this->sm->get('doctrine.entitymanager.orm_default');
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
$classes = $this->em->getMetadataFactory()->getAllMetadata();
$tool->dropSchema($classes);
$tool->createSchema($classes);

$this->categoryData = array(
'name' => 'CeciEstUnTitre',
);

parent::setUp();
}

public function testCanInsertNewRecord()
{
$category = new CategoryEntity();
$category->populate($this->categoryData);

// save data
$this->em->persist($category);
$this->em->flush();

$this->assertEquals($this->categoryData['name'], $category->getName());

return $category->getId();
}

/**
* @depends testCanInsertNewRecord
*/
public function testCanUpdateInsertedRecord($id)
{
$data = array(
'id' => $id
);
$category = $this->em->getRepository('PlaygroundGallery\Entity\Category')->find($id);
$this->assertInstanceOf('PlaygroundGallery\Entity\Category', $category);
$this->assertEquals($this->categoryData['name'], $category->getName());

$category->populate($data);
$this->em->flush();

$this->assertEquals($this->categoryData['name'], $category->getName());
}

/**
* @depends testCanInsertNewRecord
*/
public function testCanRemoveInsertedRecord($id)
{
$category = $this->em->getRepository('PlaygroundGallery\Entity\Category')->find($id);
$this->assertInstanceOf('PlaygroundGallery\Entity\Category', $category);

$this->em->remove($category);
$this->em->flush();

$category = $this->em->getRepository('PlaygroundGallery\Entity\Category')->find($id);
$this->assertEquals(false, $category);
}

public function tearDown()
{
$dbh = $this->em->getConnection();

unset($this->sm);
unset($this->em);
parent::tearDown();
}
}
Loading

0 comments on commit 3bf89be

Please sign in to comment.