Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

[WIP] first steps to an RESTController by the help of sylius resource-bundle #144

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

2.0.0 (unreleased)
------------------

* **2016-05-05**: REST write and delete support enabled

1.3.0
-----

Expand Down
24 changes: 24 additions & 0 deletions Form/Type/StaticContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Symfony\Cmf\Bundle\ContentBundle\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class StaticContent extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', TextType::class);
$builder->add('body', TextType::class);
}

public function getName()
{
return 'cmf_static_content';
}
}
51 changes: 50 additions & 1 deletion Model/StaticContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Knp\Menu\NodeInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
Expand Down Expand Up @@ -42,7 +43,8 @@ class StaticContent extends StaticContentBase implements
RouteReferrersInterface,
PublishTimePeriodInterface,
PublishableInterface,
TranslatableInterface
TranslatableInterface,
ResourceInterface
{
/**
* @var bool whether this content is publishable
Expand Down Expand Up @@ -308,4 +310,51 @@ public function getMenuNodes()
{
return $this->menuNodes;
}

/**
* Returns a string representation of the Resource.
*
* This method is necessary to allow for resource de-duplication, for example by means
* of array_unique(). The string returned need not have a particular meaning, but has
* to be identical for different ResourceInterface instances referring to the same
* resource; and it should be unlikely to collide with that of other, unrelated
* resource instances.
*
* @return string A string representation unique to the underlying Resource
*/
public function __toString()
{
return $this->getTitle();
}

/**
* Returns true if the resource has not been updated since the given timestamp.
*
* @param int $timestamp The last time the resource was loaded
*
* @return bool True if the resource has not been updated, false otherwise
*
* @deprecated since 2.8, to be removed in 3.0. If your resource can check itself for
* freshness implement the SelfCheckingResourceInterface instead.
*/
public function isFresh($timestamp)
{
return true;
}

/**
* Returns the tied resource.
*
* @return mixed The resource
*
* @deprecated since 2.8, to be removed in 3.0. As there are many different kinds of resource,
* a single getResource() method does not make sense at the interface level. You
* can still call getResource() on implementing classes, probably after performing
* a type check. If you know the concrete type of Resource at hand, the return value
* of this method may make sense to you.
*/
public function getResource()
{
return $this;
}
}
2 changes: 2 additions & 0 deletions Resources/config/serializer/cmf/Model.StaticContent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Symfony\Cmf\Bundle\ContentBundle\Model\StaticContent:
exclusion_policy: ALL
1 change: 1 addition & 0 deletions Resources/views/StaticContent/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INDEX
27 changes: 27 additions & 0 deletions Tests/Resources/DataFixtures/Phpcr/LoadContentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ODM\PHPCR\Document\Generic;
use PHPCR\Util\NodeHelper;
use Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;

class LoadContentData implements FixtureInterface
{
Expand All @@ -29,20 +30,46 @@ public function load(ObjectManager $manager)
$contentRoot->setParent($root);
$manager->persist($contentRoot);

$routeRoot = new Generic();
$routeRoot->setNodename('routes');
$routeRoot->setParent($root);
$manager->persist($routeRoot);

$content = new StaticContent();
$content->setName('content-1');
$content->setTitle('Content 1');
$content->setBody('Content 1');
$content->setParentDocument($contentRoot);
$manager->persist($content);

$route = new Route();
$route->setContent($content);
$route->setParentDocument($routeRoot);
$route->setName('content-1');
$manager->persist($route);

$content = new StaticContent();
$content->setName('content-2');
$content->setTitle('Content 2');
$content->setBody('Content 2');
$content->setParentDocument($contentRoot);
$manager->persist($content);

$collection = new StaticContent();
$collection->setName('collection');
$collection->setTitle('Collection');
$collection->setBody('Body of Collection');
$collection->setParentDocument($contentRoot);
$manager->persist($collection);

$collectionRoute = new Route();
$collectionRoute->setContent($collection);
$collectionRoute->setParentDocument($routeRoot);
$collectionRoute->setName('collection');
$manager->persist($collectionRoute);



$manager->flush();
}
}
4 changes: 4 additions & 0 deletions Tests/Resources/Fixtures/json/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "My new title",
"body": "My new body"
}
4 changes: 4 additions & 0 deletions Tests/Resources/Fixtures/json/put.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "My new title",
"body": "My new body"
}
6 changes: 6 additions & 0 deletions Tests/Resources/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function configure()
new \Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
new \Symfony\Cmf\Bundle\MenuBundle\CmfMenuBundle(),
new \Symfony\Cmf\Bundle\CoreBundle\CmfCoreBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle($this),
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
new \Symfony\Cmf\Bundle\RestBundle\CmfRestBundle(),
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INDEX
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE
26 changes: 26 additions & 0 deletions Tests/Resources/app/config/cmf_content.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,40 @@ cmf_core:
locales: ['en', 'de', 'fr']

cmf_routing:
chain:
routers_by_id:
cmf_routing.dynamic_router: 20
router.default: 100
dynamic:
enabled: true
persistence:
phpcr:
enabled: true
route_basepaths: /test/routes
controllers_by_class:
Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent: cmf_content.rest_controller:getAction

cmf_content:
persistence:
phpcr:
enabled: true
content_basepath: /test/contents

cmf_rest:
dynamic:
enabled: true
crud_controller_by_method: cmf.controller.static_content

sylius_resource:
resources:
cmf.static_content:
driver: doctrine/phpcr-odm
templates: ":StaticContent"
classes:
model: Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent
form:
default: Symfony\Cmf\Bundle\ContentBundle\Form\Type\StaticContent

fos_rest:
format_listener:
enabled: true
7 changes: 4 additions & 3 deletions Tests/Resources/app/config/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
$collection->addCollection(
$loader->import(CMF_TEST_CONFIG_DIR.'/routing/sonata_routing.yml')
);
// $collection->addCollection(
// $loader->import(__DIR__.'/routing/my_test_routing.yml')
// );

$collection->addCollection(
$loader->import(__DIR__.'/routing.yml')
);

return $collection;
4 changes: 4 additions & 0 deletions Tests/Resources/app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmf_static_content_index:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created for the redirect defintion after delete only. Should be done in a kind of an route enhancer to pass the information by class.

path: "/collection"
defaults: {_controller: "cmf.controller.static_content:indexAction" }

77 changes: 77 additions & 0 deletions Tests/WebTest/Controller/RESTContentControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Bundle\ContentBundle\Tests\WebTest\Controller;

use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de>
*/
class RESTContentControllerTest extends BaseTestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RestContent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently not sure if i should switch to behat tests as you did on resource-rest-bundle. So maybe that test will disappear.

{
public function setUp()
{
$this->db('PHPCR')->loadFixtures(array(
'Symfony\Cmf\Bundle\ContentBundle\Tests\Resources\DataFixtures\Phpcr\LoadContentData',
));
$this->client = $this->createClient();
}

public function testGET()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testGet

{
$this->client->request('GET', '/content-1', array(), array(), array('ACCEPT'=> 'application/json'));
$res = $this->client->getResponse();
$this->assertEquals(200, $res->getStatusCode());
}

public function testPUT()
{
$this->client->request(
'PUT',
'/content-1',
array(),
array(),
array('ACCEPT'=> 'application/json', 'CONTENT_TYPE' => 'application/json'),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ACCEPT header isn't currently recoginzed by sylius. even with FOSRest format listener.

file_get_contents(__DIR__ . '/../../Resources/Fixtures/json/put.json')
);
$res = $this->client->getResponse();
$this->assertEquals(Response::HTTP_OK, $res->getStatusCode());
}

public function testDELETE()
{
$this->client->request(
'DELETE',
'/content-1',
array(),
array(),
array('ACCEPT'=> 'application/json')
);
$res = $this->client->getResponse();
$this->assertEquals(Response::HTTP_FOUND, $res->getStatusCode());
}

public function testPOST()
{
$this->client->request(
'POST',
'/collection',
array(),
array(),
array('ACCEPT'=> 'application/json', 'CONTENT_TYPE' => 'application/json'),
file_get_contents(__DIR__ . '/../../Resources/Fixtures/json/post.json')
);
$res = $this->client->getResponse();
$this->assertEquals(Response::HTTP_OK, $res->getStatusCode());
}
}
24 changes: 22 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,34 @@
"homepage": "https://github.com/symfony-cmf/ContentBundle/contributors"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/ElectricMaxxx/RestBundle.git"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used my own simple method aware enhancer in that little test repo.

},
{
"type": "path",
"url": "https://github.com/dantleech/Sylius/tree/phpcrodm_form_builder/src/Sylius/Bundle/ResourceBundle",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My try to get the branch of @dantleech but does not work that way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dantleech you know what would be the right solution. Tried several options:

  • vcs+ "*.git" to get get sylius, to simply copy => version problem as sylius requires ^1.2
  • path + branch path to ResourceBundle (current one) with your branch to overwrite the "sylius/resource-bundle" but does not work do not get your branch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there is one. You can just run the tests locally use a symlink -
if this PR depends on that PR, then it can't be merged until the the
other one is merged anyway :)

On Mon, May 30, 2016 at 06:03:34AM -0700, Maximilian Berghoff wrote:

In [1]composer.json:

@@ -11,17 +11,34 @@
"homepage": "https://github.com/symfony-cmf/ContentBundle/contributors"
}
],

  • "repositories": [
  •    {
    
  •        "type": "vcs",
    
  •        "url": "https://github.com/ElectricMaxxx/RestBundle.git"
    
  •    },
    
  •    {
    
  •        "type": "path",
    
  •        "url": "https://github.com/dantleech/Sylius/tree/phpcrodm_form_builder/src/Sylius/Bundle/ResourceBundle",
    

[2]@dantleech you know what would be the right solution. Tried several
options:

 * vcs+ "*.git" to get get sylius, to simply copy => version problem as
   sylius requires ^1.2
 * path + branch path to ResourceBundle (current one) with your branch to
   overwrite the "sylius/resource-bundle" but does not work do not get
   your branch


You are receiving this because you were mentioned.
Reply to this email directly, [3]view it on GitHub, or [4]mute the thread.

Reverse link: [5]unknown

References

Visible links

  1. [WIP] first steps to an RESTController by the help of sylius resource-bundle #144 (comment)
  2. https://github.com/dantleech
  3. https://github.com/symfony-cmf/content-bundle/pull/144/files/6940d5cac5884ee7818db1346d3ffc8de5ed1f3b#r65068806
  4. https://github.com/notifications/unsubscribe/AAgZceL4GJi_fHkaM9Xp72wNi9_5CNvVks5qGuAmgaJpZM4IWrft
  5. https://github.com/symfony-cmf/content-bundle/pull/144/files/6940d5cac5884ee7818db1346d3ffc8de5ed1f3b#r65068806

"options": {
"symlink": false
}
}
],
"minimum-stability": "dev",
"require": {
"php": "^5.3.9|^7.0",
"symfony/framework-bundle": "^2.3|3.*",
"symfony-cmf/core-bundle": "^1.1",
"symfony-cmf/menu-bundle": "^1.1|2.*",
"symfony-cmf/routing-bundle": "^1.2"
"symfony-cmf/routing-bundle": "^1.2",
"sylius/resource-bundle": "^0.18",
"doctrine/orm": "^2.4.8,<2.5"
},
"require-dev": {
"symfony-cmf/testing": "^1.3",
"sonata-project/doctrine-phpcr-admin-bundle": "^1.1",
"symfony/monolog-bundle": "^2.3"
"symfony/monolog-bundle": "^2.3",
"electricmaxxx/rest-bundle":"dev-master"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be removed, when routing is ready to do its job.

},
"suggest": {
"friendsofsymfony/rest-bundle": "Improved handling for different output formats",
Expand All @@ -39,5 +56,8 @@
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"scripts": {
"post:update":"rm -rf vendor/sylius/resource-bundle/"
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<testsuites>
<testsuite name="phpcr">
<directory>./Tests/Functional</directory>
<directory>./Tests/WebTest</directory>
</testsuite>

<testsuite name="unit">
Expand Down