Passed
Branch 2.0.0-dev (fc3d15)
by Jeroen
03:30
created

VCardTest::testMultipleNotAllowedProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JeroenDesloovere\Tests\VCard;
6
7
use JeroenDesloovere\VCard\Formatter\Formatter;
8
use JeroenDesloovere\VCard\Formatter\VcfFormatter;
9
use JeroenDesloovere\VCard\Parser\Parser;
10
use JeroenDesloovere\VCard\Parser\VcfParser;
11
use JeroenDesloovere\VCard\Property\Address;
12
use JeroenDesloovere\VCard\Property\Anniversary;
13
use JeroenDesloovere\VCard\Property\Birthdate;
14
use JeroenDesloovere\VCard\Property\Email;
15
use JeroenDesloovere\VCard\Property\Gender;
16
use JeroenDesloovere\VCard\Property\Logo;
17
use JeroenDesloovere\VCard\Property\Name;
18
use JeroenDesloovere\VCard\Property\Nickname;
19
use JeroenDesloovere\VCard\Property\Note;
20
use JeroenDesloovere\VCard\Property\Parameter\Kind;
21
use JeroenDesloovere\VCard\Property\Parameter\Revision;
22
use JeroenDesloovere\VCard\Property\Parameter\Type;
23
use JeroenDesloovere\VCard\Property\Photo;
24
use JeroenDesloovere\VCard\Property\Telephone;
25
use JeroenDesloovere\VCard\Property\Title;
26
use JeroenDesloovere\VCard\VCard;
27
use org\bovigo\vfs\vfsStream;
28
use org\bovigo\vfs\vfsStreamDirectory;
29
use PHPUnit\Framework\TestCase;
30
31
/**
32
 * How to execute all tests: `vendor/bin/phpunit tests`
33
 */
34
final class VCardTest extends TestCase
35
{
36
    /** @var VCard */
37
    private $firstVCard;
38
39
    /** @var VCard */
40
    private $secondVCard;
41
42
    /** @var VCard */
43
    private $thirdVCard;
44
45
    /** @var vfsStreamDirectory - We save the generated vCard to a virtual storage */
46
    private $virtualStorage;
47
48
    public function setUp(): void
49
    {
50
        $this->setUpFirstVCard();
51
        $this->setUpSecondVCard();
52
        $this->setUpThirdVCard();
53
        $this->virtualStorage = vfsStream::setup();
54
    }
55
56
    private function setUpFirstVCard(): void
57
    {
58
        $this->firstVCard = (new VCard())
59
            ->add(Gender::male('Dude'))
60
            ->add(new Nickname('Web developer'))
61
            ->add(new Name('Desloovere', 'Jeroen'))
62
            ->add(new Address(null, null, 'Markt 1', 'Brugge', 'West-Vlaanderen', '8000', 'België', Type::work()))
63
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::home()))
64
            ->add(new Email('[email protected]', Type::work()))
65
            ->add(new Email('[email protected]', Type::home()))
66
            ->add(new Note('VCard library is amazing.'))
67
            ->add(new Birthdate(new \DateTime('2015-12-05')))
68
            ->add(new Anniversary(new \DateTime('2017-12-05')))
69
            ->add(new Telephone('+33 01 23 45 67'));
70
    }
71
72
    private function setUpSecondVCard(): void
73
    {
74
        $this->secondVCard = (new VCard())
75
            ->add(new Name('Doe', 'John'))
76
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::work()));
77
    }
78
79
    private function setUpThirdVCard(): void
80
    {
81
        $this->thirdVCard = (new VCard(Kind::organization()))
82
            ->add(new Title('Apple'))
83
            ->add(new Photo(__DIR__ . '/assets/landscape.jpeg'))
84
            ->add(new Logo(__DIR__ . '/assets/landscape.jpeg'))
85
            ->add(new Telephone('+32 486 00 00 00'));
86
    }
87
88
    public function testFormatterSavingMultipleVCardsToVcfFile(): void
89
    {
90
        // Saving "vcards-export.vcf"
91
        $formatter = new Formatter(new VcfFormatter(), 'vcards-export');
92
        $formatter->addVCard($this->firstVCard);
93
        $formatter->addVCard($this->secondVCard);
94
95
        $this->assertFalse($this->virtualStorage->hasChild('vcards-export.vcf'));
96
        $formatter->save($this->virtualStorage->url());
97
        $this->assertTrue($this->virtualStorage->hasChild('vcards-export.vcf'));
98
    }
99
100
    public function testFormatterSavingOneVCardToVcfFile(): void
101
    {
102
        // Saving "vcard-export.vcf"
103
        $formatter = new Formatter(new VcfFormatter(), 'vcard-export');
104
        $formatter->addVCard($this->firstVCard);
105
106
        $this->assertFalse($this->virtualStorage->hasChild('vcard-export.vcf'));
107
        $formatter->save($this->virtualStorage->url());
108
        $this->assertTrue($this->virtualStorage->hasChild('vcard-export.vcf'));
109
    }
110
111
    /**
112
     * @expectedException \JeroenDesloovere\VCard\Exception\VCardException
113
     */
114
    public function testMultipleNotAllowedProperties(): void
115
    {
116
        (new VCard())
117
            ->add(new Nickname('Jeroen'))
118
            ->add(new Nickname('Jeroen2'));
119
    }
120
121
    /**
122
     * @expectedException \JeroenDesloovere\VCard\Exception\VCardException
123
     */
124
    public function testMultipleNotAllowedPropertyParameters(): void
125
    {
126
        (new VCard())
127
            ->add(new Revision(new \DateTime))
128
            ->add(new Revision(new \DateTime));
129
    }
130
131
    /**
132
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
133
     * @expectedExceptionMessage File "Lorem ipsum dolor sit amet, consectetur adipiscing elit." is not readable, or doesn't exist.
134
     */
135
    public function testParserCorruptVCard(): void
136
    {
137
        new Parser(new VcfParser(), 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
138
    }
139
140
    /**
141
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
142
     * @expectedExceptionMessage File "" is not readable, or doesn't exist.
143
     */
144
    public function testParserEmptyVCard(): void
145
    {
146
        new Parser(new VcfParser(), '');
147
    }
148
149
    /**
150
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
151
     */
152
    public function testParserGetFileContentsException(): void
153
    {
154
        Parser::getFileContents(__DIR__ . '/not-existing');
155
    }
156
157
    public function testParserMultipleVCardsFromVcfFile(): void
158
    {
159
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcards.vcf'));
160
161
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
162
        $this->assertEquals($this->secondVCard->getProperties(), $parser->getVCards()[1]->getProperties());
163
    }
164
165
    public function testParserOneVCardFromVcfFile(): void
166
    {
167
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcard.vcf'));
168
169
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
170
    }
171
172
    public function testVCardGetProperties(): void
173
    {
174
        $this->assertCount(11, $this->firstVCard->getProperties());
175
        $this->assertCount(1, $this->firstVCard->getProperties(Gender::class));
176
        $this->assertCount(1, $this->firstVCard->getProperties(Nickname::class));
177
        $this->assertCount(1, $this->firstVCard->getProperties(Name::class));
178
        $this->assertCount(2, $this->firstVCard->getProperties(Address::class));
179
        $this->assertCount(2, $this->firstVCard->getProperties(Email::class));
180
        $this->assertCount(1, $this->firstVCard->getProperties(Note::class));
181
        $this->assertCount(1, $this->firstVCard->getProperties(Birthdate::class));
182
        $this->assertCount(1, $this->firstVCard->getProperties(Anniversary::class));
183
        $this->assertCount(1, $this->firstVCard->getProperties(Telephone::class));
184
185
        $this->assertCount(2, $this->secondVCard->getProperties());
186
        $this->assertCount(1, $this->secondVCard->getProperties(Name::class));
187
        $this->assertCount(1, $this->secondVCard->getProperties(Address::class));
188
189
        $this->assertCount(4, $this->thirdVCard->getProperties());
190
        $this->assertCount(1, $this->thirdVCard->getProperties(Title::class));
191
        $this->assertCount(1, $this->thirdVCard->getProperties(Photo::class));
192
        $this->assertCount(1, $this->thirdVCard->getProperties(Logo::class));
193
        $this->assertCount(1, $this->thirdVCard->getProperties(Telephone::class));
194
    }
195
}
196