-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from yungstarry/feat/fetchregion
feat: get user regions by id
- Loading branch information
Showing
6 changed files
with
168 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Region; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @extends | ||
*/ | ||
class RegionFactory extends Factory | ||
{ | ||
protected $model = Region::class; | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'id' => $this->faker->uuid(), | ||
'name' => $this->faker->city(), | ||
|
||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use App\Models\Region; | ||
use Illuminate\Database\Seeder; | ||
|
||
|
||
class RegionSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
*/ | ||
public function run() | ||
{ | ||
Region::factory()->count(10)->create(); // Creates 10 regions | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Preference; | ||
use App\Models\Region; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Tests\TestCase; | ||
use Tymon\JWTAuth\Facades\JWTAuth; | ||
|
||
class PreferenceControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected $user; | ||
protected $preference; | ||
protected $region; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = User::factory()->create(); | ||
$this->preference = Preference::factory()->create(['user_id' => $this->user->id]); | ||
$this->region = Region::factory()->create(); | ||
} | ||
|
||
public function test_get_region() | ||
{ | ||
// Authenticate the user with a valid JWT token | ||
$token = JWTAuth::fromUser($this->user); | ||
|
||
// Scenario: Region is set | ||
$this->preference->region_id = $this->region->id; | ||
$this->preference->save(); | ||
|
||
$response = $this->withHeaders([ | ||
'Authorization' => "Bearer $token", | ||
])->getJson("/api/v1/regions/{$this->user->id}"); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => 200, | ||
'message' => 'Region retrieved successfully', | ||
'data' => [ | ||
'id' => $this->region->id, | ||
'name' => $this->region->name, | ||
], | ||
]); | ||
|
||
// Scenario: Region is not set | ||
$this->preference->region_id = null; | ||
$this->preference->save(); | ||
|
||
$response = $this->withHeaders([ | ||
'Authorization' => "Bearer $token", | ||
])->getJson("/api/v1/regions/{$this->user->id}"); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => 200, | ||
'message' => 'Region has not been set', | ||
'data' => [], | ||
]); | ||
|
||
// Scenario: Preference not found | ||
$newUser = User::factory()->create(); | ||
$newToken = JWTAuth::fromUser($newUser); | ||
|
||
$response = $this->withHeaders([ | ||
'Authorization' => "Bearer $newToken", | ||
])->getJson("/api/v1/regions/{$newUser->id}"); | ||
|
||
$response->assertStatus(404) | ||
->assertJson([ | ||
'status' => 404, | ||
'message' => 'Preference not found for user', | ||
]); | ||
} | ||
} |