Passed
Branch 2.0.0-dev (b7b9ad)
by Jeroen
02:54
created

VCardTest::testParserGetFileContentsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JeroenDesloovere\Tests\VCard;
4
5
use JeroenDesloovere\VCard\Formatter\Formatter;
6
use JeroenDesloovere\VCard\Formatter\VcfFormatter;
7
use JeroenDesloovere\VCard\Parser\Parser;
8
use JeroenDesloovere\VCard\Parser\VcfParser;
9
use JeroenDesloovere\VCard\Property\Address;
10
use JeroenDesloovere\VCard\Property\Anniversary;
11
use JeroenDesloovere\VCard\Property\Birthdate;
12
use JeroenDesloovere\VCard\Property\Email;
13
use JeroenDesloovere\VCard\Property\Gender;
14
use JeroenDesloovere\VCard\Property\Name;
15
use JeroenDesloovere\VCard\Property\Nickname;
16
use JeroenDesloovere\VCard\Property\Note;
17
use JeroenDesloovere\VCard\Property\Parameter\Kind;
18
use JeroenDesloovere\VCard\Property\Parameter\Type;
19
use JeroenDesloovere\VCard\Property\Photo;
20
use JeroenDesloovere\VCard\Property\Title;
21
use JeroenDesloovere\VCard\VCard;
22
use org\bovigo\vfs\vfsStream;
23
use org\bovigo\vfs\vfsStreamDirectory;
24
use PHPUnit\Framework\TestCase;
25
26
/**
27
 * How to execute all tests: `vendor/bin/phpunit tests`
28
 */
29
final class VCardTest extends TestCase
30
{
31
    /** @var VCard */
32
    private $firstVCard;
33
34
    /** @var VCard */
35
    private $secondVCard;
36
37
    /** @var VCard */
38
    private $thirdVCard;
39
40
    /** @var vfsStreamDirectory - We save the generated vCard to a virtual storage */
41
    private $virtualStorage;
42
43
    public function setUp(): void
44
    {
45
        // Building one or multiple vCards
46
        $this->firstVCard = (new VCard())
47
            ->add(Gender::male('Dude'))
48
            ->add(new Nickname('Web developer'))
49
            ->add(new Name('Desloovere', 'Jeroen'))
50
            ->add(new Address(null, null, 'Markt 1', 'Brugge', 'West-Vlaanderen', '8000', 'België', Type::work()))
51
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::home()))
52
            ->add(new Email('[email protected]', Type::work()))
53
            ->add(new Email('[email protected]', Type::home()))
54
            ->add(new Note('VCard library is amazing.'))
55
            ->add(new Birthdate(new \DateTime('2015-12-05')))
56
            ->add(new Anniversary(new \DateTime('2017-12-05')));
57
58
        $this->secondVCard = (new VCard())
59
            ->add(new Name('Doe', 'John'))
60
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::work()));
61
62
        $this->thirdVCard = (new VCard(Kind::organization()))
63
            ->add(new Title('Apple'))
64
            ->add(new Photo(__DIR__ . '/assets/landscape.jpeg', false));
0 ignored issues
show
Unused Code introduced by
The call to JeroenDesloovere\VCard\P...ty\Photo::__construct() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            ->add(/** @scrutinizer ignore-call */ new Photo(__DIR__ . '/assets/landscape.jpeg', false));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
66
        $this->virtualStorage = vfsStream::setup();
67
    }
68
69
    public function testFormatterSavingMultipleVCardsToVcfFile(): void
70
    {
71
        // Saving "vcards.vcf"
72
        $formatter = new Formatter(new VcfFormatter(), 'vcards');
73
        $formatter->addVCard($this->firstVCard);
74
        $formatter->addVCard($this->secondVCard);
75
76
        $this->assertFalse($this->virtualStorage->hasChild('vcards.vcf'));
77
        $formatter->save($this->virtualStorage->url());
78
        $this->assertTrue($this->virtualStorage->hasChild('vcards.vcf'));
79
    }
80
81
    public function testFormatterSavingOneVCardToVcfFile(): void
82
    {
83
        // Saving "vcard.vcf"
84
        $formatter = new Formatter(new VcfFormatter(), 'vcard');
85
        $formatter->addVCard($this->firstVCard);
86
87
        $this->assertFalse($this->virtualStorage->hasChild('vcard.vcf'));
88
        $formatter->save($this->virtualStorage->url());
89
        $this->assertTrue($this->virtualStorage->hasChild('vcard.vcf'));
90
    }
91
92
    /**
93
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
94
     * @expectedExceptionMessage File "Lorem ipsum dolor sit amet, consectetur adipiscing elit." is not readable, or doesn't exist.
95
     */
96
    public function testParserCorruptVCard(): void
97
    {
98
        new Parser(new VcfParser(), 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
99
    }
100
101
    /**
102
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
103
     * @expectedExceptionMessage File "" is not readable, or doesn't exist.
104
     */
105
    public function testParserEmptyVCard(): void
106
    {
107
        new Parser(new VcfParser(), '');
108
    }
109
110
    /**
111
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
112
     */
113
    public function testParserGetFileContentsException(): void
114
    {
115
        Parser::getFileContents(__DIR__ . '/not-existing');
116
    }
117
118
    public function testParserMultipleVCardsFromVcfFile(): void
119
    {
120
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcards.vcf'));
121
122
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
123
        $this->assertEquals($this->secondVCard->getProperties(), $parser->getVCards()[1]->getProperties());
124
    }
125
126
    public function testParserOneVCardFromVcfFile(): void
127
    {
128
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcard.vcf'));
129
130
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
131
    }
132
133
    public function testVCardGetProperties(): void
134
    {
135
        $this->assertCount(10, $this->firstVCard->getProperties());
136
        $this->assertCount(1, $this->firstVCard->getProperties(Gender::class));
137
        $this->assertCount(1, $this->firstVCard->getProperties(Nickname::class));
138
        $this->assertCount(1, $this->firstVCard->getProperties(Name::class));
139
        $this->assertCount(2, $this->firstVCard->getProperties(Address::class));
140
        $this->assertCount(2, $this->firstVCard->getProperties(Email::class));
141
        $this->assertCount(1, $this->firstVCard->getProperties(Note::class));
142
        $this->assertCount(1, $this->firstVCard->getProperties(Birthdate::class));
143
        $this->assertCount(1, $this->firstVCard->getProperties(Anniversary::class));
144
145
        $this->assertCount(2, $this->secondVCard->getProperties());
146
        $this->assertCount(1, $this->secondVCard->getProperties(Name::class));
147
        $this->assertCount(1, $this->secondVCard->getProperties(Address::class));
148
149
        $this->assertCount(2, $this->thirdVCard->getProperties());
150
        $this->assertCount(1, $this->thirdVCard->getProperties(Title::class));
151
        $this->assertCount(1, $this->thirdVCard->getProperties(Photo::class));
152
    }
153
}
154