diff --git a/Entity/Factory/OrderFactory.php b/Entity/Factory/OrderFactory.php index 062db7f..f808141 100644 --- a/Entity/Factory/OrderFactory.php +++ b/Entity/Factory/OrderFactory.php @@ -10,7 +10,7 @@ ; /** - * @Assert\callback(methods={"isValidCustomer", "pickedOrderItems"}) + * @Assert\Callback(methods={"isValidCustomer"}) */ class OrderFactory { @@ -142,6 +142,7 @@ public function isValidCustomer(ExecutionContext $context) /** * @param ExecutionContext $context * @return void + * @deprecated */ public function pickedOrderItems(ExecutionContext $context) { diff --git a/Entity/Order.php b/Entity/Order.php index a9b129e..e0166ba 100644 --- a/Entity/Order.php +++ b/Entity/Order.php @@ -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 @@ -24,6 +26,7 @@ class Order * @var \DateTime * * @ORM\Column(type="datetime") + * @Assert\NotBlank() */ protected $date; @@ -31,6 +34,7 @@ class Order * @var \Acme\PizzaBundle\Entity\Customer * * @ORM\ManyToOne(targetEntity="Customer", cascade={"persist"}) + * @Assert\NotBlank() */ protected $customer; @@ -38,6 +42,7 @@ class Order * @var \Doctrine\Common\Collections\ArrayCollection * * @ORM\OneToMany(targetEntity="OrderItem", mappedBy="order", cascade={"persist"}) + * @Assert\NotBlank() */ protected $items; @@ -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: @@ -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)); } diff --git a/Entity/OrderItem.php b/Entity/OrderItem.php index 5b9ab6e..80a8128 100644 --- a/Entity/OrderItem.php +++ b/Entity/OrderItem.php @@ -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 @@ -21,17 +23,17 @@ 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; @@ -39,7 +41,9 @@ class OrderItem * @var integer * * @ORM\Column(type="integer") - * @Assert\Min(0) + * @Assert\NotBlank() + * @Assert\Min(1) + * @Assert\Type("integer") */ protected $count; @@ -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) { @@ -66,7 +70,7 @@ public function setOrder(Order $order) /** * Get the related order * - * @return \Acme\PizzaBundle\Entity\Order + * @return Order */ public function getOrder() { @@ -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) { @@ -86,7 +90,7 @@ public function setPizza(Pizza $pizza) /** * Get the related pizza * - * @return \Acme\PizzaBundle\Entity\Pizza + * @return Pizza */ public function getPizza() { @@ -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: diff --git a/README.md b/README.md index e97939c..7366a84 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Resources/views/Order/index.html.twig b/Resources/views/Order/index.html.twig index 1ca18f5..0f35e02 100644 --- a/Resources/views/Order/index.html.twig +++ b/Resources/views/Order/index.html.twig @@ -8,7 +8,7 @@

Hello customer! We want to serve you a yummy-yummy Pizza!

-
+ {{ form_row(form.known_customer, { "label": "Is known customer?" }) }} {{ form_row(form.known_phone) }} @@ -17,7 +17,7 @@ {% macro prototype(item) %} - {{ form_widget(item.pizza) }} + {{ form_widget(item.pizza, { "empty_value": "" }) }} {{ form_errors(item.pizza) }} @@ -54,6 +54,7 @@ {{ form_rest(form) }}{# form.items's prototype is rendered twice #} + {{ form_errors(form) }}
diff --git a/Tests/Entity/AbstractEntityTest.php b/Tests/Entity/AbstractEntityTest.php new file mode 100644 index 0000000..8a97a0c --- /dev/null +++ b/Tests/Entity/AbstractEntityTest.php @@ -0,0 +1,21 @@ +boot(); + + self::$validator = $kernel->getContainer()->get('validator'); + } +} diff --git a/Tests/Entity/CustomerEntityTest.php b/Tests/Entity/CustomerEntityTest.php new file mode 100644 index 0000000..3c83b8c --- /dev/null +++ b/Tests/Entity/CustomerEntityTest.php @@ -0,0 +1,59 @@ + 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); + } + } +} diff --git a/Tests/Entity/OrderEntityTest.php b/Tests/Entity/OrderEntityTest.php new file mode 100644 index 0000000..dc576c9 --- /dev/null +++ b/Tests/Entity/OrderEntityTest.php @@ -0,0 +1,57 @@ + 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); + } + } +} diff --git a/Tests/Entity/OrderItemEntityTest.php b/Tests/Entity/OrderItemEntityTest.php new file mode 100644 index 0000000..f2645db --- /dev/null +++ b/Tests/Entity/OrderItemEntityTest.php @@ -0,0 +1,83 @@ + array( + 'order' => $order = new Order(), + 'pizza' => $pizza = new Pizza(), + 'count' => 2, + ), + 'errors' => array(), + ); + + $data[] = array( + 'properties' => array(), + 'errors' => array( + //'order' => 'This value should not be blank', + 'pizza' => 'This value should not be blank', + 'count' => 'This value should not be blank', + ), + ); + + $data[] = array( + 'properties' => array( + 'order' => $order, + 'pizza' => $pizza, + 'count' => 0, + ), + 'errors' => array( + 'count' => 'This value should be 1 or more', + ), + ); + + $data[] = array( + 'properties' => array( + 'order' => $order, + 'pizza' => $pizza, + 'count' => 1.5, + ), + 'errors' => array( + 'count' => 'This value should be of type integer', + ), + ); + + return $data; + } + + /** + * @dataProvider provider + */ + public function testValidation(array $properties, array $errors) + { + $item = new OrderItem(); + + foreach ($properties as $property => $value) { + $item->set($property, $value); + } + + $violations = self::$validator->validate($item); + /* @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); + } + } +} diff --git a/Tests/Entity/PizzaEntityTest.php b/Tests/Entity/PizzaEntityTest.php new file mode 100644 index 0000000..b135255 --- /dev/null +++ b/Tests/Entity/PizzaEntityTest.php @@ -0,0 +1,84 @@ + array( + 'name' => 'Big Sicilian', + 'price' => 13.40, + ), + 'errors' => array(), + ); + + $data[] = array( + 'properties' => array( + 'name' => 'Big Sicilian', + 'price' => '13.40', + ), + 'errors' => array(), + ); + + $data[] = array( + 'properties' => array(), + 'errors' => array( + 'name' => 'This value should not be blank', + 'price' => 'This value should not be blank', + ), + ); + + $data[] = array( + 'properties' => array( + 'name' => 'Big', + 'price' => 'azerty', + ), + 'errors' => array( + 'name' => 'This value is too short. It should have 5 characters or more', + 'price' => 'This value should be a valid number', + ), + ); + + $data[] = array( + 'properties' => array( + 'name' => 'Big Sicilian', + 'price' => -13.40, + ), + 'errors' => array( + 'price' => 'This value should be 2 or more', + ), + ); + + return $data; + } + + /** + * @dataProvider provider + */ + public function testValidation(array $properties, array $errors) + { + $pizza = new Pizza(); + + foreach ($properties as $property => $value) { + $pizza->set($property, $value); + } + + $violations = self::$validator->validate($pizza); + /* @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); + } + } +} diff --git a/Tests/Selenium/OrderSeleniumTest.php b/Tests/Selenium/OrderSeleniumTest.php index 063d033..3420d0d 100644 --- a/Tests/Selenium/OrderSeleniumTest.php +++ b/Tests/Selenium/OrderSeleniumTest.php @@ -173,33 +173,6 @@ public function testOrder4CreateWithKnowCustomerButCountIsZero() $this->click("//input[@type='submit']"); $this->waitForPageToLoad(30000); - $this->assertTrue($this->isTextPresent("You have to pick at least one pizza...")); - } - - public function testOrder5CreateWithKnowCustomerButCountIsNegative() - { - $this->markTestSkipped(); - - $order = array( - 'known_phone' => '03.37.63.90.80', - 'items' => array( - array( - 'pizza' => 'Sweet Potato(7.90)', - 'count' => -1, - ), - ), - ); - - $url = $this->router->generate('acme_pizza_order_index'); - - $this->open($url); - $this->click ('order_known_customer'); - $this->type ('order_known_phone', $order['known_phone']); - $this->select('order_items_0_pizza', 'label='.$order['items'][0]['pizza']); - $this->type ('order_items_0_count', $order['items'][0]['count']); - $this->click("//input[@type='submit']"); - $this->waitForPageToLoad(30000); - - $this->assertTrue($this->isTextPresent("You have to pick at least one pizza...")); + $this->assertTrue($this->isTextPresent("This value should not be blank")); } }