ImageTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. *
  4. * @filesource ImageTest.php
  5. * @created 08.02.2016
  6. * @package chillerlan\QRCodeTest\Output
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2015 Smiley
  9. * @license MIT
  10. */
  11. namespace chillerlan\QRCodeTest\Output;
  12. use chillerlan\QRCode\Output\QRImage;
  13. use chillerlan\QRCode\Output\QRImageOptions;
  14. use chillerlan\QRCode\QRCode;
  15. class ImageTest extends \PHPUnit_Framework_TestCase{
  16. /**
  17. * @var \chillerlan\QRCode\Output\QRImageOptions
  18. */
  19. protected $options;
  20. /**
  21. * @var \chillerlan\QRCode\QRCode
  22. */
  23. protected $QRCode;
  24. protected function setUp(){
  25. $this->options = new QRImageOptions;
  26. }
  27. public function testOptionsInstance(){
  28. $this->assertInstanceOf(QRImageOptions::class, $this->options);
  29. $this->assertEquals(QRCode::OUTPUT_IMAGE_PNG, $this->options->type);
  30. $this->assertEquals(true, $this->options->base64);
  31. }
  32. public function testImageInstance(){
  33. $this->assertInstanceOf(QRImage::class, new QRImage);
  34. }
  35. public function testImageInstanceWithOptionsOverride(){
  36. $this->options->type = 'foobar';
  37. $this->options->pngCompression = 42;
  38. $this->options->jpegQuality = 'OVER 9000!!!';
  39. $this->assertInstanceOf(QRImage::class, new QRImage($this->options));
  40. }
  41. public function imageDataProvider(){
  42. return [
  43. [QRCode::OUTPUT_IMAGE_PNG, 'foobar', 'img1.png.uri'],
  44. [QRCode::OUTPUT_IMAGE_GIF, 'foobar', 'img1.gif.uri'],
  45. [QRCode::OUTPUT_IMAGE_JPG, 'foobar', 'img1.jpg.uri'],
  46. [QRCode::OUTPUT_IMAGE_PNG, 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', 'img2.png.uri'],
  47. [QRCode::OUTPUT_IMAGE_GIF, 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', 'img2.gif.uri'],
  48. [QRCode::OUTPUT_IMAGE_JPG, 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', 'img2.jpg.uri'],
  49. ];
  50. }
  51. /**
  52. * @dataProvider imageDataProvider
  53. */
  54. public function testImageOutput($type, $data, $expected){
  55. $this->options->type = $type;
  56. $output = (new QRCode($data, new QRImage($this->options)))->output();
  57. // jpeg test is causing trouble
  58. if($type !== QRCode::OUTPUT_IMAGE_JPG){
  59. $this->assertEquals(file_get_contents(__DIR__.'/image/'.$expected), $output);
  60. }
  61. }
  62. /**
  63. * @expectedException \chillerlan\QRCode\Output\QRCodeOutputException
  64. * @expectedExceptionMessage Invalid matrix!
  65. */
  66. public function testSetMatrixException(){
  67. (new QRImage)->setMatrix([]);
  68. }
  69. }