Skip to content

Commit bf166bb

Browse files
committed
test(laravel): validation tests #6932
1 parent b039008 commit bf166bb

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/Laravel/Tests/ValidationTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Laravel\Tests;
15+
16+
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17+
use Illuminate\Contracts\Config\Repository;
18+
use Illuminate\Foundation\Application;
19+
use Illuminate\Foundation\Testing\RefreshDatabase;
20+
use Orchestra\Testbench\Concerns\WithWorkbench;
21+
use Orchestra\Testbench\TestCase;
22+
23+
class ValidationTest extends TestCase
24+
{
25+
use ApiTestAssertionsTrait;
26+
use RefreshDatabase;
27+
use WithWorkbench;
28+
29+
/**
30+
* @param Application $app
31+
*/
32+
protected function defineEnvironment($app): void
33+
{
34+
tap($app['config'], function (Repository $config): void {
35+
$config->set('api-platform.formats', ['jsonld' => ['application/ld+json']]);
36+
$config->set('api-platform.docs_formats', ['jsonld' => ['application/ld+json']]);
37+
});
38+
}
39+
40+
public function testValidationCamelCase(): void
41+
{
42+
$data = [
43+
'surName' => '',
44+
];
45+
46+
$response = $this->postJson('/api/issue_6932', $data, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
47+
$response->assertJsonFragment(['violations' => [['propertyPath' => 'surName', 'message' => 'The sur name field is required.']]]); // validate that the name has been converted
48+
$response->assertStatus(422);
49+
}
50+
51+
public function testValidationSnakeCase(): void
52+
{
53+
$data = [
54+
'sur_name' => 'test',
55+
];
56+
57+
$response = $this->postJson('/api/issue_6932', $data, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
58+
$response->assertStatus(422);
59+
}
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Models;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Post;
18+
use Illuminate\Database\Eloquent\Model;
19+
20+
#[ApiResource(
21+
operations: [
22+
new Post(
23+
uriTemplate: '/issue_6932',
24+
rules: [
25+
'sur_name' => 'required',
26+
]
27+
),
28+
],
29+
)]
30+
class Issue6932 extends Model
31+
{
32+
protected $table = 'issue6932';
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
use Illuminate\Database\Migrations\Migration;
15+
use Illuminate\Database\Schema\Blueprint;
16+
use Illuminate\Support\Facades\Schema;
17+
18+
return new class extends Migration {
19+
public function up(): void
20+
{
21+
Schema::create('issue6932', function (Blueprint $table): void {
22+
$table->id();
23+
$table->string('sur_name');
24+
$table->timestamps();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('issue6932');
34+
}
35+
};

0 commit comments

Comments
 (0)