|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaminasTest\Mail\Protocol\Pop3\Xoauth2; |
| 6 | + |
| 7 | +use Laminas\Mail\Exception\RuntimeException; |
| 8 | +use Laminas\Mail\Protocol\Pop3\Response; |
| 9 | +use Laminas\Mail\Protocol\Pop3\Xoauth2\Microsoft; |
| 10 | +use Laminas\Mail\Protocol\Xoauth2\Xoauth2; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +use function fopen; |
| 14 | +use function rewind; |
| 15 | +use function str_replace; |
| 16 | +use function stream_get_contents; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers Laminas\Mail\Protocol\Pop3\Xoauth2\Microsoft |
| 20 | + */ |
| 21 | +class MicrosoftTest extends TestCase |
| 22 | +{ |
| 23 | + /** @psalm-suppress InternalClass */ |
| 24 | + public function testIntegration(): void |
| 25 | + { |
| 26 | + /** |
| 27 | + * @psalm-suppress PropertyNotSetInConstructor |
| 28 | + * @psalm-suppress InvalidExtendClass |
| 29 | + */ |
| 30 | + $protocol = new class () extends Microsoft { |
| 31 | + private string $step; |
| 32 | + |
| 33 | + /** @psalm-suppress InternalClass */ |
| 34 | + public function readRemoteResponse(): Response |
| 35 | + { |
| 36 | + if ($this->step === self::AUTH_INITIALIZE_REQUEST) { |
| 37 | + /** @psalm-suppress InternalMethod */ |
| 38 | + return new Response(self::AUTH_RESPONSE_INITIALIZED_OK, 'Auth initialized'); |
| 39 | + } |
| 40 | + |
| 41 | + /** @psalm-suppress InternalMethod */ |
| 42 | + return new Response('+OK', 'Authenticated'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Send a request |
| 47 | + * |
| 48 | + * @param string $request your request without newline |
| 49 | + * @throws RuntimeException |
| 50 | + */ |
| 51 | + public function sendRequest($request): void |
| 52 | + { |
| 53 | + $this->step = $request; |
| 54 | + parent::sendRequest($request); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Open connection to POP3 server |
| 59 | + * |
| 60 | + * @param string $host hostname or IP address of POP3 server |
| 61 | + * @param int|null $port of POP3 server, default is 110 (995 for ssl) |
| 62 | + * @param string|bool $ssl use 'SSL', 'TLS' or false |
| 63 | + * @throws RuntimeException |
| 64 | + * @return string welcome message |
| 65 | + */ |
| 66 | + public function connect($host, $port = null, $ssl = false) |
| 67 | + { |
| 68 | + $this->socket = fopen("php://memory", 'rw+'); |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return null|resource |
| 74 | + */ |
| 75 | + public function getSocket() |
| 76 | + { |
| 77 | + return $this->socket; |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + $protocol->connect('localhost', 0, false); |
| 82 | + |
| 83 | + $protocol->login('test@example.com', '123'); |
| 84 | + |
| 85 | + $this->assertInstanceOf(Microsoft::class, $protocol); |
| 86 | + |
| 87 | + $streamContents = ''; |
| 88 | + if ($socket = $protocol->getSocket()) { |
| 89 | + rewind($socket); |
| 90 | + $streamContents = stream_get_contents($socket); |
| 91 | + $streamContents = str_replace("\r\n", "\n", $streamContents); |
| 92 | + } |
| 93 | + |
| 94 | + /** @psalm-suppress InternalMethod */ |
| 95 | + $xoauth2Sasl = Xoauth2::encodeXoauth2Sasl('test@example.com', '123'); |
| 96 | + |
| 97 | + $this->assertEquals( |
| 98 | + 'AUTH XOAUTH2' . "\n" . $xoauth2Sasl . "\n", |
| 99 | + $streamContents |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments