Passed
Branch new-version (77ae24)
by Jeroen
02:34
created

VCardTest::testParserMultipleVCardsFromVcfFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 1
f 0
1
<?php
2
3
namespace JeroenDesloovere\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\Name;
11
use JeroenDesloovere\VCard\Property\Note;
12
use JeroenDesloovere\VCard\Property\Parameter\Kind;
13
use JeroenDesloovere\VCard\Property\Parameter\Type;
14
use org\bovigo\vfs\vfsStream;
15
use org\bovigo\vfs\vfsStreamDirectory;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * How to execute all tests: `vendor/bin/phpunit tests`
20
 */
21
final class VCardTest extends TestCase
22
{
23
    /**
24
     * @var vfsStreamDirectory
25
     */
26
    private $vfsRoot;
27
28
    /**
29
     * @var VCard 
30
     */
31
    private $firstVCard;
32
33
    /**
34
     * @var VCard 
35
     */
36
    private $secondVCard;
37
38
    public function setUp(): void
39
    {
40
        $this->vfsRoot = vfsStream::setup();
41
42
        // Building one or multiple vCards
43
        $this->firstVCard = (new VCard())
44
            ->add(new Name('Desloovere', 'Jeroen'))
45
            ->add(new Address(null, null, 'Markt 1', 'Brugge', 'West-Vlaanderen', '8000', 'België', Type::work()))
46
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::home()))
47
            ->add(new Note('VCard library is amazing.'));
48
49
        $this->secondVCard = (new VCard())
50
            ->add(new Name('Doe', 'John'))
51
            ->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::work()));
52
    }
53
54 View Code Duplication
    public function testFormatterSavingMultipleVCardsToVcfFile(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        // Saving "vcards.vcf"
57
        $formatter = new Formatter(new VcfFormatter(), 'vcards');
58
        $formatter->addVCard($this->firstVCard);
59
        $formatter->addVCard($this->secondVCard);
60
61
        $this->assertFalse($this->vfsRoot->hasChild('vcards.vcf'));
62
        $formatter->save($this->vfsRoot->url());
63
        $this->assertTrue($this->vfsRoot->hasChild('vcards.vcf'));
64
    }
65
66 View Code Duplication
    public function testFormatterSavingOneVCardToVcfFile(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        // Saving "vcard.vcf"
69
        $formatter = new Formatter(new VcfFormatter(), 'vcard');
70
        $formatter->addVCard($this->firstVCard);
71
72
        $this->assertFalse($this->vfsRoot->hasChild('vcard.vcf'));
73
        $formatter->save($this->vfsRoot->url());
74
        $this->assertTrue($this->vfsRoot->hasChild('vcard.vcf'));
75
    }
76
77
    /**
78
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
79
     */
80
    public function testParserCorruptVCard(): void
81
    {
82
        new Parser(new VcfParser(), 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
83
    }
84
85
    /**
86
     * @expectedException \JeroenDesloovere\VCard\Exception\ParserException
87
     */
88
    public function testParserEmptyVCard(): void
89
    {
90
        new Parser(new VcfParser(), '');
91
    }
92
93
    public function testParserMultipleVCardsFromVcfFile(): void
94
    {
95
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcards.vcf'));
96
97
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
98
        $this->assertEquals($this->secondVCard->getProperties(), $parser->getVCards()[1]->getProperties());
99
    }
100
101
    public function testParserOneVCardFromVcfFile(): void
102
    {
103
        $parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcard.vcf'));
104
105
        $this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
106
    }
107
108
    public function testVCardGetProperties(): void
109
    {
110
        $this->assertCount(4, $this->firstVCard->getProperties());
111
        $this->assertCount(1, $this->firstVCard->getProperties(Name::class));
112
        $this->assertCount(2, $this->firstVCard->getProperties(Address::class));
113
        $this->assertCount(1, $this->firstVCard->getProperties(Note::class));
114
        $this->assertCount(2, $this->secondVCard->getProperties());
115
        $this->assertCount(1, $this->secondVCard->getProperties(Name::class));
116
        $this->assertCount(1, $this->secondVCard->getProperties(Address::class));
117
    }
118
}
119