Passed
Branch main (d4eae3)
by Chema
03:50
created

FactorySingletonTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
eloc 11
c 1
b 0
f 1
dl 0
loc 29
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$1 ➔ test_without_singleton_returns_new_instance() 0 13 1
A hp$0 ➔ test_singleton_returns_same_instance() 0 12 1
A hp$1 ➔ createObject() 0 3 1
test_without_singleton_returns_new_instance() 0 13 ?
A hp$0 ➔ createObject() 0 3 1
test_singleton_returns_same_instance() 0 12 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Framework\Factory;
6
7
use Gacela\Framework\AbstractFactory;
8
use GacelaTest\Fixtures\CustomClass;
9
use GacelaTest\Fixtures\CustomInterface;
10
use PHPUnit\Framework\TestCase;
11
12
final class FactorySingletonTest extends TestCase
13
{
14
    public function test_singleton_returns_same_instance(): void
15
    {
16
        $factory = new class() extends AbstractFactory {
17
            public function createObject(): CustomInterface
18
            {
19
                return $this->singleton(CustomInterface::class, static fn (): CustomClass => new CustomClass());
20
            }
21
        };
22
        $a = $factory->createObject();
23
        $b = $factory->createObject();
24
25
        self::assertSame($a, $b);
26
    }
27
28
    public function test_without_singleton_returns_new_instance(): void
29
    {
30
        $factory = new class() extends AbstractFactory {
31
            public function createObject(): CustomInterface
32
            {
33
                return new CustomClass();
34
            }
35
        };
36
37
        $a = $factory->createObject();
38
        $b = $factory->createObject();
39
40
        self::assertNotSame($a, $b);
41
    }
42
}
43