donatj / mock-webserver

Simple mock web server in PHP for unit testing.
MIT License
131 stars 21 forks source link

Server start blocks #7

Closed lajthabalazs closed 4 months ago

lajthabalazs commented 6 years ago

This was just the library I was looking for to test out a HTTP client layer I was writing for an application. Enabled sockets extension in php.ini (running PHP version 7.2.1 on Windows), and added mock-webserver with composer as indicated.

I was following the PHPUnit example, wrote the following "test" to check out the server:

<?php
/**
 * Created by PhpStorm.
 * User: lajtha
 * Date: 2018-02-10
 * Time: 07:34
 */

namespace Test\Client\HTTP;

use donatj\MockWebServer\MockWebServer;
use PHPUnit\Framework\TestCase;

class HTTPClientImplTest extends TestCase
{
    protected static $server;

    public static function setUpBeforeClass()
    {
        print ("Starting server.\n");
        self::$server = new MockWebServer(8084);
        self::$server->start();
        print ("Server started.\n");
    }

    public function testSimpleGet()
    {
        print ("Waiting a bit. \n");
        sleep(120);
        print ("No more waiting.\n");
    }

    public static function tearDownAfterClass()/* The :void return type declaration that should be here would cause a BC issue */
    {
        print ("Server stopping.\n");
        self::$server->stop();
        print ("Server stopped.\n");
    }
}

I expect the output to be:

Starting server. Server started. Waiting a bit. No more waiting. Server stopping. Server stopped.

But just get: Starting server.

The server starts up, and is operational, but doesn't run the tests. Tried from the command line and from PhpStorms too. I don't have much experience with PHP, tried to add some threading but found that it's not as easy as in Java. How could I make the test work? Thanks, Balázs

donatj commented 6 years ago

I am sorry, I should add a note. This is not currently tested nor supported in Windows I am afraid.

The way it is currently implemented is highly dependent on Unix-like shells ability to pipe and control forked processes; something I am woefully unfamiliar with in Windows.

donatj commented 6 years ago

I have added a note here.

Sorry for the inconvenience.

donatj commented 5 years ago

This should be fixed in the next major release. See #15 ( thanks to: #13 )