Skip to content

Instantly share code, notes, and snippets.

View giorgiosironi's full-sized avatar

Giorgio Sironi giorgiosironi

View GitHub Profile
function doSomethingRef(&$o) {
$o = null;
}
function doSomething($o) {
$o = null;
}
$object = new stdClass;
// passed by reference
doSomethingRef($object); // $object is now null
@giorgiosironi
giorgiosironi / gist:1064921
Created July 5, 2011 14:21
Non existent methods cannot be mocked
<?php
class MyMethodOfficiallyDoesNotExist
{
public function __call($method, $args) {
return true;
}
}
class MockingNotExistingMethodsTest extends PHPUnit_Framework_TestCase
{
@giorgiosironi
giorgiosironi / gist:1197279
Created September 6, 2011 11:05
Assign constructor parameters to private properties
<?php
class AutomatedAssignmentOfConstructorParametersTest extends PHPUnit_Framework_TestCase
{
public function testParametersAreAssignedBasingOnTheirNames()
{
$object = new MyClass(1, 2);
$this->assertEquals(1, $object->getFirst());
$this->assertEquals(2, $object->getSecond());
}
}
@giorgiosironi
giorgiosironi / gist:1303801
Created October 21, 2011 13:05
Diff for a unit test freed from the Test Double for an external Adapter
diff --git a/test/it/polimi/hermes/LinkedInGroupAdapterTest.java b/test/it/polimi/hermes/LinkedInGroupAdapterTest.java
index c769aff..f4142c5 100644
--- a/test/it/polimi/hermes/LinkedInGroupAdapterTest.java
+++ b/test/it/polimi/hermes/LinkedInGroupAdapterTest.java
@@ -1,44 +1,45 @@
package it.polimi.hermes;
import static org.junit.Assert.assertEquals;
-
+import static org.mockito.Mockito.*;
@giorgiosironi
giorgiosironi / gist:1400405
Created November 28, 2011 13:31
With PHP 5.4, you can add setters to an object on the fly! [/sarcasm]
class ObjectEncapsulatingState
{
private $number;
public function __construct($number)
{
$this->number = $number;
}
}
<?php
namespace IBL {
class FranchiseMapper {}
}
namespace {
var_dump(new IBL\FranchiseMapper);
$closure = function() {
return new IBL\FranchiseMapper;
};
var_dump($closure());
@giorgiosironi
giorgiosironi / gist:1992039
Created March 7, 2012 08:57
PHPUnit's assertNull() failing
<?php
class Unprintable
{
public function __construct($parent = NULL, $nestingLimit = 4)
{
$this->parent = $parent;
if ($nestingLimit == 0) {
return;
}
for ($i = 0; $i < 10; $i++) {
<?php
function byReference(&$d)
{
$d = new DateTime('1990-01-01');
}
function byValue($d)
{
$d = new DateTime('1990-01-01');
}
class MyClass
{
function __construct(Collaborator %collaborator)
{
}
}
// vs.
class MyClass
// phake
Phake::mock('Namespace\CollaboratorClass')
// proposed alternative (atoum)
new \mock\Namespace\CollaboratorClass()
// Mockito (Java)
package Namespace;
mock(CollaboratorClass.class);