Skip to content
This repository was archived by the owner on Apr 30, 2019. It is now read-only.

Commit 1d29614

Browse files
committed
Complete full stack integration tests with advanced converter usage
1 parent 1ba8bca commit 1d29614

File tree

11 files changed

+243
-86
lines changed

11 files changed

+243
-86
lines changed

tests/Fixtures/Integration/Symfony/TestBundle/Controller/CartController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ public function addItemAction(Request $request)
3232
return Response::create($this->getDump($command));
3333
}
3434

35-
return $this->render('TestBundle:cart:add_item_to_cart.html.twig', [
35+
return $this->render('TestBundle:cart:add_item.html.twig', [
3636
'form' => $form->createView(),
37-
]);
37+
], Response::create(
38+
null,
39+
$form->isSubmitted() && !$form->isValid() ? Response::HTTP_BAD_REQUEST : Response::HTTP_OK
40+
));
3841
}
3942
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "elao/form-simple-object-mapper" package.
5+
*
6+
* Copyright (C) 2016 Elao
7+
*
8+
* @author Elao <contact@elao.com>
9+
*/
10+
11+
namespace Elao\FormSimpleObjectMapper\Tests\Fixtures\Integration\Symfony\TestBundle\Controller;
12+
13+
use Elao\FormSimpleObjectMapper\Tests\Fixtures\Media\Book;
14+
use Elao\FormSimpleObjectMapper\Tests\Fixtures\Media\Media;
15+
use Elao\FormSimpleObjectMapper\Tests\Fixtures\Media\MediaConverter;
16+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
20+
21+
class MediaController extends Controller
22+
{
23+
use VarDumperTestTrait;
24+
25+
public function editAction(Request $request)
26+
{
27+
$media = new Book('John Doe');
28+
29+
$builder = $this
30+
->createFormBuilder($media, [
31+
'data_class' => Media::class,
32+
'simple_object_mapper' => new MediaConverter(),
33+
])
34+
->add('mediaType')
35+
->add('author')
36+
;
37+
38+
$form = $builder->getForm();
39+
40+
$form->handleRequest($request);
41+
42+
if ($form->isSubmitted() && $form->isValid()) {
43+
$command = $form->getData();
44+
45+
return Response::create($this->getDump($command));
46+
}
47+
48+
return $this->render('TestBundle:media:edit.html.twig', [
49+
'form' => $form->createView(),
50+
], Response::create(
51+
null,
52+
$form->isSubmitted() && !$form->isValid() ? Response::HTTP_BAD_REQUEST : Response::HTTP_OK
53+
));
54+
}
55+
}

tests/Fixtures/Integration/Symfony/TestBundle/Resources/views/cart/add_item_to_cart.html.twig renamed to tests/Fixtures/Integration/Symfony/TestBundle/Resources/views/cart/add_item.html.twig

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ form(form) }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
add_item_to_cart:
22
path: '/cart/add-item'
33
defaults: { _controller: 'TestBundle:Cart:addItem' }
4+
5+
edit_media:
6+
path: '/media/edit'
7+
defaults: { _controller: 'TestBundle:Media:edit' }

tests/Fixtures/Media/Book.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "elao/form-simple-object-mapper" package.
5+
*
6+
* Copyright (C) 2016 Elao
7+
*
8+
* @author Elao <contact@elao.com>
9+
*/
10+
11+
namespace Elao\FormSimpleObjectMapper\Tests\Fixtures\Media;
12+
13+
class Book extends Media
14+
{
15+
}

tests/Fixtures/Media/Media.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "elao/form-simple-object-mapper" package.
5+
*
6+
* Copyright (C) 2016 Elao
7+
*
8+
* @author Elao <contact@elao.com>
9+
*/
10+
11+
namespace Elao\FormSimpleObjectMapper\Tests\Fixtures\Media;
12+
13+
abstract class Media
14+
{
15+
private $author;
16+
17+
public function __construct($author)
18+
{
19+
$this->author = $author;
20+
}
21+
22+
public function getAuthor()
23+
{
24+
return $this->author;
25+
}
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "elao/form-simple-object-mapper" package.
5+
*
6+
* Copyright (C) 2016 Elao
7+
*
8+
* @author Elao <contact@elao.com>
9+
*/
10+
11+
namespace Elao\FormSimpleObjectMapper\Tests\Fixtures\Media;
12+
13+
use Elao\FormSimpleObjectMapper\DataMapper\FormDataToObjectConverterInterface;
14+
use Elao\FormSimpleObjectMapper\DataMapper\ObjectToFormDataConverterInterface;
15+
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
17+
class MediaConverter implements FormDataToObjectConverterInterface, ObjectToFormDataConverterInterface
18+
{
19+
public function convertFormDataToObject(array $data, $originalData = null)
20+
{
21+
$author = $data['author'];
22+
23+
switch ($data['mediaType']) {
24+
case 'movie':
25+
return new Movie($author);
26+
case 'book':
27+
return new Book($author);
28+
default:
29+
throw new TransformationFailedException();
30+
}
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*
36+
* @param Media|null $object
37+
*/
38+
public function convertObjectToFormData($object)
39+
{
40+
if (null === $object) {
41+
return [];
42+
}
43+
44+
$mediaTypeByClass = [
45+
Movie::class => 'movie',
46+
Book::class => 'book',
47+
];
48+
49+
if (!isset($mediaTypeByClass[get_class($object)])) {
50+
throw new TransformationFailedException();
51+
}
52+
53+
return [
54+
'mediaType' => $mediaTypeByClass[get_class($object)],
55+
'author' => $object->getAuthor(),
56+
];
57+
}
58+
}

tests/Fixtures/Media/Movie.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "elao/form-simple-object-mapper" package.
5+
*
6+
* Copyright (C) 2016 Elao
7+
*
8+
* @author Elao <contact@elao.com>
9+
*/
10+
11+
namespace Elao\FormSimpleObjectMapper\Tests\Fixtures\Media;
12+
13+
class Movie extends Media
14+
{
15+
}

tests/Integration/SymfonyFullStackIntegrationTest.php

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010

1111
namespace Elao\FormSimpleObjectMapper\Tests\Integration;
1212

13+
use Symfony\Bundle\FrameworkBundle\Client;
1314
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
1617

1718
class SymfonyFullStackIntegrationTest extends WebTestCase
1819
{
19-
public function testAddItemAction()
20+
/** @var Client */
21+
private $client;
22+
23+
protected function setUp()
24+
{
25+
$this->client = static::createClient();
26+
}
27+
28+
public function testAddItemToCartAction()
2029
{
21-
$client = static::createClient();
22-
$crawler = $client->request(Request::METHOD_GET, '/cart/add-item');
23-
$response = $client->getResponse();
30+
$crawler = $this->client->request(Request::METHOD_GET, '/cart/add-item');
31+
$response = $this->client->getResponse();
2432

2533
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
2634
$this->assertCount(1, $crawler->filter('form input[name="add_item_to_cart[quantity]"][type="number"]'));
@@ -30,8 +38,8 @@ public function testAddItemAction()
3038
$form['add_item_to_cart[reference]'] = 'A000012';
3139
$form['add_item_to_cart[quantity]'] = 3;
3240

33-
$client->submit($form);
34-
$response = $client->getResponse();
41+
$this->client->submit($form);
42+
$response = $this->client->getResponse();
3543

3644
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
3745
$this->assertSame(<<<'DUMP'
@@ -43,27 +51,62 @@ public function testAddItemAction()
4351
, $response->getContent());
4452
}
4553

46-
public function testAddItemActionFormError()
54+
public function testAddItemToCartActionFormError()
55+
{
56+
$crawler = $this->client->request(Request::METHOD_POST, '/cart/add-item', [
57+
'add_item_to_cart' => [
58+
'reference' => 'A000012',
59+
'quantity' => 'invalid_number',
60+
],
61+
]);
62+
$response = $this->client->getResponse();
63+
64+
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
65+
$this->assertContains(
66+
'This value is not valid.',
67+
$crawler->filter('form input[name="add_item_to_cart[quantity]"][type="number"]')->parents()->html()
68+
);
69+
}
70+
71+
public function testEditMediaAction()
4772
{
48-
$client = static::createClient();
49-
$crawler = $client->request(Request::METHOD_GET, '/cart/add-item');
50-
$response = $client->getResponse();
73+
$crawler = $this->client->request(Request::METHOD_GET, '/media/edit');
74+
$response = $this->client->getResponse();
5175

5276
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
53-
$this->assertCount(1, $crawler->filter('form input[name="add_item_to_cart[quantity]"][type="number"]'));
54-
$this->assertCount(1, $crawler->filter('form input[name="add_item_to_cart[reference]"][type="hidden"]'));
77+
$this->assertCount(1, $crawler->filter('form input[name="form[author]"]'));
78+
$this->assertCount(1, $crawler->filter('form input[name="form[mediaType]"]'));
5579

5680
$form = $crawler->filter('form')->form();
57-
$form['add_item_to_cart[reference]'] = 'A000012';
58-
$form['add_item_to_cart[quantity]'] = 'invalid_number';
81+
$form['form[author]'] = 'Sarah Connor';
82+
$form['form[mediaType]'] = 'movie';
5983

60-
$crawler = $client->submit($form);
61-
$response = $client->getResponse();
84+
$this->client->submit($form);
85+
$response = $this->client->getResponse();
6286

6387
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
88+
$this->assertSame(<<<'DUMP'
89+
Elao\FormSimpleObjectMapper\Tests\Fixtures\Media\Movie {
90+
-author: "Sarah Connor"
91+
}
92+
DUMP
93+
, $response->getContent());
94+
}
95+
96+
public function testEditMediaActionFormError()
97+
{
98+
$crawler = $this->client->request(Request::METHOD_POST, '/media/edit', [
99+
'form' => [
100+
'author' => 'Sarah Connor',
101+
'mediaType' => 'undefined',
102+
],
103+
]);
104+
$response = $this->client->getResponse();
105+
106+
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
64107
$this->assertContains(
65108
'This value is not valid.',
66-
$crawler->filter('form input[name="add_item_to_cart[quantity]"][type="number"]')->parents()->html()
109+
$crawler->filter('form > div > ul > li')->html()
67110
);
68111
}
69112
}

0 commit comments

Comments
 (0)