Completed
Pull Request — master (#4)
by Timóteo
05:46
created

RepositoryFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TZachi\PhalconRepository\Tests\Functional;
6
7
use Phalcon\Annotations\Adapter\Memory as MemoryAdapter;
8
use RuntimeException;
9
use TZachi\PhalconRepository\Repository;
10
use TZachi\PhalconRepository\RepositoryFactory;
11
use TZachi\PhalconRepository\Tests\Mock\Model\Company;
12
use TZachi\PhalconRepository\Tests\Mock\Model\CompanyAnnotationAbsent;
13
use TZachi\PhalconRepository\Tests\Mock\Model\CompanyInvalid;
14
use TZachi\PhalconRepository\Tests\Mock\Model\CompanyMissing;
15
use TZachi\PhalconRepository\Tests\Mock\Model\CompanyRepositoryAnnotationAbsent;
16
use TZachi\PhalconRepository\Tests\Mock\Repository\Company as CompanyRepository;
17
use function get_class;
18
19
final class RepositoryFactoryTest extends TestCase
20
{
21
    /**
22
     * @var MemoryAdapter
23
     */
24
    private $annotations;
25
26
    /**
27
     * @var RepositoryFactory
28
     */
29
    private $factory;
30
31
    /**
32
     * @before
33
     */
34
    public function createDependencies(): void
35
    {
36
        $this->annotations = new MemoryAdapter();
37
        $this->factory     = new RepositoryFactory($this->annotations);
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function createShouldUseRepositoryInAnnotations(): void
44
    {
45
        self::assertInstanceOf(CompanyRepository::class, $this->factory->create(Company::class));
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function createShouldUseDefaultRepositoryWhenThereAreNoAnnotations(): void
52
    {
53
        // Make sure that the result repository is not an instance of a Repository subclass, but the actual class
54
        self::assertSame(Repository::class, get_class($this->factory->create(CompanyAnnotationAbsent::class)));
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function createShouldUseDefaultRepositoryWhenRepositoryAnnotationWasNotSpecified(): void
61
    {
62
        // Make sure that the result repository is not an instance of a Repository subclass, but the actual class
63
        self::assertSame(
64
            Repository::class,
65
            get_class($this->factory->create(CompanyRepositoryAnnotationAbsent::class))
66
        );
67
    }
68
69
    /**
70
     * @test
71
     * @dataProvider createInvalidData
72
     */
73
    public function createShouldThrowExceptionWithInvalidAnnotation(string $className): void
74
    {
75
        $this->expectException(RuntimeException::class);
76
        $this->expectExceptionMessageRegExp("/Repository class '[^']*' doesn't exists/i");
77
78
        $this->factory->create($className);
79
    }
80
81
    /**
82
     * @return string[][]
83
     */
84
    public function createInvalidData(): array
85
    {
86
        return [
87
            'Invalid class' => ['className' => CompanyInvalid::class],
88
            'Missing argument' => ['className' => CompanyMissing::class],
89
        ];
90
    }
91
}
92