1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DansMaCulotte\Newsletter\Test; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
6
|
|
|
use DansMaCulotte\Newsletter\NewsletterList; |
7
|
|
|
use DansMaCulotte\Newsletter\NewsletterListCollection; |
8
|
|
|
use DansMaCulotte\Newsletter\Exceptions\InvalidNewsletterList; |
9
|
|
|
|
10
|
|
|
class NewsletterListCollectionTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
protected $newsletterListCollection; |
13
|
|
|
|
14
|
|
|
public function setUp() : void |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->newsletterListCollection = NewsletterListCollection::createFromConfig( |
19
|
|
|
[ |
20
|
|
|
'lists' => [ |
21
|
|
|
'list1' => ['id' => 1], |
22
|
|
|
'list2' => ['id' => 2], |
23
|
|
|
'list3' => ['id' => 3], |
24
|
|
|
], |
25
|
|
|
'defaultList' => 'list3', |
26
|
|
|
] |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @test */ |
31
|
|
View Code Duplication |
public function it_can_find_a_list_by_its_name() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$list = $this->newsletterListCollection->findByName('list2'); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf(NewsletterList::class, $list); |
36
|
|
|
|
37
|
|
|
$this->assertEquals(2, $list->getId()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @test */ |
41
|
|
View Code Duplication |
public function it_will_use_the_default_list_when_not_specifing_a_listname() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$list = $this->newsletterListCollection->findByName(''); |
44
|
|
|
|
45
|
|
|
$this->assertInstanceOf(NewsletterList::class, $list); |
46
|
|
|
|
47
|
|
|
$this->assertEquals(3, $list->getId()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @test */ |
51
|
|
|
public function it_will_throw_an_exception_when_using_a_default_list_that_does_not_exist() |
52
|
|
|
{ |
53
|
|
|
$newsletterListCollection = NewsletterListCollection::createFromConfig( |
54
|
|
|
[ |
55
|
|
|
'lists' => [ |
56
|
|
|
'list1' => ['id' => 'list1'], |
57
|
|
|
], |
58
|
|
|
|
59
|
|
|
'defaultList' => 'list2', |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->expectException(InvalidNewsletterList::class); |
64
|
|
|
|
65
|
|
|
$newsletterListCollection->findByName(''); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @test */ |
69
|
|
|
public function it_will_throw_an_exception_when_there_are_no_lists_defined() |
70
|
|
|
{ |
71
|
|
|
$this->expectException(InvalidNewsletterList::class); |
72
|
|
|
|
73
|
|
|
NewsletterListCollection::createFromConfig( |
74
|
|
|
[ |
75
|
|
|
'lists' => [ |
76
|
|
|
], |
77
|
|
|
|
78
|
|
|
'defaultList' => 'list1', |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @test */ |
84
|
|
|
public function it_will_throw_an_exception_when_trying_to_find_a_list_that_does_not_exist() |
85
|
|
|
{ |
86
|
|
|
$this->expectException(InvalidNewsletterList::class); |
87
|
|
|
|
88
|
|
|
$this->newsletterListCollection->findByName('blabla'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: