Skip to content
Browse files

[Tests] Covered uniqueEntity

  • Loading branch information...
1 parent 790cfab commit 7666efc3d74bf0625fba0331ec0b0132f1da2a27 @recipe recipe committed Aug 18, 2012
View
3 ChangeLog.md
@@ -1,4 +1,7 @@
#2012-08-16
+- [Tests] Covered uniqueEntity
+
+#2012-08-16
- Implemented phpunit tests. Fixed composer for travis integration.
- Fixed issue #13. Fields with property_path = false must be excluded from validation.
- Fixed issue #16. No need to supply second parameter to createForm controller function.
View
43 Tests/Fixtures/LoadUserData.php
@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * This file is part of the JsFormValidationBundle.
+ *
+ * (c) Abhoryo <abhoryo@free.fr>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace APY\JsFormValidationBundle\Tests\Fixtures;
+
+use Doctrine\ORM\Mapping\ClassMetadata;
+use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Entity\User;
+use Doctrine\ORM\EntityManager;
+use Doctrine\Common\DataFixtures\FixtureInterface;
+use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
+use Doctrine\Common\Persistence\ObjectManager;
+
+/**
+ * Fixture loader
+ *
+ * @author Vitaliy Demidov <zend@i.ua>
+ * @since 18 Aug 2012
+ */
+class LoadUserData implements FixtureInterface {
+
+ public function load(ObjectManager $manager)
+ {
+ $user = new User();
+ $user->setId(1);
+ $user->setUsername('taken');
+ $manager->persist($user);
+
+ $metadataUser = $manager->getClassMetaData(get_class($user));
+ $metadataUser->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
+
+ $manager->flush();
+
+ $metadataUser->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_AUTO);
+ }
+}
View
3 Tests/Functional/AppKernel.php
@@ -29,7 +29,6 @@ class AppKernel extends Kernel
public function __construct($config)
{
-
$this->rootDir = __DIR__ . DIRECTORY_SEPARATOR ;
parent::__construct('test', true);
@@ -53,12 +52,14 @@ public function registerBundles()
new \Symfony\Bundle\TwigBundle\TwigBundle(),
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
+ new \Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new \JMS\AopBundle\JMSAopBundle(),
new \JMS\DiExtraBundle\JMSDiExtraBundle($this),
new \Bazinga\ExposeTranslationBundle\BazingaExposeTranslationBundle(),
new TestBundle\TestBundle(),
new \APY\JsFormValidationBundle\APYJsFormValidationBundle(),
+ new \Liip\FunctionalTestBundle\LiipFunctionalTestBundle(),
);
}
View
4 Tests/Functional/BaseTestCase.php
@@ -12,7 +12,7 @@
namespace APY\JsFormValidationBundle\Tests\Functional;
use Symfony\Component\Filesystem\Filesystem;
-use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+use Liip\FunctionalTestBundle\Test\WebTestCase;
/**
* @author Vitaliy Demidov <zend@i.ua>
@@ -58,7 +58,7 @@ protected function setUp()
$fs = new Filesystem();
- $tmpdir = $this->getTemporaryPath();
+ $tmpdir = $this->getTemporaryPath() . DIRECTORY_SEPARATOR . "web";
if (is_dir($tmpdir)) {
$fs->remove($tmpdir);
}
View
86 Tests/Functional/FormValidationTest.php
@@ -62,20 +62,28 @@ public function testSimpleFormAction()
public function testEntityAnnotationFormAction()
{
+ $this->loadFixtures(array('APY\JsFormValidationBundle\Tests\Fixtures\LoadUserData'));
+
$client = $this->createClient(array('config' => 'config.yml'));
$client->insulate();
$crawler = $client->request('GET', '/entity-annotation-form');
- $this->assertEquals(12, $crawler->filter('form input')->count(), "Number of input fields does not match.");
+ $this->assertEquals(15, $crawler->filter('form input')->count(), "Number of input fields does not match.");
$this->assertEquals(16, $crawler->filter('form select')->count(), "Number of select fields does not match.");
- $this->assertEquals(2, $crawler->filter('script')->count(), "Validation scripts have not been generated.");
+ $this->assertEquals(3, $crawler->filter('script')->count(), "Validation scripts have not been generated.");
+ $hiddenIdentifier = $crawler->filter('input#user_jsfv_identifier');
+ $this->assertEquals(1, $hiddenIdentifier->count(),
+ "Cannot find hidden input for identifier value of user form.");
+ $this->assertEquals("[null]", $crawler->filter('input#user_jsfv_identifier')->attr('value'));
$scriptSrc = $crawler->filter('script')->eq(0)->attr('src');
$scriptSrc2 = $crawler->filter('script')->eq(1)->attr('src');
+ $scriptSrc3 = $crawler->filter('script')->eq(2)->attr('src');
$this->assertEquals('/bundles/jsformvalidation/js/entity_annotation_form_form.js', $scriptSrc);
$this->assertEquals('/bundles/jsformvalidation/js/entity_annotation_form_product.js', $scriptSrc2);
+ $this->assertEquals('/bundles/jsformvalidation/js/entity_annotation_form_user.js', $scriptSrc3);
$asseticWriteTo = $this->getKernel()->getCacheDir() . "/../web";
@@ -152,7 +160,79 @@ public function testEntityAnnotationFormAction()
}
}
- file_put_contents('D:\\Downloads\\error.html', $client->getResponse()->getContent());
+ //Form with UniqueEntity constraint
+ $src = $scriptSrc3;
+ $form = 'user';
+ if (file_exists($asseticWriteTo . $src)) {
+ $script = file_get_contents($asseticWriteTo . $src);
+
+ $this->assertNotEmpty(preg_match('/var[\s]+jsfv[\s]*=[\s]*new[\s]+function/', $script),
+ "Cannot find jsfv initialization in $src.");
+ $this->assertNotEmpty(preg_match('/function[\s]+NotBlank\(/', $script),
+ "Cannot find NotBlank validator in $src.");
+ $this->assertNotEmpty(preg_match('/function[\s]+UniqueEntity\(/', $script),
+ "Cannot find UniqueEntity validator in $src.");
+
+ $this->assertNotEmpty(preg_match('/check_' . $form . '_username\:[\s]*function\(/', $script),
+ "Cannot find username validation in $src.");
+ $this->assertNotEmpty(preg_match('/checkError\(\'' . $form . '_username\',[\s]*NotBlank,/', $script),
+ "Cannot find checkError username NotBlank in $src.");
+ $this->assertNotEmpty(preg_match('/checkError\(\'' . $form . '_username\',[\s]*UniqueEntity,/', $script),
+ "Cannot find checkError username UniqueEntity in $src.");
+ $this->assertNotEmpty(preg_match('/UniqueEntity,[\s]*'
+ . '\{entity\:"APY[\\\\]+JsFormValidationBundle[\\\\]+Tests[\\\\]+Functional[\\\\]+TestBundle[\\\\]+Entity[\\\\]+User"/', $script),
+ "[UniqueEntity] Invalid value for 'entity' parameter in $src.");
+ $this->assertNotEmpty(preg_match('/identifier_field_id\:"user_jsfv_identifier"/', $script),
+ "[UniqueEntity] Invalid value for 'identifier_field_id' parameter in $src.");
+ $this->assertNotEmpty(preg_match('/message\:"Username you want is already taken."/', $script),
+ "[UniqueEntity] Invalid value for 'message' parameter in $src.");
+ $this->assertNotEmpty(preg_match('/fields\:"username",/', $script),
+ "[UniqueEntity] Invalid value for 'fields' parameter in $src.");
+
+ unset($script);
+ } else {
+ $this->assertFalse(true, "Generated javascript does not exist.");
+ }
+ }
+
+ public function providerUniqueEntityAction()
+ {
+ $entityName = 'APY\\JsFormValidationBundle\\Tests\\Functional\\TestBundle\\Entity\\User';
+ return array(
+ array(array(
+ 'entity' => $entityName,
+ 'value' => 'free',
+ 'target' => 'username',
+ 'ignore' => '[null]'
+ ), 'true'),
+ array(array(
+ 'entity' => $entityName,
+ 'value' => 'taken',
+ 'target' => 'username',
+ 'ignore' => '[null]'
+ ), 'false'),
+ array(array(
+ 'entity' => $entityName,
+ 'value' => 'taken',
+ 'target' => 'username',
+ 'ignore' => '[1]'
+ ), 'true'),
+ );
+ }
+
+ /**
+ * @dataProvider providerUniqueEntityAction
+ */
+ public function testUniqueEntityAction($value, $result)
+ {
+ $this->loadFixtures(array('APY\JsFormValidationBundle\Tests\Fixtures\LoadUserData'));
+
+ $client = $this->createClient(array('config' => 'config.yml'));
+ $client->insulate();
+
+ $client->request('POST', '/jsfv/unique-entity.en.json', $value);
+ $this->assertRegExp('/"isUnique"\:' . $result . '/', $client->getResponse()->getContent(),
+ "Wrong response from apy_js_form_validation_unique_entity.");
}
}
View
8 Tests/Functional/TestBundle/Controller/DefaultController.php
@@ -11,8 +11,10 @@
namespace APY\JsFormValidationBundle\Tests\Functional\TestBundle\Controller;
-use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Form\Type\ProductType;
use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Entity\Product;
+use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Entity\User;
+use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Form\Type\ProductType;
+use APY\JsFormValidationBundle\Tests\Functional\TestBundle\Form\Type\UserType;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
@@ -95,9 +97,13 @@ public function entityAnnotationFormAction(Request $request)
//Creates Form without second argument!
$form2 = $this->createForm(new ProductType());
+ //UniqueEntity
+ $form3 = $this->createForm(new UserType(), new User());
+
return array(
'form' => $form->createView(),
'form2' => $form2->createView(),
+ 'form3' => $form3->createView(),
);
}
}
View
86 Tests/Functional/TestBundle/Entity/User.php
@@ -0,0 +1,86 @@
+<?php
+
+/*
+ * This file is part of the JsFormValidationBundle.
+ *
+ * (c) Abhoryo <abhoryo@free.fr>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace APY\JsFormValidationBundle\Tests\Functional\TestBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+
+/**
+ * User Entity which is used for testing purposes
+ *
+ * @author Vitaliy Demidov <zend@i.ua>
+ * @since 17 Aug 2012
+ *
+ * @ORM\Table(name="user")
+ * @ORM\Entity()
+ * @UniqueEntity(fields="username", message="Username you want is already taken.")
+ */
+class User
+{
+ /**
+ * @ORM\Id
+ * @ORM\GeneratedValue
+ * @ORM\Column(type="integer")
+ */
+ private $id;
+
+ /**
+ * @Assert\NotBlank()
+ * @ORM\Column(name="username", type="string", length=128, unique = true)
+ */
+ public $username;
+
+ /**
+ * Gets Id
+ *
+ * @return int
+ */
+ public function getId()
+ {
+ $this->id;
+ }
+
+ /**
+ * Sets id
+ *
+ * @param int $id
+ * @throws \RuntimeException
+ */
+ public function setId($id)
+ {
+ if (!empty($this->id)) {
+ throw new \RuntimeException("Only updates allowed.");
+ }
+ $this->id = $id;
+ }
+
+ /**
+ * Gets username
+ *
+ * @return string
+ */
+ public function getUsername()
+ {
+ return $this->username;
+ }
+
+ /**
+ * Sets username
+ *
+ * @param string $username
+ */
+ public function setUsername($username)
+ {
+ $this->username = $username;
+ }
+}
View
57 Tests/Functional/TestBundle/Form/Type/UserType.php
@@ -0,0 +1,57 @@
+<?php
+
+/*
+ * This file is part of the JsFormValidationBundle.
+ *
+ * (c) Abhoryo <abhoryo@free.fr>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace APY\JsFormValidationBundle\Tests\Functional\TestBundle\Form\Type;
+
+use Symfony\Component\Form\FormView;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+/**
+ * User Entity FormType which is used for UniqueEntity testing
+ *
+ * @author Vitaliy Demidov <zend@i.ua>
+ * @since 18 Aug 2012
+ */
+class UserType extends AbstractType
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(FormBuilderInterface $builder, array $options)
+ {
+ $builder
+ ->add('username', 'text')
+ ;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
+ {
+ $resolver->setDefaults(array(
+ 'data_class' => 'APY\JsFormValidationBundle\Tests\Functional\TestBundle\Entity\User',
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @see Symfony\Component\Form.FormTypeInterface::getName()
+ */
+ public function getName()
+ {
+ return 'user';
+ }
+}
View
15 Tests/Functional/TestBundle/Resources/config/config.yml
@@ -16,6 +16,9 @@ framework:
router:
resource: %kernel.root_dir%/TestBundle/Resources/config/routing.yml
+liip_functional_test:
+ cache_sqlite_db: true
+
# Assetic Configuration
assetic:
debug: %kernel.debug%
@@ -28,13 +31,11 @@ assetic:
# Doctrine Configuration
doctrine:
dbal:
- driver: pdo_mysql
- host: localhost
- port: ""
- dbname: ""
- user: root
- password: ""
- charset: UTF8
+ default_connection: default
+ connections:
+ default:
+ driver: pdo_sqlite
+ path: %kernel.cache_dir%/test.db
orm:
auto_generate_proxy_classes: %kernel.debug%
View
14 Tests/Functional/TestBundle/Resources/views/Default/entityAnnotationForm.html.twig
@@ -29,4 +29,18 @@
{{ JSFV(form2) }}
{% endif -%}
+ {% if form3 is defined -%}
+ <form id="{{ form3.vars.id }}" action="{{ path("entity_annotation_form") }}"
+ method="post" class="form-horizontal" {{ form_enctype(form3) }}>
+ <fieldset>
+ {{ form_errors(form3) }}
+ {{ form_rest(form3) }}
+ <div class="form-actions">
+ <button type="submit" class="btn btn-primary">Save</button>
+ </div>
+ </fieldset>
+ </form>
+ {{ JSFV(form3) }}
+ {% endif -%}
+
{% endblock %}
View
8 composer.json
@@ -8,6 +8,10 @@
{
"name": "Yoann Petit",
"email": "abhoryo@free.fr"
+ },
+ {
+ "name": "Community contributions",
+ "homepage": "https://github.com/Abhoryo/APYJsFormValidationBundle/graphs/contributors"
}
],
"require": {
@@ -19,6 +23,7 @@
"symfony/assetic-bundle": "2.*",
"doctrine/orm": "*",
"doctrine/doctrine-bundle": "*",
+ "doctrine/doctrine-fixtures-bundle": "*",
"symfony/filesystem": "2.*",
"sensio/framework-extra-bundle": "*",
"symfony/css-selector": "2.*",
@@ -34,7 +39,8 @@
"sensio/framework-extra-bundle": "*",
"jms/di-extra-bundle": "*",
"jms/aop-bundle": "*",
- "willdurand/expose-translation-bundle": "master-dev"
+ "willdurand/expose-translation-bundle": "master-dev",
+ "liip/functional-test-bundle": "*"
},
"autoload": {
"psr-0": { "APY\\JsFormValidationBundle": "" }

0 comments on commit 7666efc

Please sign in to comment.
Something went wrong with that request. Please try again.