|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MercadoPago\AdbPayment\Test\Unit\Controller\Index; |
| 4 | + |
| 5 | +use Magento\Framework\App\Request\Http as Request; |
| 6 | +use Magento\Framework\App\Response\Http as Response; |
| 7 | +use Magento\Framework\App\Action\Context; |
| 8 | +use MercadoPago\AdbPayment\Controller\Index\GenerateQrCode; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class GenerateQrCodeTest extends TestCase |
| 12 | +{ |
| 13 | + private $controller; |
| 14 | + private $requestMock; |
| 15 | + private $responseMock; |
| 16 | + private $contextMock; |
| 17 | + private $loggerMock; |
| 18 | + private $responseBody; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->requestMock = $this->createMock(Request::class); |
| 23 | + $this->responseMock = $this->createMock(Response::class); |
| 24 | + $this->contextMock = $this->createMock(Context::class); |
| 25 | + $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class); |
| 26 | + |
| 27 | + $this->contextMock->method('getRequest')->willReturn($this->requestMock); |
| 28 | + $this->contextMock->method('getResponse')->willReturn($this->responseMock); |
| 29 | + |
| 30 | + $this->responseMock->method('setHeader')->willReturnSelf(); |
| 31 | + $this->responseMock->method('setHttpResponseCode')->willReturnSelf(); |
| 32 | + $this->responseMock->method('setBody')->willReturnCallback(function ($body) { |
| 33 | + $this->responseBody = $body; |
| 34 | + return $this->responseMock; |
| 35 | + }); |
| 36 | + |
| 37 | + $this->controller = new GenerateQrCode($this->contextMock, $this->loggerMock); |
| 38 | + } |
| 39 | + |
| 40 | + public function testExecuteWithoutDataParam() |
| 41 | + { |
| 42 | + $this->requestMock->method('getParam')->with('data')->willReturn(null); |
| 43 | + |
| 44 | + $this->loggerMock |
| 45 | + ->expects($this->once()) |
| 46 | + ->method('debug') |
| 47 | + ->with(json_encode(['error' => "'data' parameter not provided."])); |
| 48 | + |
| 49 | + $this->controller->execute(); |
| 50 | + } |
| 51 | + |
| 52 | + public function testExecuteWithInvalidBase64() |
| 53 | + { |
| 54 | + $this->requestMock->method('getParam')->with('data')->willReturn('invalid_base64'); |
| 55 | + |
| 56 | + $this->loggerMock |
| 57 | + ->expects($this->once()) |
| 58 | + ->method('debug') |
| 59 | + ->with(json_encode(['error' => "imagecreatefromstring(): Data is not in a recognized format"])); |
| 60 | + |
| 61 | + $this->controller->execute(); |
| 62 | + } |
| 63 | + |
| 64 | + public function testExecuteWithValidBase64() |
| 65 | + { |
| 66 | + $image = imagecreatetruecolor(100, 100); |
| 67 | + ob_start(); |
| 68 | + imagepng($image); |
| 69 | + $imageData = ob_get_clean(); |
| 70 | + imagedestroy($image); |
| 71 | + |
| 72 | + $base64Image = base64_encode($imageData); |
| 73 | + |
| 74 | + $this->requestMock->method('getParam')->with('data')->willReturn($base64Image); |
| 75 | + |
| 76 | + $this->controller->execute(); |
| 77 | + |
| 78 | + $this->assertNotEmpty($this->responseBody); |
| 79 | + $this->assertStringContainsString("\x89PNG", $this->responseBody); |
| 80 | + } |
| 81 | +} |
0 commit comments