-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerScienceResourceControllerTest.php
More file actions
106 lines (80 loc) · 3.44 KB
/
ComputerScienceResourceControllerTest.php
File metadata and controls
106 lines (80 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\ComputerScienceResource;
use Database\Factories\ComputerScienceResourceFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
use Tests\TestResources\ComputerScienceResourceTestResource;
class ComputerScienceResourceControllerTest extends TestCase
{
use RefreshDatabase;
protected $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
public function test_can_post_resource()
{
$this->actingAs($this->user);
$formData = ComputerScienceResourceTestResource::fake();
Log::debug("Form data: ". json_encode($formData));
$response = $this->postJson(route('resources.store'), $formData);
$response->assertStatus(302); // a redirect after successful creation
$response->assertRedirect();
// Check it is created
$createdResource = ComputerScienceResource::where('name', $formData['name'])->first();
$this->assertNotNull($createdResource);
}
public function test_cannot_post_resource_unauthed()
{
$formData = ComputerScienceResourceTestResource::fake();
$response = $this->postJson(route('resources.store'), $formData);
$response->assertStatus(401); // Auth required
$createdResource = ComputerScienceResource::where('name', $formData['name'])->first();
$this->assertNull($createdResource);
}
public function test_cannot_post_resource_with_long_name()
{
$this->actingAs($this->user);
$formData = ComputerScienceResourceTestResource::fake();
// Set to invalid name
$formData['name'] = str_repeat('0', 101);
$response = $this->postJson(route('resources.store'), $formData);
$response->assertStatus(422); // a fail
}
public function test_cannot_post_resource_with_invalid_fields()
{
$this->actingAs($this->user);
$validData = ComputerScienceResourceTestResource::fake();
$invalidDataSets = [
'name' => str_repeat('a', 101), # Too long
'description' => str_repeat('a', 10001), # Too long
'platforms' => ['invalid_platform'],
'page_url' => 'not-a-url',
'difficulty' => 'invalid_difficulty',
'pricing' => 'invalid_pricing',
'topic_tags' => ['tag1', 'tag2'], // Less than required minimum of 3
'image_url' => 'not-a-url',
'programming_language_tags' => null,
'general_tags' => ['a','a','a'], // Not distinct
];
// Choose from one of the invalid fields
foreach ($invalidDataSets as $field => $invalidValue) {
$testData = $validData;
$testData[$field] = $invalidValue;
$response = $this->postJson(route('resources.store'), $testData);
$this->assertTrue(
$response->status() === 422,
"Failed asserting that the server responded with a 422 status code for invalid '$field'. Response status: " . $response->status()
);
$not_created_resource = ComputerScienceResource::first();
$this->assertNull(
$not_created_resource,
"Failed asserting that a resource with name '{$testData['name']}' was not created in the database. Invalid field being: " . $field
);
}
}
}