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 1745 2014-03-29 15:19:05Z alphadevx $
10: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
11: * @copyright Copyright (c) 2014, 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: ob_start();
81: $result = curl_exec($ch);
82: ob_end_clean();
83:
84: $this->assertEquals(404, curl_getinfo($ch, CURLINFO_HTTP_CODE), 'Testing that the server returns a 404 not found');
85:
86: if(!empty($pid))
87: AlphaPHPServerUtils::stop($pid);
88: }
89:
90: /**
91: * Testing that we can stop the server and hit it with a curl request
92: *
93: * @since 1.2.2
94: */
95: public function testStop() {
96: $pid = AlphaPHPServerUtils::start('localhost','8081','.');
97: sleep(1); // wait a second to give the server time to start...
98:
99: $this->assertTrue($pid > 0, 'Testing that a PID was returned after starting the server');
100:
101: $ch = curl_init();
102: curl_setopt($ch, CURLOPT_URL, 'http://localhost:8081/');
103: curl_setopt($ch, CURLOPT_TIMEOUT, 3);
104: ob_start();
105: $result = curl_exec($ch);
106: ob_end_clean();
107:
108: $this->assertEquals(404, curl_getinfo($ch, CURLINFO_HTTP_CODE), 'Testing that the server returns a 404 not found');
109:
110: if(!empty($pid)) {
111: AlphaPHPServerUtils::stop($pid);
112: sleep(1); // wait a second to give the server time to stop...
113: }
114:
115: $result = curl_exec($ch);
116:
117: $this->assertEquals(7, curl_errno($ch), 'Testing that second request after the server shutdown failed due being unable to connect');
118: }
119:
120: /**
121: * Testing that we can check the status of the server when stopped or running
122: *
123: * @since 1.2.2
124: */
125: public function testStatus() {
126: $pid = AlphaPHPServerUtils::start('localhost','8081','.');
127:
128: $this->assertTrue(AlphaPHPServerUtils::status($pid), 'Testing that the status of the server is true when it is running');
129:
130: if(!empty($pid))
131: AlphaPHPServerUtils::stop($pid);
132:
133: $this->assertFalse(AlphaPHPServerUtils::status($pid), 'Testing that the status of the server is false when it is stopped');
134: }
135: }
136:
137: ?>