-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractFactoryTest.php
126 lines (97 loc) · 3.67 KB
/
AbstractFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* This file is part of coisa/factory.
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*
* @link https://github.com/coisa/factory
*
* @copyright Copyright (c) 2020 Felipe Sayão Lobato Abreu <github@felipeabreu.com.br>
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace CoiSA\Factory;
use PHPUnit\Framework\TestCase;
/**
* Class AbstractFactoryTest.
*
* @package CoiSA\Factory
*/
final class AbstractFactoryTest extends TestCase implements AbstractFactoryInterface
{
private $container;
public function setUp()
{
$this->container = $this->prophesize('Psr\\Container\\ContainerInterface');
}
public function testCreateWithContainerWillReturnContainerFactory()
{
$this->markTestIncomplete();
$class = \uniqid('class', false);
$object = new \stdClass();
$object->class = $class;
$this->container->has($class)->willReturn(true);
$this->container->get($class)->willReturn($object);
AbstractFactory::setContainer($this->container->reveal());
$containerFactory = AbstractFactory::create($class);
self::assertInstanceOf('CoiSA\\Factory\\ContainerFactory', $containerFactory);
self::assertSame($object, $containerFactory->create());
}
public function testGetFactoryWithStringFactoryGivenWillReturnFactoryInstance()
{
$objectClass = \uniqid('class', false);
$factoryClass = 'CoiSA\\Factory\\Stub\\Factory\\TestFactory';
AbstractFactory::setFactory($objectClass, $factoryClass);
$factory = AbstractFactory::getFactory($objectClass);
self::assertInstanceOf($factoryClass, $factory);
}
/**
* @expectedException \ArgumentCountError
*/
public function testCreateWithoutArgumentsWillThrowBadMethodCallException()
{
AbstractFactory::create();
}
/**
* @expectedException \ReflectionException
*/
public function testCreateWithNonExistentClassWillThrowException()
{
AbstractFactory::create(__NAMESPACE__ . '\\' . \uniqid('Test', false));
}
public function testCreateWithoutArgumentsReturnObjectOnClassWithoutConstructor()
{
$object = AbstractFactory::create(__NAMESPACE__ . '\\Stub\\ClassWithoutConstructor');
self::assertInstanceOf(__NAMESPACE__ . '\\Stub\\ClassWithoutConstructor', $object);
}
public function testCreateWithoutArgumentsReturnInitializedObjectOnClassWithoutArgumentConstructor()
{
$object = AbstractFactory::create(__NAMESPACE__ . '\\Stub\\ConstructorWithoutArgument');
self::assertInstanceOf(__NAMESPACE__ . '\\Stub\\ConstructorWithoutArgument', $object);
self::assertStringStartsWith('test', $object->argument);
}
public static function create()
{
$object = new \stdClass();
$object->argv = \func_get_args();
return $object;
}
public function testCreateWithAbstractFactoryImplementationWillReturnCreateFromGivenClass()
{
$class = \get_called_class();
$arg1 = \uniqid('arg1', true);
$arg2 = \uniqid('arg2', true);
$object = AbstractFactory::create($class, $arg1, $arg2);
self::assertInstanceOf('stdClass', $object);
self::assertEquals(array($arg1, $arg2), $object->argv);
}
public function testGetFactoryWillReturnGivenSetFactory()
{
$class = \get_called_class();
$factory = new CallableFactory(function() {
return true;
});
AbstractFactory::setFactory($class, $factory);
self::assertSame($factory, AbstractFactory::getFactory($class));
}
}