Skip to content

Commit

Permalink
tests/main: all tests coverage added
Browse files Browse the repository at this point in the history
  • Loading branch information
2amjsouza committed Sep 1, 2023
1 parent 6cfe684 commit 9f5e3b1
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
1 change: 0 additions & 1 deletion app/Livewire/QrCodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function download(string $extension)
$pathName = 'qrcode/' . uniqid() . ".$extension";
Storage::put($pathName, $this->qrCode->writeString());

//TODO command to delete files
return Storage::download($pathName, 'qrcode');
}

Expand Down
9 changes: 6 additions & 3 deletions app/Livewire/Traits/LogoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ trait LogoTrait
public function applyLogo()
{
$this->validate($this->getLogoRules());
/** @var TemporaryUploadedFile $filename */
$filename = uniqid() . '.' . $this->file->getClientOriginalExtension();

$this->file->storePubliclyAs('public/qrcode', $filename);
$filePath = $this->file
->storePubliclyAs(
'public/qrcode',
$filename
);

$this->dispatch('apply-logo', '../storage/app/public/qrcode/' . $filename, 'local')
$this->dispatch('apply-logo', '../storage/app/' . $filePath)
->to(QrCodeComponent::class);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Traits/MarginTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait MarginTrait
{
public int $margins = 15;
public string|int $margins = 15;

public function applyMargin()
{
Expand Down
85 changes: 85 additions & 0 deletions tests/Unit/Livewire/QrCodeComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Livewire;

use App\Enum\FormatEnum;
use App\Livewire\QrCodeComponent;
use Da\QrCode\Contracts\LabelInterface;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class QrCodeComponentTest extends TestCase
{
public function test_create_qrcode()
{
Livewire::test(QrCodeComponent::class)
->call('create', FormatEnum::Text->value, ['text' => '2am. Technologies'])
->assertStatus(200);

Livewire::test(QrCodeComponent::class)
->call('create', FormatEnum::ICal->value, [
'summary' => 'testing',
'startTimestamp' => '2023-01-08 20:35:00',
'endTimestamp' => '2023-01-08 20:45:00',
])
->assertStatus(200);
}

public function test_apply_colors()
{
Livewire::test(QrCodeComponent::class)
->call('applyColors', [
'r' => 255,
'g' => 255,
'b' => 255,
], [
'r' => 255,
'g' => 0,
'b' => 0,
])
->assertStatus(200);
}

public function test_apply_label()
{
Livewire::test(QrCodeComponent::class)
->call('applyLabel', '2am. Technologies', 22, LabelInterface::ALIGN_CENTER)
->assertStatus(200);
}

public function test_apply_margin()
{
Livewire::test(QrCodeComponent::class)
->call('applyMargin', 22)
->assertStatus(200);
}

public function test_apply_logo()
{
Storage::fake('local');
$filePath = UploadedFile::fake()
->image('image.png')
->storePubliclyAs(
'public/qrcode',
'image.png'
);

Livewire::test(QrCodeComponent::class)
->call('applyLogo', 'storage/framework/testing/disks/local/' . $filePath)
->assertStatus(200);
}

public function test_download()
{
Storage::fake('local');

Livewire::test(QrCodeComponent::class)
->call('download', 'png')
->assertFileDownloaded();

Livewire::test(QrCodeComponent::class)
->call('download', 'jpg')
->assertFileDownloaded();
}
}
61 changes: 60 additions & 1 deletion tests/Unit/Livewire/Traits/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Livewire\Traits;

use App\Enum\FormatEnum;
use App\Enum\OptionsEnum;
use App\Livewire\QrCodeBuilder;
use Da\QrCode\Contracts\LabelInterface;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;

Expand All @@ -25,7 +28,8 @@ public function test_apply_colors()
->assertHasNoErrors([
'strForeground',
'strBackground'
]);
])
->assertNotDispatched('apply-colors');
}

public function test_apply_label()
Expand All @@ -37,5 +41,60 @@ public function test_apply_label()
->call('applyLabel')
->assertHasNoErrors()
->assertDispatched('apply-label');

Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->set('label', '')
->set('size', 0)
->set('align', '')
->call('applyLabel')
->assertHasErrors([
'label',
'size',
'align'
])
->assertNotDispatched('apply-label');
}

public function test_apply_logo()
{
Storage::fake('tests');
$file = UploadedFile::fake()->image('image.pne');

Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->set('file', $file)
->call('applyLogo')
->assertHasNoErrors()
->assertDispatched('apply-logo');

Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->set('file', '')
->call('applyLogo')
->assertHasErrors(['file'])
->assertNotDispatched('apply-logo');
}

public function test_apply_margin()
{
Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->set('margins', 22)
->call('applyMargin')
->assertHasNoErrors()
->assertDispatched('apply-margin');

Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->set('margins', -1)
->call('applyMargin')
->assertHasErrors(['margins'])
->assertNotDispatched('apply-margin');
}

public function test_toogle_option_form()
{
Livewire::test(QrCodeBuilder::class, [FormatEnum::Text->value])
->call('toggleOption', OptionsEnum::Label->value)
->call('toggleOption', OptionsEnum::Colors->value)
->call('toggleOption', OptionsEnum::Margin->value)
->call('toggleOption', OptionsEnum::Logo->value)
->assertHasNoErrors();
}
}

0 comments on commit 9f5e3b1

Please sign in to comment.