namedConstruct() public static method

public static namedConstruct ( )
コード例 #1
0
 public function testInstantiatesObjectWithFactory()
 {
     $fixture = new SimpleFixture('dummy', DummyWithNamedConstructor::class, SpecificationBagFactory::create(new MethodCallWithReference(new StaticReference(DummyWithNamedConstructor::class), 'namedConstruct')));
     $set = $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext());
     $expected = DummyWithNamedConstructor::namedConstruct();
     $actual = $set->getObjects()->get($fixture)->getInstance();
     $this->assertEquals($expected, $actual);
 }
コード例 #2
0
 public function provideFixturesToInstantiate()
 {
     (yield 'with default constructor – use default constructor' => [[DummyWithDefaultConstructor::class => ['dummy' => []]], new DummyWithDefaultConstructor()]);
     (yield 'with explicit default constructor - use constructor' => [[DummyWithExplicitDefaultConstructor::class => ['dummy' => []]], new DummyWithExplicitDefaultConstructor()]);
     (yield 'with named constructor - use factory function' => [[DummyWithNamedConstructor::class => ['dummy' => ['__construct' => ['namedConstruct' => []]]]], DummyWithNamedConstructor::namedConstruct()]);
     (yield 'with default constructor and optional parameters without parameters - use constructor function' => [[DummyWithOptionalParameterInConstructor::class => ['dummy' => []]], new DummyWithOptionalParameterInConstructor()]);
     (yield 'with default constructor and optional parameters without parameters - use constructor function' => [[DummyWithOptionalParameterInConstructor::class => ['dummy' => []]], new DummyWithOptionalParameterInConstructor()]);
     (yield 'with default constructor and optional parameters with parameters - use constructor function' => [[DummyWithOptionalParameterInConstructor::class => ['dummy' => ['__construct' => [100]]]], new DummyWithOptionalParameterInConstructor(100)]);
     (yield 'with default constructor and required parameters with no parameters - throw exception' => [[DummyWithRequiredParameterInConstructor::class => ['dummy' => []]], null]);
     (yield 'with default constructor and required parameters with parameters - use constructor function' => [[DummyWithRequiredParameterInConstructor::class => ['dummy' => ['__construct' => [100]]]], new DummyWithRequiredParameterInConstructor(100)]);
     (yield 'with default constructor and required parameters with parameters and unique value - use constructor function' => [[DummyWithRequiredParameterInConstructor::class => ['dummy' => ['__construct' => ['0 (unique)' => 100]]]], new DummyWithRequiredParameterInConstructor(100)]);
     (yield 'with named constructor and optional parameters with no parameters - use factory function' => [[DummyWithNamedConstructorAndOptionalParameters::class => ['dummy' => ['__construct' => ['namedConstruct' => []]]]], DummyWithNamedConstructorAndOptionalParameters::namedConstruct()]);
     (yield 'with named constructor and optional parameters with parameters - use factory function' => [[DummyWithNamedConstructorAndOptionalParameters::class => ['dummy' => ['__construct' => ['namedConstruct' => [100]]]]], DummyWithNamedConstructorAndOptionalParameters::namedConstruct(100)]);
     (yield 'with named constructor and optional parameters with parameters and unique value - use factory function' => [[DummyWithNamedConstructorAndOptionalParameters::class => ['dummy' => ['__construct' => ['namedConstruct' => ['0 (unique)' => 100]]]]], DummyWithNamedConstructorAndOptionalParameters::namedConstruct(100)]);
     (yield 'with named constructor and required parameters with no parameters - throw exception' => [[DummyWithNamedConstructorAndRequiredParameters::class => ['dummy' => ['__construct' => ['namedConstruct' => []]]]], null]);
     (yield 'with named constructor and required parameters with parameters - use factory function' => [[DummyWithNamedConstructorAndRequiredParameters::class => ['dummy' => ['__construct' => ['namedConstruct' => [100]]]]], DummyWithNamedConstructorAndRequiredParameters::namedConstruct(100)]);
     (yield 'with unknown named constructor' => [[DummyWithDefaultConstructor::class => ['dummy' => ['__construct' => ['unknown' => []]]]], null]);
     (yield 'with private constructor – throw exception' => [[DummyWithPrivateConstructor::class => ['dummy' => []]], null]);
     (yield 'with protected constructor – throw exception' => [[DummyWithProtectedConstructor::class => ['dummy' => []]], null]);
     (yield 'with private named constructor – throw exception' => [[DummyWithNamedPrivateConstructor::class => ['dummy' => ['__construct' => ['namedConstruct' => []]]]], null]);
     (yield 'with default constructor but specified no constructor – use reflection' => [[DummyWithDefaultConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithDefaultConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with explicit constructor but specified no constructor – use reflection' => [[DummyWithExplicitDefaultConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithExplicitDefaultConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with named constructor but specified no constructor – use reflection' => [[DummyWithNamedConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithNamedConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with named constructor and optional parameters but specified no constructor – use reflection' => [[DummyWithNamedConstructorAndOptionalParameters::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithNamedConstructorAndOptionalParameters::class))->newInstanceWithoutConstructor()]);
     (yield 'with named constructor and required parameters but specified no constructor – use reflection' => [[DummyWithNamedConstructorAndRequiredParameters::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithNamedConstructorAndRequiredParameters::class))->newInstanceWithoutConstructor()]);
     (yield 'with optional parameters in constructor but specified no constructor – use reflection' => [[DummyWithOptionalParameterInConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithOptionalParameterInConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with required parameters in constructor but specified no constructor – use reflection' => [[DummyWithRequiredParameterInConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithRequiredParameterInConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with private constructor – use reflection' => [[DummyWithPrivateConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithPrivateConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with protected constructor – use reflection' => [[DummyWithProtectedConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithProtectedConstructor::class))->newInstanceWithoutConstructor()]);
     (yield 'with private named constructor – use reflection' => [[DummyWithNamedPrivateConstructor::class => ['dummy' => ['__construct' => false]]], (new \ReflectionClass(DummyWithNamedPrivateConstructor::class))->newInstanceWithoutConstructor()]);
 }