public function testLoad() { $category = $this->getMockBuilder('Sonata\\ClassificationBundle\\Model\\CategoryInterfacer')->setMethods(array('getId'))->disableOriginalConstructor()->getMock(); $category->expects($this->any())->method('getId')->will($this->returnValue(23)); $this->categoryManager->expects($this->any())->method('find')->with($this->equalTo('23'))->will($this->returnValue($category)); $block = $this->getMock('Sonata\\BlockBundle\\Model\\BlockInterface'); $block->expects($this->any())->method('getSetting')->with($this->equalTo('categoryId'))->will($this->returnValue(23)); $block->expects($this->once())->method('setSetting')->with($this->equalTo('categoryId'), $this->equalTo($category)); $blockService = $this->getMockForAbstractClass('Sonata\\ClassificationBundle\\Block\\Service\\AbstractCategoriesBlockService', array('block.service', $this->templating, $this->contextManager, $this->categoryManager, $this->categoryAdmin)); $blockService->load($block); }
/** * @param Options $options * * @return array */ public function getChoices(Options $options) { if (!$options['category'] instanceof CategoryInterface) { return array(); } if ($options['context'] === null) { $categories = $this->manager->getRootCategories(); } else { $categories = array($this->manager->getRootCategory($options['context'])); } $choices = array(); foreach ($categories as $category) { $choices[$category->getId()] = sprintf('%s (%s)', $category->getName(), $category->getContext()->getId()); $this->childWalker($category, $options, $choices); } return $choices; }
/** * @param CategoryInterface|int $id * @param CategoryInterface|null $default * * @return CategoryInterface */ protected final function getCategory($id, CategoryInterface $default = null) { if ($id instanceof CategoryInterface) { return $id; } if (is_numeric($id)) { return $this->categoryManager->find($id); } return $default; }
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); }
/** * Write a category, this method is used by both POST and PUT action methods. * * @param Request $request Symfony request * @param int|null $id A category identifier * * @return View|FormInterface */ protected function handleWriteCategory($request, $id = null) { $category = $id ? $this->getCategory($id) : null; $form = $this->formFactory->createNamed(null, 'sonata_classification_api_form_category', $category, array('csrf_protection' => false)); FormHelper::removeFields($request->request->all(), $form); $form->handleRequest($request); if ($form->isValid()) { $category = $form->getData(); $this->categoryManager->save($category); $view = \FOS\RestBundle\View\View::create($category); $serializationContext = SerializationContext::create(); $serializationContext->setGroups(array('sonata_api_read')); $serializationContext->enableMaxDepthChecks(); $view->setSerializationContext($serializationContext); return $view; } return $form; }
/** * {@inheritdoc} */ public function getNewInstance() { $media = parent::getNewInstance(); if ($this->hasRequest()) { if ($this->getRequest()->isMethod('POST')) { $media->setProviderName($this->getRequest()->get(sprintf('%s[providerName]', $this->getUniqid()), null, true)); } else { $media->setProviderName($this->getRequest()->get('provider')); } $media->setContext($context = $this->getRequest()->get('context')); if ($categoryId = $this->getPersistentParameter('category')) { $category = $this->categoryManager->find($categoryId); if ($category && $category->getContext()->getId() == $context) { $media->setCategory($category); } } } return $media; }