Skip to content

Commit

Permalink
added validation tests + some ui fix + tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
brikou committed Aug 6, 2011
1 parent bbb73ef commit 54fa764
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Entity/Factory/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
;

/**
* @Assert\callback(methods={"isValidCustomer", "pickedOrderItems"})
* @Assert\Callback(methods={"isValidCustomer"})
*/
class OrderFactory
{
Expand Down Expand Up @@ -142,6 +142,7 @@ public function isValidCustomer(ExecutionContext $context)
/**
* @param ExecutionContext $context
* @return void
* @deprecated
*/
public function pickedOrderItems(ExecutionContext $context)
{
Expand Down
23 changes: 16 additions & 7 deletions Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Acme\PizzaBundle\Entity;

use Doctrine\ORM\Mapping as ORM,
Symfony\Component\Validator\Constraints as Assert;
use
Doctrine\ORM\Mapping as ORM,
Symfony\Component\Validator\Constraints as Assert
;

/**
* @ORM\Entity
Expand All @@ -24,20 +26,23 @@ class Order
* @var \DateTime
*
* @ORM\Column(type="datetime")
* @Assert\NotBlank()
*/
protected $date;

/**
* @var \Acme\PizzaBundle\Entity\Customer
*
* @ORM\ManyToOne(targetEntity="Customer", cascade={"persist"})
* @Assert\NotBlank()
*/
protected $customer;

/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="OrderItem", mappedBy="order", cascade={"persist"})
* @Assert\NotBlank()
*/
protected $items;

Expand Down Expand Up @@ -156,8 +161,12 @@ public function set($name, $value)
$this->setDate($value);
break;

case 'id':
$this->setId($value);
case 'customer':
$this->setCustomer($value);
break;

case 'items':
$this->setItems($value);
break;

default:
Expand All @@ -177,12 +186,12 @@ public function set($name, $value)
public function get($name)
{
switch ($name) {
case 'date':
return $this->getDate($value);

case 'id':
return $this->getId($value);

case 'date':
return $this->getDate($value);

default:
throw new \InvalidArgumentException(sprintf('Generic getter for "%s" is not defined', $name));
}
Expand Down
36 changes: 22 additions & 14 deletions Entity/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Acme\PizzaBundle\Entity;

use Doctrine\ORM\Mapping as ORM,
Symfony\Component\Validator\Constraints as Assert;
use
Doctrine\ORM\Mapping as ORM,
Symfony\Component\Validator\Constraints as Assert
;

/**
* @ORM\Entity
Expand All @@ -21,25 +23,27 @@ class OrderItem
protected $id;

/**
* @var \Acme\PizzaBundle\Entity\Order
* @var Order
*
* @ORM\ManyToOne(targetEntity="Order", inversedBy="items")
*/
protected $order;

/**
* @var \Acme\PizzaBundle\Entity\Pizza
* @var Pizza
*
* @ORM\ManyToOne(targetEntity="Pizza")
* @Assert\Type(type="Acme\PizzaBundle\Entity\Pizza", message="You have to pick a pizza from the list")
* @Assert\NotBlank()
*/
protected $pizza;

/**
* @var integer
*
* @ORM\Column(type="integer")
* @Assert\Min(0)
* @Assert\NotBlank()
* @Assert\Min(1)
* @Assert\Type("integer")
*/
protected $count;

Expand All @@ -56,7 +60,7 @@ public function getId()
/**
* Set the related order
*
* @param \Acme\PizzaBundle\Entity\Order $order
* @param Order $order
*/
public function setOrder(Order $order)
{
Expand All @@ -66,7 +70,7 @@ public function setOrder(Order $order)
/**
* Get the related order
*
* @return \Acme\PizzaBundle\Entity\Order
* @return Order
*/
public function getOrder()
{
Expand All @@ -76,7 +80,7 @@ public function getOrder()
/**
* Set the related pizza
*
* @param \Acme\PizzaBundle\Entity\Pizza $pizza
* @param Pizza $pizza
*/
public function setPizza(Pizza $pizza)
{
Expand All @@ -86,7 +90,7 @@ public function setPizza(Pizza $pizza)
/**
* Get the related pizza
*
* @return \Acme\PizzaBundle\Entity\Pizza
* @return Pizza
*/
public function getPizza()
{
Expand Down Expand Up @@ -124,12 +128,16 @@ public function getCount()
public function set($name, $value)
{
switch ($name) {
case 'count':
$this->setCount($value);
case 'order':
$this->setOrder($value);
break;

case 'id':
$this->setId($value);
case 'pizza':
$this->setPizza($value);
break;

case 'count':
$this->setCount($value);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ steps:
```

Now you can run test (assuming that Selenium RC is running `java -jar selenium-server-standalone-2.2.0.jar`)
with `phpunit -c app/ src/Acme/PizzaBundle/Tests/Selenium/`
with `phpunit -c app/ src/Acme/PizzaBundle/Tests/`
If you want you can submit other missing tests.
5 changes: 3 additions & 2 deletions Resources/views/Order/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p>Hello customer! We want to serve you a yummy-yummy Pizza!</p>

<form method="post" action="{{ path("acme_pizza_order_index") }}" {{ form_enctype(form) }}>
<form method="post" action="{{ path("acme_pizza_order_index") }}" {{ form_enctype(form) }} novalidate="novalidate">

{{ form_row(form.known_customer, { "label": "Is known customer?" }) }}
{{ form_row(form.known_phone) }}
Expand All @@ -17,7 +17,7 @@
{% macro prototype(item) %}
<tr>
<td>
{{ form_widget(item.pizza) }}
{{ form_widget(item.pizza, { "empty_value": "" }) }}
{{ form_errors(item.pizza) }}
</td>
<td>
Expand Down Expand Up @@ -54,6 +54,7 @@
</table>

{{ form_rest(form) }}{# form.items's prototype is rendered twice #}
{{ form_errors(form) }}

<input type="submit" />
</form>
Expand Down
21 changes: 21 additions & 0 deletions Tests/Entity/AbstractEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Acme\PizzaBundle\Tests\Entity;

abstract class AbstractEntityTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\Validator\Validator
*/
static protected $validator;

public static function setUpBeforeClass()
{
require_once "{$_SERVER['KERNEL_DIR']}/AppKernel.php";

$kernel = new \AppKernel('test', true);
$kernel->boot();

self::$validator = $kernel->getContainer()->get('validator');
}
}
59 changes: 59 additions & 0 deletions Tests/Entity/CustomerEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Acme\PizzaBundle\Tests\Entity;

use
Acme\PizzaBundle\Entity\Customer
;

class CustomerEntityTest extends AbstractEntityTest
{
public static function provider()
{
$data = array();

$data[] = array(
'properties' => array(
'name' => 'Patricia S. Kemp',
'street' => '3347 Duck Creek Road',
'city' => 'Palo Alto, CA 94306',
'phone' => '650-813-0200',
),
'errors' => array(),
);

$data[] = array(
'properties' => array(),
'errors' => array(
'name' => 'This value should not be blank',
'street' => 'This value should not be blank',
'city' => 'This value should not be blank',
'phone' => 'This value should not be blank',
),
);

return $data;
}

/**
* @dataProvider provider
*/
public function testValidation(array $properties, array $errors)
{
$customer = new Customer();

foreach ($properties as $property => $value) {
$customer->set($property, $value);
}

$violations = self::$validator->validate($customer, array('Customer'));
/* @var $violations \Symfony\Component\Validator\ConstraintViolationList */

$this->assertEquals(count($errors), count($violations), (string) $violations);

foreach ($errors as $property => $message) {
$pattern = sprintf('/\.%s:\s+%s$/m', $property, $message);
$this->assertRegExp($pattern, (string) $violations, $violations);
}
}
}
57 changes: 57 additions & 0 deletions Tests/Entity/OrderEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Acme\PizzaBundle\Tests\Entity;

use
Acme\PizzaBundle\Entity\Order,
Acme\PizzaBundle\Entity\Customer,
Doctrine\Common\Collections\ArrayCollection
;

class OrderEntityTest extends AbstractEntityTest
{
public static function provider()
{
$data = array();

$data[] = array(
'properties' => array(
'date' => new \DateTime(),
'customer' => new Customer(),
'items' => new ArrayCollection(),
),
'errors' => array(),
);

$data[] = array(
'properties' => array(),
'errors' => array(
'customer' => 'This value should not be blank',
),
);

return $data;
}

/**
* @dataProvider provider
*/
public function testValidation(array $properties, array $errors)
{
$order = new Order();

foreach ($properties as $property => $value) {
$order->set($property, $value);
}

$violations = self::$validator->validate($order);
/* @var $violations \Symfony\Component\Validator\ConstraintViolationList */

$this->assertEquals(count($errors), count($violations), (string) $violations);

foreach ($errors as $property => $message) {
$pattern = sprintf('/\.%s:\s+%s$/m', $property, $message);
$this->assertRegExp($pattern, (string) $violations, $violations);
}
}
}
Loading

0 comments on commit 54fa764

Please sign in to comment.