      1: <?php
      2:
      3: /**
      4:  * Test cases for implementations of the AlphaFilterInterface
      5:  *
      6:  * @package alpha::tests
      7:  * @since 1.2.2
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: AlphaPHPServerUtils_Test.php 1672 2013-10-15 20:34:29Z alphadevx $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class AlphaPHPServerUtils_Test extends PHPUnit_Framework_TestCase {
     48:
     49:     /**
     50:      * Called before the test functions will be executed
     51:      * this function is defined in PHPUnit_TestCase and overwritten
     52:      * here
     53:      *
     54:      * @since 1.2.2
     55:      */
     56:     protected function setUp() {}
     57:
     58:     /**
     59:      * Called after the test functions are executed
     60:      * this function is defined in PHPUnit_TestCase and overwritten
     61:      * here
     62:      *
     63:      * @since 1.2.2
     64:      */
     65:     protected function tearDown() {}
     66:
     67:     /**
     68:      * Testing that we can start the server and hit it with a curl request
     69:      *
     70:      * @since 1.2.2
     71:      */
     72:     public function testStart() {
     73:         $pid = AlphaPHPServerUtils::start('localhost','8081','.');
     74:         sleep(1); // wait a second to give the server time to start...
     75:
     76:         $this->assertTrue($pid > 0, 'Testing that a PID was returned after starting the server');
     77:
     78:         $ch = curl_init();
     79:         curl_setopt($ch, CURLOPT_URL, 'http://localhost:8081/');
     80:         $result = curl_exec($ch);
     81:
     82:         $this->assertEquals(404, curl_getinfo($ch, CURLINFO_HTTP_CODE), 'Testing that the server returns a 404 not found');
     83:
     84:         if(!empty($pid))
     85:             AlphaPHPServerUtils::stop($pid);
     86:     }
     87:
     88:     /**
     89:      * Testing that we can stop the server and hit it with a curl request
     90:      *
     91:      * @since 1.2.2
     92:      */
     93:     public function testStop() {
     94:         $pid = AlphaPHPServerUtils::start('localhost','8081','.');
     95:         sleep(1); // wait a second to give the server time to start...
     96:
     97:         $this->assertTrue($pid > 0, 'Testing that a PID was returned after starting the server');
     98:
     99:         $ch = curl_init();
    100:         curl_setopt($ch, CURLOPT_URL, 'http://localhost:8081/');
    101:         curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    102:         $result = curl_exec($ch);
    103:
    104:         $this->assertEquals(404, curl_getinfo($ch, CURLINFO_HTTP_CODE), 'Testing that the server returns a 404 not found');
    105:
    106:         if(!empty($pid)) {
    107:             AlphaPHPServerUtils::stop($pid);
    108:             sleep(1); // wait a second to give the server time to stop...
    109:         }
    110:
    111:         $result = curl_exec($ch);
    112:
    113:         $this->assertEquals(7, curl_errno($ch), 'Testing that second request after the server shutdown failed due being unable to connect');
    114:     }
    115:
    116:     /**
    117:      * Testing that we can check the status of the server when stopped or running
    118:      *
    119:      * @since 1.2.2
    120:      */
    121:     public function testStatus() {
    122:         $pid = AlphaPHPServerUtils::start('localhost','8081','.');
    123:
    124:         $this->assertTrue(AlphaPHPServerUtils::status($pid), 'Testing that the status of the server is true when it is running');
    125:
    126:         if(!empty($pid))
    127:             AlphaPHPServerUtils::stop($pid);
    128:
    129:         $this->assertFalse(AlphaPHPServerUtils::status($pid), 'Testing that the status of the server is false when it is stopped');
    130:     }
    131: }
    132:
    133: ?>