Petra Barus
Petra Barus

Reputation: 4013

How to start PHP server, run PHPUnit, and stop PHP server in Ant?

I have a target like this in Ant

<target name="test">
    <exec executable="php" failonerror="true">
        <arg value="-S"/>
        <arg value="localhost:80"/>
        <arg value="-t"/>
        <arg value="web"/>
    </exec>
    <exec executable="phpunit" failonerror="true">
        <arg value="tests"/>
    </exec>
</target>

The problem is when I run this, the target will block because of the PHP build-in server. How do I start the PHP server, and then run the PHP unit, and then stop the server when the PHP unit completes (either success or failed)?

Upvotes: 1

Views: 598

Answers (2)

Petra Barus
Petra Barus

Reputation: 4013

I finally got some working solution. To get the PHP server PID, I need to execute ps command and then do regular expression to get the PID and then kill the server.

<project name="myapp" default="test" basedir=".">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="ant-contrib.jar" />
    <target name="start-server">
        <echo>Starting PHP server</echo>
        <exec executable="php" spawn="true">
            <arg value="-S"/>
            <arg value="localhost:8080"/>
            <arg value="-t"/>
            <arg value="${basedir}"/>
        </exec>
        <sleep seconds="1"/>
    </target>
    <target name="stop-server">
        <!-- Getting the PHP server PID -->
        <exec executable="ps">
            <arg value="ax"/>
            <redirector outputproperty="php.ps">
                <outputfilterchain>
                    <linecontains>
                        <contains value="php"/>
                        <contains value="localhost:8080"/>
                    </linecontains>
                </outputfilterchain>
            </redirector>
        </exec>
        <propertyregex property="php.pid" input="${php.ps}" regexp="^\s+(\d+)" select="\1"/>
        <echo>Killing PHP server at ${php.pid}</echo>
        <exec executable="kill">
            <arg value="${php.pid}"/>
        </exec>
    </target>
    <target name="test">
        <antcall target="start-server"></antcall>
        <echo>Starting test</echo>
        <sleep seconds="3"/>
        <echo>Finishing test</echo>
        <antcall target="stop-server"></antcall>
    </target>
</project>

Upvotes: 1

M A
M A

Reputation: 72884

If you want Ant to spawn the php process, you can set spawn="true" in the task invocation:

<exec executable="php" failonerror="true" spawn="true">
    <arg value="-S"/>
    <arg value="localhost:80"/>
    <arg value="-t"/>
    <arg value="web"/>
</exec>

A note on its usage from the documentation though:

If you spawn a command, its output will not be logged by ant. The input, output, error, and result property settings are not active when spawning a process.

Upvotes: 2

Related Questions