Added test for the Symfony constraints (#1213)

* Added test for the Symfony constraints

* Added missing ,

* Removed whiteline

* Moved test from JMS to FunctionTest

* Style ci changes

* Removed unused code

* Removed use
This commit is contained in:
Martijn van Kempen 2018-02-05 18:39:58 +01:00 committed by Guilhem N
parent b2adddecd3
commit c72085a507
5 changed files with 200 additions and 11 deletions

View File

@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Operation; use Nelmio\ApiDocBundle\Annotation\Operation;
use Nelmio\ApiDocBundle\Annotation\Security; use Nelmio\ApiDocBundle\Annotation\Security;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article; use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraints;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User; use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType; use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType;
@ -168,4 +169,16 @@ class ApiController
public function securityAction() public function securityAction()
{ {
} }
/**
* @Route("/swagger/symfonyConstraints", methods={"GET"})
* @SWG\Response(
* response="201",
* description="Used for symfony constraints test",
* @Model(type=SymfonyConstraints::class)
* )
*/
public function symfonyConstraintsAction()
{
}
} }

View File

@ -13,7 +13,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* User. * User.
@ -28,8 +27,6 @@ class JMSUser
* @Serializer\Groups({"list"}) * @Serializer\Groups({"list"})
* *
* @SWG\Property(description = "User id", readOnly = true, title = "userid", example=1, default = null) * @SWG\Property(description = "User id", readOnly = true, title = "userid", example=1, default = null)
*
* @Assert\NotBlank()
*/ */
private $id; private $id;

View File

@ -0,0 +1,140 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class SymfonyConstraints
{
/**
* @var int
*
* @Assert\NotBlank()
*/
private $propertyNotBlank;
/**
* @var int
*
* @Assert\NotNull()
*/
private $propertyNotNull;
/**
* @var int
*
* @Assert\Length(min="1")
*/
private $propertyAssertLengthRequired;
/**
* @var int
*
* @Assert\Length(min="0", max="50")
*/
private $propertyAssertLengthMinAndMax;
/**
* @var int
*
* @Assert\Regex(pattern="/[a-z]{2}/")
*/
private $propertyRegex;
/**
* @var int
*
* @Assert\Count(min="0", max="10")
*/
private $propertyCount;
/**
* @var int
*
* @Assert\Choice(choices={"choice1", "choice2"})
*/
private $propertyChoice;
/**
* @var int
*
* @Assert\Expression(
* "this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()",
* message="If this is a tech post, the category should be either php or symfony!"
* )
*/
private $propertyExpression;
/**
* @param int $propertyNotBlank
*/
public function setPropertyNotBlank(int $propertyNotBlank): void
{
$this->propertyNotBlank = $propertyNotBlank;
}
/**
* @param int $propertyNotNull
*/
public function setPropertyNotNull(int $propertyNotNull): void
{
$this->propertyNotNull = $propertyNotNull;
}
/**
* @param int $propertyAssertLengthRequired
*/
public function setPropertyAssertLengthRequired(int $propertyAssertLengthRequired): void
{
$this->propertyAssertLengthRequired = $propertyAssertLengthRequired;
}
/**
* @param int $propertyAssertLengthMinAndMax
*/
public function setPropertyAssertLengthMinAndMax(int $propertyAssertLengthMinAndMax): void
{
$this->propertyAssertLengthMinAndMax = $propertyAssertLengthMinAndMax;
}
/**
* @param int $propertyRegex
*/
public function setPropertyRegex(int $propertyRegex): void
{
$this->propertyRegex = $propertyRegex;
}
/**
* @param int $propertyCount
*/
public function setPropertyCount(int $propertyCount): void
{
$this->propertyCount = $propertyCount;
}
/**
* @param int $propertyChoice
*/
public function setPropertyChoice(int $propertyChoice): void
{
$this->propertyChoice = $propertyChoice;
}
/**
* @param int $propertyExpression
*/
public function setPropertyExpression(int $propertyExpression): void
{
$this->propertyExpression = $propertyExpression;
}
}

View File

@ -11,7 +11,6 @@
namespace Nelmio\ApiDocBundle\Tests\Functional; namespace Nelmio\ApiDocBundle\Tests\Functional;
use EXSyst\Component\Swagger\Operation;
use EXSyst\Component\Swagger\Tag; use EXSyst\Component\Swagger\Tag;
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
@ -256,4 +255,50 @@ class FunctionalTest extends WebTestCase
]; ];
$this->assertEquals($expected, $operation->getSecurity()); $this->assertEquals($expected, $operation->getSecurity());
} }
public function testSymfonyConstraintDocumentation()
{
$this->assertEquals([
'required' => [
'propertyNotBlank',
'propertyNotNull',
'propertyAssertLengthRequired',
],
'properties' => [
'propertyNotBlank' => [
'type' => 'integer',
],
'propertyNotNull' => [
'type' => 'integer',
],
'propertyAssertLengthRequired' => [
'type' => 'integer',
'minLength' => '1',
],
'propertyAssertLengthMinAndMax' => [
'type' => 'integer',
'maxLength' => '50',
'minLength' => '0',
],
'propertyRegex' => [
'type' => 'integer',
'pattern' => '.*[a-z]{2}.*',
],
'propertyCount' => [
'type' => 'integer',
'maxItems' => '10',
'minItems' => '0',
],
'propertyChoice' => [
'type' => 'integer',
'enum' => ['choice1', 'choice2'],
],
'propertyExpression' => [
'type' => 'integer',
'pattern' => 'If this is a tech post, the category should be either php or symfony!',
],
],
'type' => 'object',
], $this->getModel('SymfonyConstraints')->toArray());
}
} }

View File

@ -67,9 +67,6 @@ class JMSFunctionalTest extends WebTestCase
'type' => 'date', 'type' => 'date',
], ],
], ],
'required' => [
'id',
],
], $this->getModel('JMSUser')->toArray()); ], $this->getModel('JMSUser')->toArray());
} }
@ -99,9 +96,6 @@ class JMSFunctionalTest extends WebTestCase
'example' => '1', 'example' => '1',
], ],
], ],
'required' => [
'id',
],
], $this->getModel('JMSUser2')->toArray()); ], $this->getModel('JMSUser2')->toArray());
} }