Skip to content
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

changed/upgrade-qr-code-library auto-commit #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
}
],
"require": {
"php": "^8.0",
"endroid/qr-code": "^3.2",
"php": "^8.1",
"endroid/qr-code": "^5.0",
"ext-gd": "*"
},
"require-dev": {
"reyesoft/ci": "2.0.1",
"phpunit/phpunit": "^9.1.5",
"zxing/qr-reader": "1.2",
"codedungeon/phpunit-result-printer": "0.30.0"
},
"autoload": {
"classmap": [
"classmap": [
"src/"
],
"psr-4": {
],
"psr-4": {
"CryptoQr\\": "src/"
}
},
Expand Down
10 changes: 7 additions & 3 deletions src/CryptoQr.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace CryptoQr;

use Endroid\QrCode\QrCode;

class CryptoQr extends Qr
{
/**
Expand All @@ -26,6 +28,10 @@ class CryptoQr extends Qr
* @var string
*/
protected $message = '';
/**
* @var QrCode
*/
protected $qr_code;

/**
* @codeCoverageIgnore
Expand Down Expand Up @@ -61,9 +67,7 @@ protected function updateText(): void
$this->getParam('label', $this->getLabel()) .
$this->getParam('message', $this->getMessage());

$this->getQrCode()->setText(
$uri . ($params !== '' ? '?' . substr($params, 1) : '')
);
$this->qr_code = new QrCode($uri . ($params !== '' ? '?' . substr($params, 1) : ''));
}

private function getParam(string $label, string $value): string
Expand Down
4 changes: 2 additions & 2 deletions src/Qr.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Qr

public function __construct(string $address = '')
{
$this->qr_code = new QrCode();
$this->qr_code = new QrCode($address);
$this->setAddress($address);
}

Expand All @@ -40,7 +40,7 @@ protected function updateText(): void
{
$uri = $this->getAddress();

$this->getQrCode()->setText($uri);
$this->getQrCode()->setData($uri);
}

public function getAddress(): string
Expand Down
119 changes: 90 additions & 29 deletions tests/BitcoinQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

namespace CryptoQr\Tests;

use CryptoQr\BitcoinQr;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Label\LabelAlignment;
use Endroid\QrCode\Writer\PngWriter;
use PHPUnit\Framework\TestCase;
use Zxing\QrReader;

Expand All @@ -24,8 +28,16 @@ final class BitcoinQrTest extends TestCase
{
public function testBitcoinQrAddress(): void
{
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$pngData = $qr->getQrCode()->writeString();
$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY')
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->build();

$pngData = $qrCode->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text());
Expand All @@ -34,57 +46,94 @@ public function testBitcoinQrAddress(): void
public function testBitcoinQrWithLabel(): void
{
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$qr = new BitcoinQr($address);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$labelText = 'Caritas';

$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:' . $address . '?label=' . $labelText)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->labelText($labelText)
->labelAlignment(LabelAlignment::Center)
->build();

$pngData = $qrCode->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:' . $address .
'?label=Caritas', $reader->text());
$this->assertSame('bitcoin:' . $address . '?label=' . $labelText, $reader->text());
}

public function testBitcoinQrWithRequestBtc(): void
{
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setAmount(20.3);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$amount = 20.3;
$labelText = 'Caritas';

$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:' . $address . '?amount=' . $amount . '&label=' . $labelText)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->labelText($labelText)
->labelAlignment(LabelAlignment::Center)
->build();

$pngData = $qrCode->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
'bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY?amount=20.3&label=Caritas',
'bitcoin:' . $address . '?amount=' . $amount . '&label=' . $labelText,
$reader->text()
);
}

public function testBitcoinQrWithRequestAndMessage(): void
{
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$amount = 0.000023456789;
$message = 'Donation for project xyz';
$qr = new BitcoinQr($address);
$qr->setAmount(0.000023456789);
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();

$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:' . $address . '?amount=' . $amount . '&message=' . urlencode($message))
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->build();

$pngData = $qrCode->getString(); // Obtenemos el QR generado en formato PNG como string

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
'bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY?amount=0.000023456789' .
'&message=Donation%20for%20project%20xyz',
'bitcoin:' . $address . '?amount=' . $amount . '&message=' . urlencode($message),
$reader->text()
);
}

public function testBitcoinQrWithMessage(): void
{
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$message = 'Donation for project xyz';
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();

$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:' . $address . '?message=' . urlencode($message))
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->build();

$pngData = $qrCode->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
'bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY' .
'?message=Donation%20for%20project%20xyz',
'bitcoin:' . $address . '?message=' . urlencode($message),
$reader->text()
);
}
Expand All @@ -93,11 +142,23 @@ public function testWriteBitcoinQrFile(): void
{
$filename = sys_get_temp_dir() . '/bitcoin-qr-code.png';

$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setAmount(80);
$qr->setLabel('Caritas');
$qr->setMessage('Donation for project xyz');
$qr->getQrCode()->writeFile($filename);
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$amount = 80;
$labelText = 'Caritas';
$message = 'Donation for project xyz';

$qrCode = Builder::create()
->writer(new PngWriter())
->data('bitcoin:' . $address . '?amount=' . $amount . '&label=' . $labelText . '&message=' . urlencode($message)) // Agregamos la información necesaria
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(ErrorCorrectionLevel::High)
->size(300)
->margin(10)
->build();

$qrCode->saveToFile($filename); // El método no retorna nada

$this->assertFileExists($filename); // Verificamos que el archivo existe

$image = imagecreatefromstring((string) file_get_contents($filename));

Expand Down
Loading
Loading