Skip to content

Instantly share code, notes, and snippets.

View marcelog's full-sized avatar

Marcelo Gornstein marcelog

View GitHub Profile
mkdir -p /usr/cross-freebsd/x86_64-pc-freebsd7/include
mkdir -p /usr/cross-freebsd/x86_64-pc-freebsd7/lib
tar jxf binutils-2.21.tar.bz2
cd binutils-2.21
./configure --enable-libssp --enable-gold --enable-ld --target=x86_64-pc-freebsd7 --prefix=/usr/cross-freebsd
gmake
gmake install
<?php
namespace {
// This allow us to configure the behavior of the "global mock"
$mockSocketCreate = false;
}
namespace My\Nice\Namezpace {
// And this here, does the trick: it will override the socket_create()
// function in your code *just for the namespace* where you are defining it.
// This relies on the code above calling the socket_create function without
phpunit --debug --process-isolation --verbose --stop-on-incomplete --stop-on-skipped --stop-on-failure --stop-on-error --colors --coverage-html html Test_SomeClass.php
@marcelog
marcelog / SomeClass.php
Last active May 8, 2016 19:17
Part of: http://marcelog.github.io/articles/php_mock_global_functions_for_unit_tests_with_phpunit.html Mocking global PHP functions to achieve 100% coverage in our tests
<?php
namespace My\Nice\Namezpace;
class SomeClass
{
public function doSomething()
{
// Notice how the global function "socket_create" is called without
// the leading backslash (relative instead of absolute call in a
// namespaced environment). This will let us later mock the call
$req = "<?xml version='1.0' encoding='UTF-8' ?>;
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:ns1='urn:Something'>
<SOAP-ENV:Body>
<ns1:SomeMethodRequiringAnAttachment>
</ns1:SomeMethodRequiringAnAttachment>
<?php
/**
* Send a POST requst using cURL
* @param string $url to request
* @param array $post values to send
* @param array $options for cURL
* @return string
*/
function curl_post($url, $post = NULL)
{
<?php
function send($url, $msg, $attachments = array())
{
$tempFile = tmpfile();
if ($tempFile === false) {
throw new \Exception('Could not create temp file');
}
$dime = new Net_DIME_Message($tempFile);
$dime->sendData($msg, 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($attachments as $id => $attachment) {
<?php
$file = './a.jpg';
$fileContents = file_get_contents($file);
// This is computed so the attachment id is the md5 sum of the file in question.
$fileMD5 = md5($fileContents);
$result = send($req, array($fileMD5 => $fileContents));
<?php
$myDir = realpath(__DIR__ . '/..');
// Register a cute autoloader for our own files.
spl_autoload_register(function($class) use ($srcDir) {
$file = "$srcDir/$class.php";
if (file_exists($file)) {
require_once $file;
return true;
}
});