0

I want to run a task in Phing where I first run the PHP server and then run a PHP Unit test.

This is what I have so far:

<target name="test">
    <!-- Run the PHP server -->
    <exec executable="php">
        <arg line="-S localhost:81 server.php"/>
    </exec>

    <!-- Run my tests -->
    <exec executable="${phpunit.bin}" dir="${test.dir}" passthru="true" returnProperty="test.result">
        <arg line="IntegrationTests"/>
    </exec>

    <!-- Check if succeeded -->
    <condition property="test.succeeded">
        <equals arg1="${test.result}" arg2="0"/>
    </condition>

    <fail unless="test.succeeded" message="Unit Tests Failed"/>
</target>

The issue is that Phing hangs after creating the PHP server.

The issue is solved by adding the spawn property like so:

<exec executable="php" spawn="true">

This works as expected, except that the process never actually exits even after Phing exits. In other words, the PHP server is still running long after the Phing has completed it's tasks.

Therefore my question is how to properly run a php server in the background in Phing?

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

1 Answers1

1

phing's ExecTask does not tell you the process ID, so you can't simply do a kill $pid.

Doing a killall php will kill phing itself, too :)

The best option (still a hack) is probably to pgrep for php -S localhost and kill that process:

<exec command="pkill -f 'php -S localhost:81'"/>

But you have to do that in every case, even if the build fails - so add that line before checking the succeeded property.

cweiske
  • 30,033
  • 14
  • 133
  • 194