Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
add POST method to the API to create a message
Browse files Browse the repository at this point in the history
  • Loading branch information
bdejacobet committed Feb 2, 2015
1 parent 3ca330e commit a87ab52
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 4 deletions.
62 changes: 60 additions & 2 deletions Controller/Api/MessageController.php
Expand Up @@ -13,11 +13,17 @@

use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View as FOSRestView;

use JMS\Serializer\SerializationContext;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormFactoryInterface;

use Sonata\NotificationBundle\Model\Message;
use Sonata\NotificationBundle\Model\MessageInterface;
use Sonata\NotificationBundle\Model\MessageManagerInterface;

/**
Expand All @@ -34,14 +40,21 @@ class MessageController
*/
protected $messageManager;

/**
* @var FormFactoryInterface
*/
protected $formFactory;

/**
* Constructor
*
* @param MessageManagerInterface $messageManager
* @param FormFactoryInterface $formFactory
*/
public function __construct(MessageManagerInterface $messageManager)
public function __construct(MessageManagerInterface $messageManager, FormFactoryInterface $formFactory)
{
$this->messageManager = $messageManager;
$this->formFactory = $formFactory;
}

/**
Expand Down Expand Up @@ -92,6 +105,51 @@ public function getMessagesAction(ParamFetcherInterface $paramFetcher)
return $this->getMessageManager()->getPager($criteria, $page, $limit, $sort);
}

/**
* Adds a message
*
* @ApiDoc(
* input={"class"="sonata_notification_api_form_message", "name"="", "groups"={"sonata_api_write"}},
* output={"class"="Sonata\NotificationBundle\Model\Message", "groups"={"sonata_api_read"}},
* statusCodes={
* 200="Returned when successful",
* 400="Returned when an error has occurred while message creation"
* }
* )
*
* @Route(requirements={"_format"="json|xml"})
*
* @param Request $request A Symfony request
*
* @return MessageInterface
*/
public function postMessageAction(Request $request)
{
$message = null;

$form = $this->formFactory->createNamed(null, 'sonata_notification_api_form_message', $message, array(
'csrf_protection' => false,
));

$form->bind($request);

if ($form->isValid()) {
$message = $form->getData();

$this->messageManager->save($message);

$view = FOSRestView::create($message);
$serializationContext = SerializationContext::create();
$serializationContext->setGroups(array('sonata_api_read'));
$serializationContext->enableMaxDepthChecks();
$view->setSerializationContext($serializationContext);

return $view;
}

return $form;
}

/**
* @return MessageManagerInterface
*/
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SonataNotificationExtension.php
Expand Up @@ -55,6 +55,7 @@ public function load(array $configs, ContainerBuilder $container)

if (isset($bundles['FOSRestBundle']) && isset($bundles['NelmioApiDocBundle'])) {
$loader->load('api_controllers.xml');
$loader->load('api_form.xml');
}

if ($config['admin']['enabled'] && isset($bundles['SonataDoctrineORMAdminBundle'])) { // for now, only support for ORM
Expand Down
1 change: 1 addition & 0 deletions Resources/config/api_controllers.xml
Expand Up @@ -7,6 +7,7 @@
<services>
<service id="sonata.notification.controller.api.message" class="Sonata\NotificationBundle\Controller\Api\MessageController" >
<argument type="service" id="sonata.notification.manager.message" />
<argument type="service" id="form.factory" />
</service>
</services>
</container>
19 changes: 19 additions & 0 deletions Resources/config/api_form.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="sonata.notification.api.form.type.message" class="Sonata\CoreBundle\Form\Type\DoctrineORMSerializationType">
<tag name="form.type" alias="sonata_notification_api_form_message" />

<argument type="service" id="jms_serializer.metadata_factory" />
<argument type="service" id="doctrine" />
<argument>sonata_notification_api_form_message</argument>
<argument>%sonata.notification.admin.message.entity%</argument>
<argument>sonata_api_write</argument>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Resources/config/serializer/Model.Message.xml
Expand Up @@ -3,7 +3,7 @@
<class name="Sonata\NotificationBundle\Model\Message" exclusion-policy="all" xml-root-name="message">

<property name="type" type="string" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write" />
<property name="body" type="string" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write" />
<property name="body" type="array" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write" />

<property name="state" type="integer" expose="true" since-version="1.0" groups="sonata_api_read" />
<property name="restartCount" type="integer" expose="true" since-version="1.0" groups="sonata_api_read" />
Expand Down
18 changes: 18 additions & 0 deletions Resources/config/validation.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">


<class name="Sonata\NotificationBundle\Model\Message">
<property name="type">
<constraint name="NotBlank" />
</property>

<property name="body">
<constraint name="NotBlank" />
</property>
</class>

</constraint-mapping>
41 changes: 40 additions & 1 deletion Tests/Controller/Api/MessageControllerTest.php
Expand Up @@ -10,7 +10,7 @@


namespace Sonata\NotificationBundle\Tests\Controller\Api;

use Symfony\Component\HttpFoundation\Request;
use Sonata\NotificationBundle\Controller\Api\MessageController;


Expand All @@ -35,6 +35,45 @@ public function testGetMessagesAction()
$this->assertEquals(array(), $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher));
}

public function testPostMessageAction()
{
$message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface');

$messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
$messageManager->expects($this->once())->method('save')->will($this->returnValue($message));

$form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
$form->expects($this->once())->method('bind');
$form->expects($this->once())->method('isValid')->will($this->returnValue(true));
$form->expects($this->once())->method('getData')->will($this->returnValue($message));

$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));

$view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());

$this->assertInstanceOf('FOS\RestBundle\View\View', $view);
}

public function testPostMessageInvalidAction()
{
$message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface');

$messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
$messageManager->expects($this->never())->method('save')->will($this->returnValue($message));

$form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
$form->expects($this->once())->method('bind');
$form->expects($this->once())->method('isValid')->will($this->returnValue(false));

$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));

$view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());

$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
}

/**
* @param $message
* @param $messageManager
Expand Down

0 comments on commit a87ab52

Please sign in to comment.