create profile api's with tests

This commit is contained in:
2024-08-20 08:04:33 +00:00
committed by Hamidreza Ranjbarpour
parent f1626a9d67
commit c710183aa9
18 changed files with 979 additions and 83 deletions

View File

@@ -0,0 +1,115 @@
<?php
namespace Tests\Feature\Facades;
use App\Facades\File\FileFacade;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FileTest extends TestCase
{
use WithFaker;
/**
* A basic feature test example.
*/
public function test_image_is_saved(): void
{
Storage::fake('public');
$inputFile = UploadedFile::fake()->image($this->faker->image);
$path = FileFacade::save($inputFile, 'testDir');
Storage::disk('public')->assertExists($path);
}
public function test_image_is_saved_with_specified_name(): void
{
Storage::fake('public');
$imageName = 'photo.png';
$inputFile = UploadedFile::fake()->image($imageName);
$actualPath = FileFacade::save($inputFile, 'testDir', $imageName);
$expectedPath = 'testDir/photo.png';
Storage::disk('public')->assertExists($actualPath);
$this->assertEquals($expectedPath, $actualPath);
}
public function test_file_is_saved(): void
{
Storage::fake('public');
$fileName = 'fake' . '.' . $this->faker->fileExtension();
$inputFile = UploadedFile::fake()->create($fileName);
$path = FileFacade::save($inputFile, 'testDir');
Storage::disk('public')->assertExists($path);
}
public function test_file_is_saved_with_specified_name(): void
{
Storage::fake('public');
$fileName = 'fake' . '.' . $this->faker->fileExtension();
$inputFile = UploadedFile::fake()->create($fileName);
$actualPath = FileFacade::save($inputFile, 'testDir', $fileName);
$expectedPath = 'testDir/' . $fileName;
Storage::disk('public')->assertExists($actualPath);
$this->assertEquals($expectedPath, $actualPath);
}
public function test_file_is_saved_with_specified_disk(): void
{
Storage::fake('public');
Storage::fake('p2');
$fileName = 'fake' . '.' . $this->faker->fileExtension();
$inputFile = UploadedFile::fake()->create($fileName);
$path = FileFacade::save($inputFile, 'testDir', disk: 'p2');
Storage::disk('p2')->assertExists($path);
Storage::disk('public')->assertMissing($path);
}
public function test_delete_throws_exception_with_empty_file_path(): void
{
$this->assertThrows(
fn() => FileFacade::delete(''),
\InvalidArgumentException::class
);
}
public function test_file_is_deleted(): void
{
Storage::fake('public');
$fileName = 'fake' . '.' . $this->faker->fileExtension();
$inputFile = UploadedFile::fake()->create($fileName);
$path = Storage::disk('public')->putFile('testDir', $inputFile);
Storage::disk('public')->assertExists($path);
FileFacade::delete($path);
Storage::disk('public')->assertMissing($path);
}
public function test_file_with_absolute_path_is_deleted(): void
{
Storage::fake('public');
$fileName = 'fake' . '.' . $this->faker->fileExtension();
$inputFile = UploadedFile::fake()->create($fileName);
$path = Storage::disk('public')->putFile('testDir', $inputFile);
$absolutePath = Storage::disk('public')->url($path);
Storage::disk('public')->assertExists($path);
FileFacade::delete($absolutePath, url_is_absolute: true);
Storage::disk('public')->assertMissing($path);
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace Tests\Feature\V3\Profile;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ChangePasswordTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_current_password_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'));
$response->assertUnprocessable()
->assertInvalid([
'current_password' => __('validation.required', ['attribute' => 'رمز عبور فعلی'])
]);
}
public function test_current_password_should_be_string()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'current_password' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'current_password' => __('validation.string', ['attribute' => 'رمز عبور فعلی'])
]);
}
public function test_current_password_should_match_the_regex()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'current_password' => $this->faker->numberBetween(10000000, 99999999)
]);
$response->assertUnprocessable()
->assertInvalid([
'current_password' => __('validation.regex', ['attribute' => 'رمز عبور فعلی'])
]);
}
public function test_current_password_should_have_at_least_8_characters()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'current_password' => $this->faker->numerify()
]);
$response->assertUnprocessable()
->assertInvalid([
'current_password' => __('validation.min.string', ['attribute' => 'رمز عبور فعلی', 'min' => 8])
]);
}
public function test_new_password_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'));
$response->assertUnprocessable()
->assertInvalid([
'new_password' => __('validation.required', ['attribute' => 'رمز عبور جدید'])
]);
}
public function test_new_password_should_be_string()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'new_password' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'new_password' => __('validation.string', ['attribute' => 'رمز عبور جدید'])
]);
}
public function test_new_password_should_match_the_regex()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'new_password' => $this->faker->numberBetween(10000000, 99999999)
]);
$response->assertUnprocessable()
->assertInvalid([
'new_password' => __('validation.regex', ['attribute' => 'رمز عبور جدید'])
]);
}
public function test_new_password_should_have_at_least_8_characters()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.changePassword'), [
'new_password' => $this->faker->numerify()
]);
$response->assertUnprocessable()
->assertInvalid([
'new_password' => __('validation.min.string', ['attribute' => 'رمز عبور جدید', 'min' => 8])
]);
}
public function test_can_not_change_the_password_of_the_user_with_wrong_current_password(): void
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
$this->actingAs($user);
$password = $this->faker->numerify('#a######');
$response = $this->post(route('v3.profile.changePassword'), [
'current_password' => 'fake1234',
'new_password' => $password,
]);
$response->assertStatus(422);
$response->assertExactJson([
'message' => __('messages.incorrect_current_password'),
"type" => "logical_exception"
]);
}
public function test_user_can_change_the_password(): void
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('v3.profile.changePassword'), [
'current_password' => 'password1234',
'new_password' => $this->faker->numerify('#a######'),
]);
$response->assertStatus(200);
$response->assertExactJson([
'message' => __('messages.successful')
]);
}
}

View File

@@ -0,0 +1,239 @@
<?php
namespace Tests\Feature\V3\Profile;
use App\Facades\File\FileFacade;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class EditTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_first_name_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'));
$response->assertUnprocessable()
->assertInvalid([
'first_name' => __('validation.required', ['attribute' => 'نام'])
]);
}
public function test_first_name_should_be_string()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'), [
'first_name' => $this->faker->numberBetween(0, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'first_name' => __('validation.string', ['attribute' => 'نام'])
]);
}
public function test_last_name_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'));
$response->assertUnprocessable()
->assertInvalid([
'last_name' => __('validation.required', ['attribute' => 'نام خانوادگی'])
]);
}
public function test_last_name_should_be_string()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'), [
'last_name' => $this->faker->numberBetween(0, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'last_name' => __('validation.string', ['attribute' => 'نام خانوادگی'])
]);
}
public function test_degree_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'));
$response->assertUnprocessable()
->assertInvalid([
'degree' => __('validation.required', ['attribute' => 'مدرک تحصیلی'])
]);
}
public function test_major_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'));
$response->assertUnprocessable()
->assertInvalid([
'major' => __('validation.required', ['attribute' => 'شغل'])
]);
}
public function test_mobile_is_required()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'));
$response->assertUnprocessable()
->assertInvalid([
'mobile' => __('validation.required', ['attribute' => 'تلفن همراه'])
]);
}
public function test_mobile_should_match_the_regex()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'), [
'mobile' => $this->faker->numberBetween(1000000, 9999999)
]);
$response->assertUnprocessable()
->assertInvalid([
'mobile' => __('validation.regex', ['attribute' => 'تلفن همراه'])
]);
}
public function test_mobile_should_be_numeric()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'), [
'mobile' => $this->faker->name()
]);
$response->assertUnprocessable()
->assertInvalid([
'mobile' => __('validation.numeric', ['attribute' => 'تلفن همراه'])
]);
}
public function test_mobile_should_be_11_digits()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('v3.profile.edit'), [
'mobile' => $this->faker->numberBetween(10000, 99999)
]);
$response->assertUnprocessable()
->assertInvalid([
'mobile' => __('validation.digits', ['attribute' => 'تلفن همراه', 'digits' => 11])
]);
}
public function test_avatar_should_match_the_mimes()
{
$user = User::factory()->create();
$this->actingAs($user);
$image = UploadedFile::fake()->create('image.txt', 40);
$response = $this->postJson(route('v3.profile.edit'), [
'avatar' => $image
]);
$response->assertUnprocessable()
->assertInvalid([
'avatar' => __('validation.mimes', ['attribute' => 'آواتار', 'values' => 'png, jpg, jpeg'])
]);
}
public function test_avatar_should_be_less_than_2048kb()
{
$user = User::factory()->create();
$this->actingAs($user);
$image = UploadedFile::fake()->create('image.jpg', 40000);
$response = $this->postJson(route('v3.profile.edit'), [
'avatar' => $image
]);
$response->assertUnprocessable()
->assertInvalid([
'avatar' => __('validation.max.file', ['attribute' => 'آواتار', 'max' => 2048])
]);
}
public function test_user_can_edit_the_profile()
{
$user = User::factory()->create();
$this->actingAs($user);
$avatar = UploadedFile::fake()->create('image.jpg', 1024);
FileFacade::shouldReceive('save')
->andReturn('image.jpg');
FileFacade::shouldReceive('delete');
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'degree' => $this->faker->name,
'major' => $this->faker->name,
'mobile' => $this->faker->numerify('09#########'),
'avatar' => $avatar
];
$response = $this->postJson(route('v3.profile.edit'), $data);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful')
]);
$this->assertDatabaseHas('users', [
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'mobile' => $data['mobile'],
'degree' => $data['degree'],
]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Tests\Feature\V3\Profile;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class InfoTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_user_info_returns_successfully(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->get(route('v3.profile.info'));
$response->assertStatus(200);
$response->assertExactJson([
'data' => [
'username' => $user->username,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'name' => $user->name,
'last_login' => $user->last_login,
'major' => $user->major,
'degree' => $user->degree,
'mobile' => $user->mobile,
'avatar' => $user->avatar,
]
]);
}
}