Skip to content

Commit

Permalink
cover laravel qrcode factory formats creation
Browse files Browse the repository at this point in the history
  • Loading branch information
2amjsouza committed Dec 13, 2023
1 parent dcb2909 commit a55c18e
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Controllers/LaravelResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ final class LaravelResourceController
*/
public function __invoke(Request $request)
{
ob_end_clean();
if (ini_get('output_buffering')) {
ob_clean();
}

$data = $request->only([
'content',
Expand Down
2 changes: 1 addition & 1 deletion src/Enums/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Format extends Enum
public const MMS = MmsFormat::class;
public const PHONE_FORMAT = PhoneFormat::class;
public const SNS_FORMAT = SmsFormat::class;
public const VCARD = VCardFormat::class;
public const V_CARD = VCardFormat::class;
public const WIFI = WifiFormat::class;
public const YOUTUBE = YoutubeFormat::class;
}
260 changes: 260 additions & 0 deletions tests/unit/LaravelQrCodeFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace unit;
use Da\QrCode\Enums\Format;
use Da\QrCode\Enums\Gradient;
use Da\QrCode\Factory\LaravelQrCodeFactory;
use Da\QrCode\QrCode;
Expand Down Expand Up @@ -91,6 +92,265 @@ public function testCreateGradientQrCode()
);
}

public function testFactoryFormatText()
{
$content = '2am. Technologies';
$qrCode = LaravelQrCodeFactory::make(
$content,
Format::TEXT
);

$this->assertEquals($qrCode->getText(), $content);
}

public function testFactoryFormatBookMark()
{
$content = [
'title' => '2am. Technologies',
'url' => 'https://2am.tech',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::BOOK_MARK
);

$this->assertTrue(
str_contains($qrCode->getText(), 'MEBKM:TITLE:')
&& str_contains($qrCode->getText(), ';URL:')
);
}

public function testFactoryFormatBtc()
{
$content = [
'name' => '2am. Technologies',
'amount' => 1,
'message' => 'unt test',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::BTC
);

$this->assertTrue(str_contains($qrCode->getText(), 'bitcoin:'));
}

public function testFactoryFormatGeo()
{
$content = [
'lat' => 100,
'lng' => 100,
'altitude' => 1,
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::GEO
);

$this->assertTrue(str_contains($qrCode->getText(), 'GEO:'));
}

public function testFactoryFormatICal()
{
$content = [
'summary' => 'unit test',
'startTimestamp' => 1702451054,
'endTimestamp' => 1702454654,
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::I_CAL
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'BEGIN:VEVENT')
&& str_contains($content, 'SUMMARY:')
&& str_contains($content, 'DTSTART:')
&& str_contains($content, 'DTEND:')
&& str_contains($content, 'END:VEVENT')
);
}

public function testFactoryFormatMailMessage()
{
$content = [
'subject' => 'unit test',
'body' => 'unit test body',
'email' => '[email protected]',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::MAIL_MESSAGE
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'MATMSG:TO:')
&& str_contains($content, 'SUB:')
&& str_contains($content, 'BODY:')
);
}

public function testFactoryFormatMailTo()
{
$content = [
'email' => '[email protected]',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::MAIL_TO
);
$content = $qrCode->getText();

$this->assertTrue(str_contains($content, 'MAILTO:'));
}

public function testFactoryFormatMeCard()
{
$content = [
'firstName' => 'unit',
'lastName' => 'testing',
'nickName' => 'unit testing',
'address' => 'saint monica st',
'phone' => '1 1111 221',
'videoPhone' => '1 111 121',
'birthday' => '05/11/1990',
'note' => '',
'email' => '[email protected]',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::ME_CARD
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'MECARD:')
&& str_contains($content, 'N:')
&& str_contains($content, 'SOUND:')
&& str_contains($content, 'TEL:')
&& str_contains($content, 'TEL-AV:')
&& str_contains($content, 'EMAIL:')
&& str_contains($content, 'NOTE:')
&& str_contains($content, 'BDAY:')
&& str_contains($content, 'ADR:')
&& str_contains($content, 'URL:')
&& str_contains($content, 'NICKNAME:')
);
}

public function testFactoryFormatMms()
{
$content = [
'msg' => 'unit test',
'phone' => '1 111 122',

];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::MMS
);
$content = $qrCode->getText();

$this->assertTrue(str_contains($content, 'MMSTO:'));
}

public function testFactoryFormatPhone()
{
$content = [
'phone' => '1 111 122',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::PHONE_FORMAT
);
$content = $qrCode->getText();

$this->assertTrue(str_contains($content, 'TEL:'));
}

public function testFactoryFormatSms()
{
$content = [
'phone' => '1 111 122',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::SNS_FORMAT
);
$content = $qrCode->getText();

$this->assertTrue(str_contains($content, 'SMS:'));
}

public function testFactoryFormatVCard()
{
$content = [
'name' => 'testing',
'fullName' => 'unit testing',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::V_CARD
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'BEGIN:VCARD')
&& str_contains($content, 'END:VCARD')
);
}

public function testFactoryFormatWifi()
{
$content = [
'authentication' => 'wpa2',
'ssid' => 'unit-testing',
'password' => 'xxxxxxxxxx',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::WIFI
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'WIFI:')
&& str_contains($content, 'S:')
&& str_contains($content, 'P:')
);
}

public function testFactoryFormatYoutube()
{
$content = [
'videoId' => '123456',
];

$qrCode = LaravelQrCodeFactory::make(
$content,
Format::YOUTUBE
);
$content = $qrCode->getText();

$this->assertTrue(
str_contains($content, 'youtube://')
);
}

protected function normalizeString($string)
{
return str_replace(
Expand Down

0 comments on commit a55c18e

Please sign in to comment.