Skip to content

Commit

Permalink
test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abourtnik committed Jan 17, 2025
1 parent dc80c2a commit 55adc6f
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 43 deletions.
58 changes: 25 additions & 33 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -1,53 +1,45 @@
APP_NAME=Laravel
APP_ENV=local
APP_NAME=ClipZoneTest
APP_ENV=testing
APP_KEY=base64:QikAJAlo0evYLq2RYFxGv/PRrSIfJcNDj2qFiRp1oUs=
APP_DEBUG=true
APP_URL=http://localhost:8080
BCRYPT_ROUNDS=4

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=:memory:
DB_DATABASE=clipzone_test
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file

CACHE_DRIVER=array

FILESYSTEM_DISK=local

QUEUE_CONNECTION=sync
SESSION_DRIVER=file

SESSION_DRIVER=array
SESSION_LIFETIME=120
SESSION_DOMAIN=localhost
SESSION_CONNECTION=session

# MEMCACHED_HOST=127.0.0.1
MEMCACHED_HOST=memcached

# REDIS_HOST=127.0.0.1
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=[email protected]
MAIL_MAILER=array
MAIL_FROM_ADDRESS=[email protected]
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
DEBUGBAR_ENABLED=false

SUPPORT_EMAIL=[email protected]
SERVER_EMAIL=[email protected]

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
CASHIER_CURRENCY=eur
CASHIER_LOGGER=stack

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
SCOUT_DRIVER=null

SCOUT_DRIVER=database
TELESCOPE_ENABLED=false
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ deploy: ## Deploy application

test: ## Run test
docker exec -it php_container php artisan config:clear
docker exec -it php_container php artisan test --stop-on-failure --env=testing
docker exec -it php_container php artisan test --env=testing --stop-on-failure

logs: ## See last logs
docker exec -it php_container tail -f storage/logs/laravel.log
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Requests/Video/FileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function (string $attribute, string $value, Closure $fail) {
// file mimetype only available on first chunk other chunk mimetype are application/octet-stream
Rule::when($this->get('resumableChunkNumber') == 1, [new MimetypeEnum(VideoType::class)]),
'max:'.self::CHUNK_SIZE // Chunk size 10mo
]
],
'resumableIdentifier' => 'required|string',
'resumableChunkNumber' => 'required|integer|min:1',
'resumableTotalChunks' => 'required|integer|min:1',
];
}

Expand All @@ -59,7 +62,7 @@ function (string $attribute, string $value, Closure $fail) {
*/
public function messages(): array
{
$fileSize = Number::fileSize($this->get('resumableTotalSize'));
$fileSize = Number::fileSize($this->get('resumableTotalSize', 0));
$maxSize = Number::fileSize(config('plans.'.Auth::user()->plan.'.max_file_size'));

return [
Expand Down
6 changes: 5 additions & 1 deletion app/Services/VideoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getSuggestedVideos(Video $parentVideo): Collection
return $user_videos->merge($category_videos)->merge($random_videos);
}

public function getNextVideo (Collection $suggestedVideos, ?Playlist $playlist, ?int $currentIndex) : NextVideo {
public function getNextVideo (Collection $suggestedVideos, ?Playlist $playlist, ?int $currentIndex) : NextVideo | null {

if ($playlist && ($currentIndex + 1 !== $playlist->videos_count)) {

Expand All @@ -67,6 +67,10 @@ public function getNextVideo (Collection $suggestedVideos, ?Playlist $playlist,

}

if ($suggestedVideos->isEmpty()) {
return null;
}

return new SuggestedVideo($suggestedVideos->first());
}
}
2 changes: 0 additions & 2 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Factories\Factory;

use App\Models\User;
use App\Models\Comment;

/**
Expand All @@ -23,7 +22,6 @@ public function definition() : array

return [
'content' => fake()->realText(config('validation.comment.content.max')),
'user_id' => User::inRandomOrder()->first()->id,
'ip' => fake()->ipv4(),
'created_at' => $date,
'updated_at' => $date,
Expand Down
2 changes: 1 addition & 1 deletion database/factories/InteractionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function definition() : array
*
* @return $this
*/
public function configure()
public function configure() : static
{
return $this->sequence(
fn ($sequence) => ['user_id' => fake()->unique($sequence->index === 0)->randomElement(User::pluck('id'))]
Expand Down
11 changes: 10 additions & 1 deletion database/factories/ThumbnailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ class ThumbnailFactory extends Factory
public function definition() : array
{
return [
'file' => fake()->word(),
'file' => fake()->regexify('^[a-zA-Z0-9]{40}\.jpg$'),
];
}

public function configure(): static
{
return $this->sequence(
fn ($sequence) => [
'is_active' => $sequence->index === 0,
]
);
}

/**
* Indicate that the thumbnail is generated.
*
Expand Down
9 changes: 9 additions & 0 deletions database/factories/VideoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ public function banned() : Factory
];
});
}

public function withStatus($status) : Factory
{
return $this->state(function (array $attributes) use ($status) {
return [
'status' => $status,
];
});
}
}
4 changes: 3 additions & 1 deletion resources/views/videos/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<video-player
thumbnail_url="{{$video->thumbnail_url}}"
file_url="{{$video->file_url}}"
next_video="{{$nextVideo->route}}"
next_video="{{$nextVideo?->route}}"
subtitles="{{$subtitles}}"
/>
@endif
Expand Down Expand Up @@ -348,7 +348,9 @@ class="btn btn-danger"
<div class="card card-body bg-light-dark cursor-pointer" x-show="!open" @click="open=true">
<div class="d-flex justify-content-between align-items-center">
<div class="d-flex flex-column gap-1 w-95">
@if($nextVideo)
<div> <span class="fw-bold">{{__('Next')}} :</span> {{$nextVideo->title}} </div>
@endif
<small class="text-sm">{{Str::limit($playlist->title, 45)}} • <span class="text-muted text-sm">{{$currentIndex + 1}}/{{$playlist->videos_count}}</span></small>
</div>
<i class="fa-solid fa-chevron-down"></i>
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
->controller(CommentController::class)
->middleware('throttle:api')
->group(function () {
Route::get('/', 'index');
Route::get('/', 'index')->name('index');
Route::get('/{comment}/replies', 'replies')->name('replies');
Route::middleware('auth:sanctum')->group(function () {
Route::post('/', 'store')->name('store');
Expand Down
66 changes: 66 additions & 0 deletions tests/Feature/Api/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Feature\Api;

use App\Enums\VideoStatus;
use App\Models\Comment;
use App\Models\Video;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;

class CommentTest extends TestCase
{
use RefreshDatabase;

/**
* A basic feature test example.
*
* @return void
*/
public function test_comments_list(): void
{
$video = Video::factory()->withStatus(VideoStatus::PUBLIC->value)->forUser()->create();

Comment::factory()->forUser()->for($video)->count(3)->createQuietly();

$response = $this->get(route('comments.index', $video->uuid));

$response
->assertStatus(200)
->assertJson(fn (AssertableJson $json) =>
$json
->has('data', 3, fn (AssertableJson $json) =>
$json
->whereType('id', 'integer')
->whereType('class', 'string')
->whereType('content', 'string')
->whereType('parsed_content', 'string')
->whereType('short_content', 'string')
->whereType('is_long', 'boolean')
->has('user', fn (AssertableJson $json) =>
$json->whereType('id', 'integer')
->whereType('username', 'string')
->whereType('avatar', 'string')
->whereType('route', 'string')
->whereType('is_video_author', 'boolean')
)
->whereType('video_uuid', 'string')
->whereType('created_at', 'string')
->whereType('is_updated', 'boolean')
->whereType('likes_count', 'integer')
->whereType('dislikes_count', 'integer')
->whereType('is_reply', 'boolean')
->whereType('is_pinned', 'boolean')
->whereType('has_replies', 'boolean')
->whereType('replies_count', 'integer')
->whereType('is_video_author_reply', 'boolean')
->whereType('is_video_author_like', 'boolean')
->etc()
)
->has('links')
->has('meta')
->has('count')
);
}
}
51 changes: 51 additions & 0 deletions tests/Feature/Api/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Feature\Api;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;

class UserTest extends TestCase
{
use RefreshDatabase;

/**
* A basic feature test example.
*
* @return void
*/
public function test_user_show(): void
{
$user = User::factory()->create();

$response = $this->get(route('users.show', $user->id));

$response
->assertStatus(200)
->assertJson(fn (AssertableJson $json) =>
$json
->whereType('id', 'integer')
->whereType('username', 'string')
->whereType('slug', 'string')
->whereType('avatar', 'string')
->whereType('banner', 'string')
->whereType('show_subscribers', 'boolean')
->when($user->show_subscribers, fn() =>
$json->whereType('subscribers', 'integer')
)
->whereType('videos_count', 'integer')
->whereType('short_description', 'string')
->whereType('description', 'string')
->whereType('website', 'string')
->whereType('country_code', 'string')
->whereType('country', 'string')
->whereType('views', 'integer')
->whereType('created_at', 'string')
->whereType('pinned_video', 'array|null')
->whereType('videos', 'array')
->whereType('playlists', 'array')
);
}
}
Loading

0 comments on commit 55adc6f

Please sign in to comment.