Newer
Older
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
107
108
109
<?php
declare(strict_types=1);
namespace modules\Api\Rest\V1;
use App\Database\Seeds\FakeSinglePodcastApiSeeder;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
class PodcastTest extends CIUnitTestCase
{
use FeatureTestTrait;
use DatabaseTestTrait;
/**
* @var bool
*/
protected $migrate = true;
/**
* @var bool
*/
protected $migrateOnce = false;
/**
* @var string|null
*/
protected $namespace;
/**
* @var string
*/
protected $seed = 'FakeSinglePodcastApiSeeder';
/**
* @var string
*/
protected $basePath = 'app/Database';
/**
* @var array<mixed>
*/
private array $podcast = [];
private string $podcastApiUrl;
/**
* @param array<mixed> $data
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->podcast = FakeSinglePodcastApiSeeder::podcast();
$this->podcast['created_at'] = [];
$this->podcast['updated_at'] = [];
$this->podcastApiUrl = config('Api')
->gateway;
}
public function testList(): void
{
$result = $this->call('get', $this->podcastApiUrl . 'podcasts');
$result->assertStatus(200);
$result->assertHeader('Content-Type', 'application/json; charset=UTF-8');
$result->assertJSONFragment([
0 => $this->podcast,
]);
}
public function testView(): void
{
$result = $this->call('get', $this->podcastApiUrl . 'podcasts/1');
$result->assertStatus(200);
$result->assertHeader('Content-Type', 'application/json; charset=UTF-8');
$result->assertJSONFragment($this->podcast);
}
public function testViewNotFound(): void
{
$result = $this->call('get', $this->podcastApiUrl . 'podcasts/2');
$result->assertStatus(404);
$result->assertJSONExact(
[
'status' => 404,
'error' => 404,
'messages' => [
'error' => 'Podcast not found',
],
]
);
$result->assertHeader('Content-Type', 'application/json; charset=UTF-8');
}
/*
* Refreshing database to fetch empty array of podcasts
*/
public function testListEmpty(): void
{
$this->regressDatabase();
$this->migrateDatabase();
$result = $this->call('get', $this->podcastApiUrl . 'podcasts');
$result->assertStatus(200);
$result->assertHeader('Content-Type', 'application/json; charset=UTF-8');
$result->assertJSONExact([]);
$this->seed($this->seed);
}
}