mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-03-12 02:26:09 +03:00
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:
parent
b2adddecd3
commit
c72085a507
@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Annotation\Model;
|
||||
use Nelmio\ApiDocBundle\Annotation\Operation;
|
||||
use Nelmio\ApiDocBundle\Annotation\Security;
|
||||
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\Form\DummyType;
|
||||
use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType;
|
||||
@ -168,4 +169,16 @@ class ApiController
|
||||
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()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
|
||||
|
||||
use JMS\Serializer\Annotation as Serializer;
|
||||
use Swagger\Annotations as SWG;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* User.
|
||||
@ -28,8 +27,6 @@ class JMSUser
|
||||
* @Serializer\Groups({"list"})
|
||||
*
|
||||
* @SWG\Property(description = "User id", readOnly = true, title = "userid", example=1, default = null)
|
||||
*
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
private $id;
|
||||
|
||||
|
140
Tests/Functional/Entity/SymfonyConstraints.php
Normal file
140
Tests/Functional/Entity/SymfonyConstraints.php
Normal 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;
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||
|
||||
use EXSyst\Component\Swagger\Operation;
|
||||
use EXSyst\Component\Swagger\Tag;
|
||||
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
|
||||
|
||||
@ -256,4 +255,50 @@ class FunctionalTest extends WebTestCase
|
||||
];
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
@ -67,9 +67,6 @@ class JMSFunctionalTest extends WebTestCase
|
||||
'type' => 'date',
|
||||
],
|
||||
],
|
||||
'required' => [
|
||||
'id',
|
||||
],
|
||||
], $this->getModel('JMSUser')->toArray());
|
||||
}
|
||||
|
||||
@ -78,7 +75,7 @@ class JMSFunctionalTest extends WebTestCase
|
||||
$this->assertEquals([
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'id' => ['type' => 'integer'],
|
||||
'id' => ['type' => 'integer'],
|
||||
'user' => ['$ref' => '#/definitions/JMSUser2'],
|
||||
'name' => ['type' => 'string'],
|
||||
],
|
||||
@ -99,9 +96,6 @@ class JMSFunctionalTest extends WebTestCase
|
||||
'example' => '1',
|
||||
],
|
||||
],
|
||||
'required' => [
|
||||
'id',
|
||||
],
|
||||
], $this->getModel('JMSUser2')->toArray());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user