forked from endroid/qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request endroid#143 from iltar/feature/split-twig-extension
Split of the twig extension
- Loading branch information
Showing
6 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Jeroen van den Enden <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Endroid\QrCode\Twig\Extension; | ||
|
||
use Endroid\QrCode\Factory\QrCodeFactoryInterface; | ||
use Twig_Extension; | ||
use Twig_SimpleFunction; | ||
|
||
final class QrCodeUriExtension extends Twig_Extension | ||
{ | ||
private $qrCodeFactory; | ||
|
||
public function __construct(QrCodeFactoryInterface $qrCodeFactory) | ||
{ | ||
$this->qrCodeFactory = $qrCodeFactory; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new Twig_SimpleFunction('qrcode_data_uri', [$this, 'qrCodeDataUriFunction']), | ||
]; | ||
} | ||
|
||
public function qrCodeDataUriFunction(string $text, array $options = []): string | ||
{ | ||
return $this->qrCodeFactory->create($text, $options)->writeDataUri(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Jeroen van den Enden <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Endroid\QrCode\Tests\Twig\Extension; | ||
|
||
use Endroid\QrCode\Factory\QrCodeFactory; | ||
use Endroid\QrCode\Twig\Extension\QrCodeExtension; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
class QrCodeExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @group legacy | ||
* @expectedDeprecation Using Endroid\QrCode\Twig\Extension\QrCodeExtension::qrCodeDataUriFunction is deprecated as of 3.1 and will be removed in 4.0. Use Endroid\QrCode\Twig\Extension\QrCodeUriExtension::qrCodeDataUriFunction() instead. | ||
*/ | ||
public function testQrCodeDataUriFunction() | ||
{ | ||
$router = $this->prophesize(RouterInterface::class); | ||
$extension = new QrCodeExtension(new QrCodeFactory(), $router->reveal()); | ||
$this->assertStringStartsWith('data:image/png;base64,', $extension->qrCodeDataUriFunction('Foobar')); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* @expectedDeprecation Using Endroid\QrCode\Twig\Extension\QrCodeExtension::qrCodePathFunction is deprecated as of 3.1 and will be removed in 4.0. Install the EndroidQrCodeBundle instead. | ||
*/ | ||
public function testQrCodePathFunction() | ||
{ | ||
$router = $this->prophesize(RouterInterface::class); | ||
$extension = new QrCodeExtension(new QrCodeFactory(), $router->reveal()); | ||
|
||
$router | ||
->generate('endroid_qrcode_generate', ['extension' => 'png', 'text' => 'Foobar'], UrlGeneratorInterface::ABSOLUTE_PATH) | ||
->willReturn('/some-qr-code-path'); | ||
|
||
$this->assertSame('/some-qr-code-path', $extension->qrCodePathFunction('Foobar')); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* @expectedDeprecation Using Endroid\QrCode\Twig\Extension\QrCodeExtension::qrCodeUrlFunction is deprecated as of 3.1 and will be removed in 4.0. Install the EndroidQrCodeBundle instead. | ||
*/ | ||
public function testQrCodeUrlFunction() | ||
{ | ||
$router = $this->prophesize(RouterInterface::class); | ||
$extension = new QrCodeExtension(new QrCodeFactory(), $router->reveal()); | ||
|
||
$router | ||
->generate('endroid_qrcode_generate', ['extension' => 'png', 'text' => 'Foobar'], UrlGeneratorInterface::ABSOLUTE_URL) | ||
->willReturn('https://some-qr-code-url'); | ||
|
||
$this->assertSame('https://some-qr-code-url', $extension->qrCodeUrlFunction('Foobar')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Jeroen van den Enden <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Endroid\QrCode\Tests\Twig\Extension; | ||
|
||
use Endroid\QrCode\Factory\QrCodeFactory; | ||
use Endroid\QrCode\Twig\Extension\QrCodeUriExtension; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class QrCodeUriExtensionTest extends TestCase | ||
{ | ||
public function testQrCodeDataUriFunction() | ||
{ | ||
$extension = new QrCodeUriExtension(new QrCodeFactory()); | ||
$this->assertStringStartsWith('data:image/png;base64,', $extension->qrCodeDataUriFunction('Foobar')); | ||
} | ||
} |