Skip to content

Commit

Permalink
Merge pull request #303 from gquemener/feature/named-constructor
Browse files Browse the repository at this point in the history
Support instanciation of object with named constructor
  • Loading branch information
tshelburne committed Jan 7, 2016
2 parents c05812b + f791f97 commit c8cb341
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function canInstantiate(Fixture $fixture)
{
$reflConstruct = new \ReflectionMethod($fixture->getClass(), '__construct');

return !$reflConstruct->isPublic() || (!$fixture->shouldUseConstructor() && !version_compare(PHP_VERSION, '5.4', '<'));
return (!$reflConstruct->isPublic() && '__construct' === $fixture->getConstructorMethod()) || (!$fixture->shouldUseConstructor() && !version_compare(PHP_VERSION, '5.4', '<'));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Nelmio/Alice/Fixtures/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LoaderTest extends \PHPUnit_Framework_TestCase
const GROUP = 'Nelmio\Alice\support\models\Group';
const CONTACT = 'Nelmio\Alice\support\models\Contact';
const PRIVATE_CONSTRUCTOR_CLASS = 'Nelmio\Alice\support\models\PrivateConstructorClass';
const NAMED_CONSTRUCTOR_CLASS = 'Nelmio\Alice\support\models\NamedConstructorClass';

/**
* @var \Nelmio\Alice\Fixtures\Loader
Expand Down Expand Up @@ -105,6 +106,20 @@ public function testCreatePrivateConstructorInstance()
$this->assertInstanceOf(self::PRIVATE_CONSTRUCTOR_CLASS, $res['test1']);
}

public function testCreateNamedConstructorInstance()
{
$res = $this->loadData([
self::NAMED_CONSTRUCTOR_CLASS => [
'foo' => [
'__construct' => ['withLambda' => ['λ']],
],
],
]);

$this->assertInstanceOf(self::NAMED_CONSTRUCTOR_CLASS, $res['foo']);
$this->assertSame('λ', $res['foo']->lambda);
}

public function testLoadInvalidFile()
{
$file = __DIR__.'/../support/fixtures/invalid.php';
Expand Down
18 changes: 18 additions & 0 deletions tests/Nelmio/Alice/support/models/NamedConstructorClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Nelmio\Alice\support\models;

class NamedConstructorClass
{
public $lambda;

private function __construct($lambda)
{
$this->lambda = $lambda;
}

public static function withLambda($lambda)
{
return new self($lambda);
}
}

0 comments on commit c8cb341

Please sign in to comment.