Skip to content

Commit

Permalink
test: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Jun 8, 2021
1 parent 3f28328 commit e1e2593
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -650,7 +650,7 @@ jobs:
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run Behat tests
run: vendor/bin/behat --out=std --format=progress --profile=eloquent --no-interaction
run: vendor/bin/behat --out=std --format=progress --profile=${{ matrix.app_env == 'eloquent' && 'eloquent' || 'eloquent-mapped' }} --no-interaction

phpunit-no-deprecations:
name: PHPUnit (PHP ${{ matrix.php }}) (no deprecations)
Expand Down
8 changes: 8 additions & 0 deletions behat.yml.dist
Expand Up @@ -112,6 +112,14 @@ eloquent:
filters:
tags: '@eloquent&&~@!eloquent'

eloquent-mapped:
suites:
default: false
eloquent:
<<: *eloquent-suite
filters:
tags: '@eloquent_mapped&&~@!eloquent_mapped'

default-coverage:
suites:
default: &default-coverage-suite
Expand Down
1 change: 1 addition & 0 deletions features/main/circular_reference.feature
@@ -1,4 +1,5 @@
@!eloquent
@!eloquent_mapped
Feature: Circular references handling
In order to handle circular references
As a developer
Expand Down
1 change: 1 addition & 0 deletions features/main/composite.feature
@@ -1,5 +1,6 @@
@!mongodb
@!eloquent
@!eloquent_mapped
Feature: Retrieve data with Composite identifiers
In order to retrieve relations with composite identifiers
As a client software developer
Expand Down
2 changes: 2 additions & 0 deletions features/main/crud.feature
@@ -1,4 +1,5 @@
@eloquent
@eloquent_mapped
Feature: Create-Retrieve-Update-Delete
In order to use an hypermedia API
As a client software developer
Expand Down Expand Up @@ -103,6 +104,7 @@ Feature: Create-Retrieve-Update-Delete
Then the response status code should be 404

@!eloquent
@!eloquent_mapped
Scenario: Get a collection
When I send a "GET" request to "/dummies"
Then the response status code should be 200
Expand Down
1 change: 1 addition & 0 deletions features/main/custom_identifier.feature
@@ -1,4 +1,5 @@
@eloquent
@eloquent_mapped
Feature: Using custom identifier on resource
In order to use an hypermedia API
As a client software developer
Expand Down
1 change: 1 addition & 0 deletions features/main/patch.feature
@@ -1,4 +1,5 @@
@eloquent
@eloquent_mapped
Feature: Sending PATCH requets
As a client software developer
I need to be able to send partial updates
Expand Down
5 changes: 5 additions & 0 deletions features/main/relation.feature
@@ -1,4 +1,5 @@
@eloquent
@eloquent_mapped
Feature: Relations support
In order to use a hypermedia API
As a client software developer
Expand Down Expand Up @@ -81,6 +82,7 @@ Feature: Relations support

@!mongodb
@!eloquent
@!eloquent_mapped
Scenario: Create a friend relationship
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/related_to_dummy_friends" with body:
Expand Down Expand Up @@ -112,6 +114,7 @@ Feature: Relations support

@!mongodb
@!eloquent
@!eloquent_mapped
Scenario: Get the relationship
When I send a "GET" request to "/related_to_dummy_friends/dummyFriend=1;relatedDummy=1"
And the response status code should be 200
Expand Down Expand Up @@ -400,6 +403,7 @@ Feature: Relations support
"""

@!eloquent
@!eloquent_mapped
Scenario: Issue #1222
Given there are people having pets
When I add "Content-Type" header equal to "application/ld+json"
Expand Down Expand Up @@ -434,6 +438,7 @@ Feature: Relations support
"""

@!eloquent
@!eloquent_mapped
Scenario: Eager load relations should not be duplicated
Given there is an order with same customer and recipient
When I add "Content-Type" header equal to "application/ld+json"
Expand Down
1 change: 1 addition & 0 deletions features/main/validation.feature
@@ -1,3 +1,4 @@
@eloquent_mapped
Feature: Using validations groups
As a client software developer
I need to be able to use validation groups
Expand Down
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Controller\EloquentMapped;

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Resource\DummyValidation;
use Symfony\Component\Routing\Annotation\Route;

class DummyValidationController
{
/**
* @Route(
* methods={"POST"},
* name="post_validation_groups",
* path="/dummy_validation/validation_groups",
* defaults={"_api_resource_class"=DummyValidation::class, "_api_collection_operation_name"="post_validation_groups"}
* )
*/
public function postValidationGroups($data)
{
return $data;
}

/**
* @Route(
* methods={"POST"},
* name="post_validation_sequence",
* path="/dummy_validation/validation_sequence",
* defaults={"_api_resource_class"=DummyValidation::class, "_api_collection_operation_name"="post_validation_sequence"}
* )
*/
public function postValidationSequence($data)
{
return $data;
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/TestBundle/Models/DummyValidation.php
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Models;

use ApiPlatform\Core\Annotation\ApiResource;
use Illuminate\Database\Eloquent\Model;

/**
* @ApiResource(
* collectionOperations={
* "get"={"method"="GET"},
* "post"={"path"="dummy_validation.{_format}", "method"="POST"},
* "post_validation_groups"={"route_name"="post_validation_groups", "validation_groups"={"a"}, "method"="GET"},
* "post_validation_sequence"={"route_name"="post_validation_sequence", "validation_groups"="app.dummy_validation.group_generator", "method"="GET"}
* }
* )
*/
class DummyValidation extends Model
{
public $timestamps = false;

protected $apiProperties = [
'name',
'title',
'code',
];
}
137 changes: 137 additions & 0 deletions tests/Fixtures/TestBundle/Resource/DummyValidation.php
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Resource;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Models\DummyValidation as DummyValidationModel;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource(
* dataModel=DummyValidationModel::class,
* collectionOperations={
* "get"={"method"="GET"},
* "post"={"path"="dummy_validation.{_format}", "method"="POST"},
* "post_validation_groups"={"route_name"="post_validation_groups", "validation_groups"={"a"}, "method"="GET"},
* "post_validation_sequence"={"route_name"="post_validation_sequence", "validation_groups"="app.dummy_validation.group_generator", "method"="GET"}
* }
* )
*/
class DummyValidation
{
/**
* @var int The id
*
* @ApiProperty(identifier=true)
*/
private $id;

/**
* @var string|null The dummy name
*
* @Assert\NotNull(groups={"a"})
*/
private $name;

/**
* @var string|null The dummy title
*
* @Assert\NotNull(groups={"b"})
*/
private $title;

/**
* @var string The dummy code
*/
private $code;

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @return DummyValidation
*/
public function setId(int $id)
{
$this->id = $id;

return $this;
}

/**
* @return string|null
*/
public function getName()
{
return $this->name;
}

/**
* @param string|null $name
*
* @return DummyValidation
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* @return string|null
*/
public function getTitle()
{
return $this->title;
}

/**
* @param string|null $title
*
* @return DummyValidation
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* @return string
*/
public function getCode()
{
return $this->code;
}

/**
* @param string $code
*
* @return DummyValidation
*/
public function setCode($code)
{
$this->code = $code;

return $this;
}
}
4 changes: 4 additions & 0 deletions tests/Fixtures/app/config/routing_eloquent_mapped.yml
@@ -1,2 +1,6 @@
_main:
resource: routing.yml

controller:
resource: '@TestBundle/Controller/EloquentMapped'
type: annotation
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use WouterJ\EloquentBundle\Facade\Schema;

class CreateDummyValidationTable extends Migration
{
public function up(): void
{
Schema::create('dummy_validations', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable(true);
$table->string('title')->nullable(true);
$table->string('code');
});
}

public function down(): void
{
Schema::drop('dummy_validations');
}
}

0 comments on commit e1e2593

Please sign in to comment.