Skip to content

Commit

Permalink
Added socket assumption.
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 committed May 17, 2015
1 parent e62245a commit 53a8bc4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Assume.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
class Assume
{
use Extensions\Network;
use Extensions\System;

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Assumptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ function assumeExtensionLoaded()
);
}
}

if (!function_exists('assumeSocket')) {
/**
* Make an assumption and throw
* {@link MehrAlsNix\Assumptions\AssumptionViolatedException} if it fails.
*/
function assumeSocket()
{
$args = func_get_args();
call_user_func_array(
array('MehrAlsNix\Assumptions\Assume', 'assumeSocket'),
$args
);
}
}
39 changes: 39 additions & 0 deletions src/Extensions/Network.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Assumptions
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @copyright 2015 MehrAlsNix (http://www.mehralsnix.de)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://github.com/MehrAlsNix/Assumptions
*/

namespace MehrAlsNix\Assumptions\Extensions;

/**
* Trait Network
* @package MehrAlsNix\Assumptions\Extensions
*/
trait Network
{
private static $SOCK_ERR_MSG = 'Unable to create socket: ';

/**
* @param string $address
* @param int $port
*/
public static function assumeSocket($address, $port = 0)
{
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

assumeThat($socket, not(is(false)), self::$SOCK_ERR_MSG . socket_last_error($socket));
assumeThat(@socket_connect($socket, $address, $port), not(is(false)), 'Unable to connect: ' . $address . ':' . $port);
}
}
4 changes: 4 additions & 0 deletions src/Extensions/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static function assumePhpVersion($atLeast, $message = '')
assumeTrue(version_compare(phpversion(), $atLeast, '>='), $message);
}

/**
* @param string $extension
* @param string $message
*/
public static function assumeExtensionLoaded($extension, $message = '')
{
assumeTrue(extension_loaded($extension), $message);
Expand Down
4 changes: 2 additions & 2 deletions tests/AssumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AssumeTest extends TestCase
/**
* @test
* @expectedException \MehrAlsNix\Assumptions\AssumptionViolatedException
* @expectedExceptionMessage Should stop here...
* @expectedExceptionMessage Unable to connect
*/
public function throwExceptionWithWrongAssumption()
{
Expand All @@ -40,6 +40,6 @@ public function throwExceptionWithWrongAssumption()
assumeNotNull(1, 2, 3);
assumeTrue(true, 'Assume "true" failed.');
assumeFalse(false, 'Assume "false" failed.');
assumeFalse(true, 'Should stop here...');
assumeSocket('127.0.0.1', 123);
}
}

0 comments on commit 53a8bc4

Please sign in to comment.