forked from sonata-project/SonataMediaBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sonata-project#899 from core23/fixmedia-command
Created new command for creating default categories
- Loading branch information
Showing
3 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\MediaBundle\Command; | ||
|
||
use Sonata\ClassificationBundle\Model\ContextInterface; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class FixMediaContextCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure() | ||
{ | ||
$this->setName('sonata:media:fix-media-context'); | ||
$this->setDescription('Generate the default category for each media context'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$pool = $this->getContainer()->get('sonata.media.pool'); | ||
$contextManager = $this->getContainer()->get('sonata.classification.manager.context'); | ||
$cateoryManager = $this->getContainer()->get('sonata.classification.manager.category'); | ||
|
||
foreach ($pool->getContexts() as $context => $contextAttrs) { | ||
/** @var ContextInterface $defaultContext */ | ||
$defaultContext = $contextManager->findOneBy(array( | ||
'id' => $context, | ||
)); | ||
|
||
if (!$defaultContext) { | ||
$output->writeln(sprintf(" > default context for '%s' is missing, creating one", $context)); | ||
$defaultContext = $contextManager->create(); | ||
$defaultContext->setId($context); | ||
$defaultContext->setName(ucfirst($context)); | ||
$defaultContext->setEnabled(true); | ||
|
||
$contextManager->save($defaultContext); | ||
} | ||
|
||
$defaultCategory = $cateoryManager->getRootCategory($defaultContext); | ||
|
||
if (!$defaultCategory) { | ||
$output->writeln(sprintf(" > default category for '%s' is missing, creating one", $context)); | ||
$defaultCategory = $cateoryManager->create(); | ||
$defaultCategory->setContext($defaultContext); | ||
$defaultCategory->setName(ucfirst($context)); | ||
$defaultCategory->setEnabled(true); | ||
$defaultCategory->setPosition(0); | ||
|
||
$cateoryManager->save($defaultCategory); | ||
} | ||
} | ||
|
||
$output->writeln('Done!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\MediaBundle\Tests\Command; | ||
|
||
use Sonata\ClassificationBundle\Model\CategoryManagerInterface; | ||
use Sonata\ClassificationBundle\Model\ContextManagerInterface; | ||
use Sonata\MediaBundle\Command\FixMediaContextCommand; | ||
use Sonata\MediaBundle\Provider\Pool; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Console\Tests\Command\CommandTest; | ||
|
||
class FixMediaContextCommandTest extends CommandTest | ||
{ | ||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* @var Application | ||
*/ | ||
protected $application; | ||
|
||
/** | ||
* @var ContainerAwareCommand | ||
*/ | ||
protected $command; | ||
|
||
/** | ||
* @var CommandTester | ||
*/ | ||
protected $tester; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|Pool | ||
*/ | ||
private $pool; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|ContextManagerInterface | ||
*/ | ||
private $contextManger; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|CategoryManagerInterface | ||
*/ | ||
private $categoryManger; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); | ||
|
||
$this->command = new FixMediaContextCommand(); | ||
$this->command->setContainer($this->container); | ||
|
||
$this->application = new Application(); | ||
$this->application->add($this->command); | ||
|
||
$this->tester = new CommandTester($this->application->find('sonata:media:fix-media-context')); | ||
|
||
$this->pool = $this->getMockBuilder('Sonata\MediaBundle\Provider\Pool')->disableOriginalConstructor()->getMock(); | ||
|
||
$this->contextManger = $this->getMock('Sonata\ClassificationBundle\Model\ContextManagerInterface'); | ||
|
||
$this->categoryManger = $this->getMockBuilder('Sonata\ClassificationBundle\Entity\CategoryManager')->disableOriginalConstructor()->getMock(); | ||
|
||
$this->container->expects($this->any()) | ||
->method('get') | ||
->will($this->returnCallback(function ($id) { | ||
switch ($id) { | ||
case 'sonata.media.pool': | ||
return $this->pool; | ||
case 'sonata.classification.manager.context': | ||
return $this->contextManger; | ||
case 'sonata.classification.manager.category': | ||
return $this->categoryManger; | ||
} | ||
|
||
return; | ||
})); | ||
} | ||
|
||
public function testExecuteWithExisting() | ||
{ | ||
$context = array( | ||
'providers' => array(), | ||
'formats' => array(), | ||
'download' => array(), | ||
); | ||
|
||
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); | ||
|
||
$contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface'); | ||
|
||
$this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue($contextModel)); | ||
|
||
$category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface'); | ||
|
||
$this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue($category)); | ||
|
||
$output = $this->tester->execute(array()); | ||
|
||
$this->assertRegExp('@Done!@', $this->tester->getDisplay()); | ||
|
||
$this->assertSame(0, $output); | ||
} | ||
|
||
public function testExecuteWithEmptyRoot() | ||
{ | ||
$context = array( | ||
'providers' => array(), | ||
'formats' => array(), | ||
'download' => array(), | ||
); | ||
|
||
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); | ||
|
||
$contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface'); | ||
|
||
$this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue($contextModel)); | ||
|
||
$category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface'); | ||
|
||
$this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue(null)); | ||
$this->categoryManger->expects($this->once())->method('create')->will($this->returnValue($category)); | ||
$this->categoryManger->expects($this->once())->method('save')->with($this->equalTo($category)); | ||
|
||
$output = $this->tester->execute(array()); | ||
|
||
$this->assertRegExp('@ > default category for \'foo\' is missing, creating one\s+Done!@', $this->tester->getDisplay()); | ||
|
||
$this->assertSame(0, $output); | ||
} | ||
|
||
public function testExecuteWithNew() | ||
{ | ||
$context = array( | ||
'providers' => array(), | ||
'formats' => array(), | ||
'download' => array(), | ||
); | ||
|
||
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); | ||
|
||
$contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface'); | ||
|
||
$this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue(null)); | ||
$this->contextManger->expects($this->once())->method('create')->will($this->returnValue($contextModel)); | ||
$this->contextManger->expects($this->once())->method('save')->with($this->equalTo($contextModel)); | ||
|
||
$category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface'); | ||
|
||
$this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue(null)); | ||
$this->categoryManger->expects($this->once())->method('create')->will($this->returnValue($category)); | ||
$this->categoryManger->expects($this->once())->method('save')->with($this->equalTo($category)); | ||
|
||
$output = $this->tester->execute(array()); | ||
|
||
$this->assertRegExp('@ > default category for \'foo\' is missing, creating one\s+Done!@', $this->tester->getDisplay()); | ||
|
||
$this->assertSame(0, $output); | ||
} | ||
} |