amphp / phpunit-util

Helper package to ease testing with PHPUnit.
MIT License
21 stars 8 forks source link

The docs example test is left waiting indefinitely #23

Open programarivm opened 1 year ago

programarivm commented 1 year ago

👋 Hi there,

At ChesslaBlab we're writing functional tests for the PHP Chess Server.

See:

At this moment I'm trying to run the docs example as it is shown in the two images below.

tab_01 Figure 1. php cli/testing.php

tab_02 Figure 2. vendor/bin/phpunit tests/functional/

However, the test is left waiting indefinitely.

<?php

namespace ChessServer\Tests\Functional;

use Amp\ByteStream;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Socket;

class StartCommandTest extends AsyncTestCase
{
    public function test(): void
    {
        $socket = Socket\connect('tcp://127.0.0.1:8080');

        $socket->write('/start classical fen');

        $expected = '{"\/start":{"variant":"classical","mode":"fen","fen":"rnbqkbnr\/pppppppp\/8\/8\/8\/8\/PPPPPPPP\/RNBQKBNR w KQkq -"}}';

        $this->assertSame($expected, ByteStream\buffer($socket));
    }
}

Also this is the cli/testing.php script running the TCP socket server on port 8080 shown in Figure 1.

<?php

namespace ChessServer\Cli;

use ChessServer\Socket\TcpSocket;
use Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../');
$dotenv->load();

$server = new TcpSocket($_ENV['TCP_PORT']);

Any help would be greatly appreciated.

Thank you,

kelunik commented 1 year ago

Hi @programarivm, you're buffering the socket contents until the socket closes, but the socket is likely not closed in that test, so the test hangs.

trowski commented 1 year ago

Hi @programarivm!

Amp\ByteStream\buffer() waits until the stream has closed to return the buffered content. Are you expecting $socket to have closed after the response is received? If not, rather than buffer() you'll want to consider reading in loop until the expected payload is received, using Amp\ByteStream\BufferedReader, or if the JSON is line-delimited, you may be able to use Amp\ByteStream\parseLineDelimitedJson().

If the above isn't the issue, I can pull the repos and investigate further.

programarivm commented 1 year ago

Thank you @kelunik and @trowski for the help.

Not too sure about this one yet. No worries, I suppose for the time being the Selenium automated tests will be kind of equivalent to the so-called functional tests.