Skip to content

Commit 88816a5

Browse files
committed
Merge pull request #4 from sergeylukin/assign-properties-after-instantiation
Allow setting properties after instantiation
2 parents dab8818 + 5b4357f commit 88816a5

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ $stub = new Stub([
1818
'answer' => 42,
1919
'callMe' => function($a) { return $a; } # Anything which is a callable
2020
]);
21+
$stub->foo = 'bar';
22+
$stub->myMethod = function() { return 50; };
2123
echo $stub->answer(); # => 42
2224
echo $stub->answer; # => 42
2325
echo $stub->callMe('maybe'); # => 'maybe'
26+
echo $stub->foo; # => 'bar'
27+
echo $stub->myMethod(); # => 50
2428
echo $stub->lol()->hey(); # => $stub
2529

2630
// Different configurations

src/Nulpunkt/PhpStub/Stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function __get($name)
4444
}
4545
return null;
4646
}
47+
48+
public function __set($name, $value)
49+
{
50+
$this->methods[$name] = $value;
51+
}
4752

4853
public function __isset($property)
4954
{

test/Nulpunkt/PhpStub/StubTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ public function testThatAStubRespondToDefinedMethods()
1212
$this->assertSame(42, $stub->getId());
1313
}
1414

15+
public function testThatMethodsCanBeAssignedAfterInstantiation()
16+
{
17+
$stub = new Stub();
18+
$stub->myMethod = function() { return 42; };
19+
$this->assertSame(42, $stub->myMethod());
20+
}
21+
22+
public function testThatPropertiesCanBeAssignedAfterInstantiation()
23+
{
24+
$stub = new Stub();
25+
$stub->foo = 'bar';
26+
$this->assertSame('bar', $stub->foo);
27+
}
28+
1529
public function testThatItHasAllProperties()
1630
{
1731
$stub = new Stub();

0 commit comments

Comments
 (0)