-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
325-refactor-model_binding-in-get-student_photo-endpoint #334
Open
diegdar
wants to merge
8
commits into
main
Choose a base branch
from
325-refactor-model_binding-in-get-student_photo-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cce6d2f
refactor(routes): :recycle: rename studentId parameter to student in …
diegdar a8b3fbc
Merge branch 'main' of https://github.com/IT-Academy-BCN/ita-profiles…
diegdar 11790b4
Merge branch 'main' of https://github.com/IT-Academy-BCN/ita-profiles…
diegdar 9a155d9
refactor(test): :recycle: refactor GetStudentImageControllerTest for …
diegdar 1237071
refactor(controller/route): :recycle: refactor route and controller f…
diegdar 5d8b0b8
refactor(service): :recycle: delete GetStudentImageService and its te…
diegdar 65cadba
refactor(annotations): :recycle: refactor GetStudentImageAnnotation d…
diegdar f2446c9
Merge branch 'main' of https://github.com/IT-Academy-BCN/ita-profiles…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,89 +9,87 @@ | |
}; | ||
use App\Service\Student\GetStudentImageService; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Mockery\MockInterface; | ||
use Illuminate\Support\Facades\Storage; | ||
use Tests\TestCase; | ||
|
||
class GetStudentImageControllerTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
private const PHOTOS_PATH = 'public/photos/'; | ||
protected User $user; | ||
protected Student $student; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
private function createUser() | ||
{ | ||
return User::factory()->create([ | ||
$this->user = User::factory()->create([ | ||
'dni'=>'27827083G', 'password'=>'Password%123' | ||
]); | ||
|
||
$this->student =Student::factory()->for($this->user)->create(); | ||
|
||
} | ||
|
||
public function testCanInstantiateAnUser(): void | ||
{ | ||
$this->assertInstanceOf(User::class, $this->user); | ||
} | ||
|
||
private function createStudent(User $user):Student | ||
public function testCanInstantiateAStudent(): void | ||
{ | ||
return Student::factory()->for($user)->create(); | ||
$this->assertInstanceOf(Student::class, $this->student); | ||
} | ||
|
||
private function getUserToken(User $user):string | ||
{ //hay que pasar lo datos manualmente porque el password es encriptado | ||
private function getUserToken():string | ||
{ | ||
$singInUserData = ['dni'=>'27827083G', 'password'=>'Password%123']; | ||
$url = route('signin'); | ||
$response = $this->json('POST', $url, $singInUserData); | ||
$token = $response->json('token'); | ||
return $token; | ||
} | ||
|
||
public function test_get_student_image():void | ||
public function testCanGetStudentImage():void | ||
{ | ||
$user = $this->createUser(); | ||
$student = $this->createStudent($user); | ||
$token = $this->getUserToken($user); | ||
$studentId = $student->id; | ||
$student = $this->student; | ||
$token = $this->getUserToken(); | ||
|
||
$url = route('student.photo.get', $studentId); | ||
$url = route('student.photo.get', $student->id); | ||
$response = $this->withHeaders([ | ||
'Authorization' => 'Bearer ' . $token | ||
])->get($url); | ||
|
||
$response->assertStatus(200); | ||
} | ||
$response->assertJson(['photo' => Storage::url(self::PHOTOS_PATH . $student->photo)]);} | ||
|
||
public function test_get_student_image_not_found():void | ||
public function testCanGetAnEmptyArrayWhenTheStudentHasNoPhoto():void | ||
{ | ||
$user = $this->createUser(); | ||
$student = $this->createStudent($user); | ||
$student = $this->student; | ||
$student->photo = null; | ||
$student->save(); | ||
$token = $this->getUserToken($user); | ||
$studentId = $student->id; | ||
$token = $this->getUserToken(); | ||
|
||
$url = route('student.photo.get', $studentId); | ||
$url = route('student.photo.get', $student->id); | ||
$response = $this->withHeaders([ | ||
'Authorization' => 'Bearer ' . $token | ||
])->get($url); | ||
|
||
$response->assertStatus(200); | ||
$response->assertJson([]); | ||
$response->assertJson(['photo' => '']); | ||
} | ||
|
||
public function test_can_return_a_500_on_internal_server_error(): void | ||
public function testCanReturns_404WithInvalidStudentUuid(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beware the underscore |
||
{ | ||
$this->mock(GetStudentImageService::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('execute') | ||
->andThrow(new \Exception('Internal Server Error')); | ||
}); | ||
$user = $this->createUser(); | ||
$student = $this->createStudent($user); | ||
$token = $this->getUserToken($user); | ||
$studentId = $student->id; | ||
|
||
$url = route('student.photo.get', $studentId); | ||
$invalidUuid = 'invalidUuid'; | ||
$token = $this->getUserToken(); | ||
|
||
$url = route('student.photo.get', $invalidUuid); | ||
$response = $this->withHeaders([ | ||
'Authorization' => 'Bearer ' . $token | ||
])->get($url); | ||
|
||
$response->assertStatus(500); | ||
$response->assertStatus(404); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the fixtures tests (only check the instance of what you are testing, if this has sense)