Skip to content

Commit

Permalink
Merge pull request jeroendesloovere#145 from danger89/fix-ut
Browse files Browse the repository at this point in the history
Fix unit tester again after Version merge & bug fix version parser + bug fix kind parameter
  • Loading branch information
jeroendesloovere authored Mar 3, 2019
2 parents 0fe666f + a65a72a commit 6869766
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ $formatter->download();
* [x] [ADDRESS](./src/Property/Address.php) - The address of the object represented in structured parts

### Communications Properties:
* [x] [TEL](./src/Property/Telephone.php) - The telephone number as a tel URI
* [x] [EMAIL](./src/Property/Email.php) - The email address as a mailto URI
* [x] [TEL](./src/Property/Telephone.php) - The telephone number(s) as a tel URI
* [x] [EMAIL](./src/Property/Email.php) - The email address(es) as a mailto URI
* [ ] IMPP - The IMPP instant messaging contact information
* [ ] LANG - The language of the object

Expand All @@ -87,7 +87,7 @@ $formatter->download();
* [ ] UID - A unique identifier for the object
* [ ] CLIENTPIDMAP - Not required
* [ ] URL - Any URL related to the object
* [X] [VERSION](./src/Property/Parameter/Version.php) - Not required (namespace will capture this)
* [X] [VERSION](./src/Property/Parameter/Version.php) - Is mandatory for 4.0

### Security Properties:
* [ ] KEY - The security key of the object
Expand Down
33 changes: 30 additions & 3 deletions src/Parser/VcfParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use JeroenDesloovere\VCard\Parser\Property\NodeParserInterface;
use JeroenDesloovere\VCard\Property\NodeInterface;
use JeroenDesloovere\VCard\VCard;
use JeroenDesloovere\VCard\Property\Parameter\Version;
use JeroenDesloovere\VCard\Property\Parameter\Kind;

final class VcfParser implements ParserInterface
{
Expand Down Expand Up @@ -67,16 +69,41 @@ private function parseParameters(?string $parameters): array

private function parseVCard(string $content): VCard
{
$vCard = new VCard();
$vCard = $this->createVcardObjectWithProperties($content);

$lines = explode("\n", $content);
foreach ($lines as $line) {
$this->parseVCardLine($line, $vCard);
$this->parseVCardContentLine($line, $vCard);
}

return $vCard;
}

private function parseVCardLine(string $line, VCard &$vCard): void
private function createVcardObjectWithProperties(string $content): VCard
{
$vcardProperties = array(
Kind::getNode() => null,
Version::getNode() => null);

$lines = explode("\n", $content);
foreach ($lines as $line) {
/**
* @var string $node
* @var string $value
*/
@list($node, $value) = explode(':', $line, 2);
if (array_key_exists($node, $this->parsers)) {
// Only check on either Kind or Version node
if ($node == Kind::getNode() || $node == Version::getNode()) {
$vcardProperties[$node] = $this->parsers[$node]->parseVcfString($value);
}
}
}

return new VCard($vcardProperties[Kind::getNode()], $vcardProperties[Version::getNode()]);
}

private function parseVCardContentLine(string $line, VCard &$vCard): void
{
// Strip grouping information. We don't use the group names. We
// simply use a list for entries that have multiple values.
Expand Down
21 changes: 12 additions & 9 deletions src/Property/Parameter/Kind.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
*/
final class Kind implements PropertyParameterInterface, SimpleNodeInterface
{
// Group - To represent groups of vCard objects
protected const GROUP = 'Group';
// group - To represent groups of vCard objects
protected const GROUP = 'group';

// Individual - To represent people
protected const INDIVIDUAL = 'Individual';
// individual - To represent people
protected const INDIVIDUAL = 'individual';

// Location - To represent location objects
protected const LOCATION = 'Location';
// location - To represent location objects
protected const LOCATION = 'location';

// Organization - To represent organisations
protected const ORGANIZATION = 'Organization';
// org - To represent organisations
protected const ORGANIZATION = 'org';

public const POSSIBLE_VALUES = [
self::GROUP,
Expand All @@ -43,8 +43,11 @@ final class Kind implements PropertyParameterInterface, SimpleNodeInterface
*/
public function __construct(string $value)
{
$value = strtolower($value);
if (!in_array($value, self::POSSIBLE_VALUES, true)) {
throw PropertyParameterException::forWrongValue($value, self::POSSIBLE_VALUES);
// If value is absent or not understood 'individual' must be used,
// as stated in RFC 6350
$this->value = self::INDIVIDUAL;
}

$this->value = $value;
Expand Down
120 changes: 115 additions & 5 deletions tests/VCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use JeroenDesloovere\VCard\Property\Parameter\Kind;
use JeroenDesloovere\VCard\Property\Parameter\Revision;
use JeroenDesloovere\VCard\Property\Parameter\Type;
use JeroenDesloovere\VCard\Property\Parameter\Version;
use JeroenDesloovere\VCard\Property\Photo;
use JeroenDesloovere\VCard\Property\Telephone;
use JeroenDesloovere\VCard\Property\Title;
Expand Down Expand Up @@ -158,20 +159,125 @@ public function testParserGetFileContentsException(): void
}

/**
* Test the Telephone parser independent
* Test the Telephone parser independently.
*/
public function testTelephoneParser(): void
{
// Given
$vcard = (new Vcard())->add(new Telephone('+33-01-23-45-67'));
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nTEL;VALUE=uri;TYPE=home:tel:+33-01-23-45-67\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), "BEGIN:VCARD\r\nTEL;VALUE=uri;TYPE=home:tel:+33-01-23-45-67\r\nEND:VCARD");
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getProperties(Telephone::class), $parser->getVCards()[0]->getProperties(Telephone::class));
}

/**
* Test the Version parameter parser independently.
* With version 4 and version 3, should result in not equal (bad weather)
*/
public function testVersionParameterParserBadWeather(): void
{
// Given
// Version 4
$vcard = new Vcard(null, Version::version4());
// Version 3
$content = "BEGIN:VCARD\r\nVERSION:3.0\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertNotEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

/**
* Test the Version parameter parser independently.
* Both version 4 (good weather)
*/
public function testVersionParameterParserGoodWeather(): void
{
// Given
$vcard = new Vcard(null, Version::version4());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

/**
* Test the Version parameter parser independently.
* Without any version specified, should use the default Version value (4.0)
*/
public function testVersionParameterParserWithoutVersion(): void
{
// Given
$vcard = new Vcard();
$content = "BEGIN:VCARD\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

/**
* Test the Kind parameter parser independently.
* Test vcard with individual person (is default kind)
*/
public function testKindIndividual(): void
{
// Given
$vcard = new Vcard();
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:individual\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

/**
* Test the Kind parameter parser independently.
* Test vcard with department/organization
*/
public function testKindOrganization(): void
{
// Given
$vcard = new Vcard(Kind::organization());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:org\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

/**
* Test the Kind parameter parser independently.
* Test vcard with group
*/
public function testKindGroup(): void
{
// Given
$vcard = new Vcard(Kind::group());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:group\r\nEND:VCARD";

// When
$parser = new Parser(new VcfParser(), $content);

// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}

public function testParserMultipleVCardsFromVcfFile(): void
{
$parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcards.vcf'));
Expand All @@ -188,6 +294,7 @@ public function testParserOneVCardFromVcfFile(): void
}

/**
* Integration test:
* Validate the number of properties from the created vCards in the Setup.
*/
public function testVCardGetProperties(): void
Expand Down Expand Up @@ -222,7 +329,8 @@ public function testTelephonePropertyContent(): void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"KIND:Individual\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"TEL;TYPE=home;VALUE=uri:tel:+33-01-23-45-67\r\n" .
"TEL;TYPE=work;VALUE=uri:tel:+33-05-42-41-96\r\n" .
"END:VCARD\r\n";
Expand All @@ -246,7 +354,8 @@ public function testNamePropertyContent(): void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"KIND:Individual\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"N:Berg;Melroy;van den;Mr.;\r\n" .
"END:VCARD\r\n";

Expand All @@ -268,7 +377,8 @@ public function testAddressPropertyContentWithLineBreak() : void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"KIND:Individual\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"ADR;TYPE=home:42;Villa;Main Street 500;London;Barnet;EN4 0AG;United Kingd\r\n" .
// Line break because of 75 octets width limit, immediately followed by a single white space.
" om\r\n" .
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vcard.vcf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN:VCARD
KIND:Individual
KIND:individual
VERSION:4.0
REV:2017-12-15T10:24:32Z
GENDER:M;Dude
Expand Down
6 changes: 3 additions & 3 deletions tests/assets/vcards.vcf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN:VCARD
KIND:Individual
KIND:individual
VERSION:4.0
REV:2017-12-15T10:24:32Z
GENDER:M;Dude
Expand All @@ -16,14 +16,14 @@ TEL;VALUE=uri;TYPE=home:tel:+33-01-23-45-67
TEL;VALUE=uri;TYPE=work:tel:+33-05-42-41-96
END:VCARD
BEGIN:VCARD
KIND:Individual
KIND:individual
VERSION:4.0
REV:2017-12-15T10:24:32Z
N:John;;Doe;;
ADR;TYPE=work:;Penthouse;Korenmarkt 1;Gent;Oost-Vlaanderen;9000;België
END:VCARD
BEGIN:VCARD
KIND:Organization
KIND:org
VERSION:4.0
REV:2017-12-15T10:24:32Z
TITLE:Apple
Expand Down

0 comments on commit 6869766

Please sign in to comment.