Passed
Branch 2.0.0-dev (9fc37f)
by Jeroen
02:57
created

VCardTest::setUpThirdVCard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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