diff --git a/composer.json b/composer.json index 8596896..65f3e56 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "php": ">=5.3.3", "behat/transliterator": "~1.0" }, + "require-dev": { + "phpunit/phpunit": "4.6.*" + }, "autoload": { "psr-4": { "JeroenDesloovere\\VCard\\": "src/" } } diff --git a/src/VCard.php b/src/VCard.php index 5f746d9..397d567 100644 --- a/src/VCard.php +++ b/src/VCard.php @@ -112,11 +112,17 @@ public function addCompany($company) * Add email * * @param string $address The e-mail address + * @param string [optional] $type The type of the email address + * $type may be PREF | WORK | HOME + * or any combination of these: e.g. "PREF;WORK" * @return $this */ - public function addEmail($address) + public function addEmail($address, $type = '') { - $this->setProperty('EMAIL;INTERNET', $address); + $this->setProperty( + 'EMAIL;INTERNET' . (($type != '') ? ';' . $type : ''), + $address + ); return $this; } @@ -352,6 +358,21 @@ public function buildVCalendar() return $string; } + /** + * Returns the browser user agent string. + * + * @return string + */ + protected function getUserAgent() + { + if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) { + $browser = strtolower($_SERVER['HTTP_USER_AGENT']); + } else { + $browser = 'unknown'; + } + return $browser; + } + /** * Decode * @@ -461,7 +482,7 @@ public function getHeaders($asAssociative) 'Connection' => $connection ); } - + return array( 'Content-type: ' . $contentType, 'Content-Disposition: ' . $contentDisposition, @@ -491,7 +512,7 @@ public function getOutput() public function isIOS() { // get user agent - $browser = strtolower($_SERVER['HTTP_USER_AGENT']); + $browser = $this->getUserAgent(); return (strpos($browser, 'iphone') || strpos($browser, 'ipod') || strpos($browser, 'ipad')); } @@ -578,7 +599,7 @@ private function setProperty($key, $value) */ protected function shouldAttachmentBeCal() { - $browser = strtolower($_SERVER['HTTP_USER_AGENT']); + $browser = $this->getUserAgent(); $matches = array(); preg_match('/os (\d+)_(\d+)\s+/', $browser, $matches); diff --git a/tests/VCardTest.php b/tests/VCardTest.php index 09fcea7..ca6a9c5 100644 --- a/tests/VCardTest.php +++ b/tests/VCardTest.php @@ -21,10 +21,29 @@ */ class VCardTest extends \PHPUnit_Framework_TestCase { + /** + * @var VCard + */ + protected $vcard = null; + + /** + * Data provider for testEmail() + * + * @return array + */ + public function emailDataProvider() { + return array( + array(array('john@doe.com')), + array(array('john@doe.com', 'WORK' => 'john@work.com')), + array(array('WORK' => 'john@work.com', 'HOME' => 'john@home.com')), + array(array('PREF;WORK' => 'john@work.com', 'HOME' => 'john@home.com')), + ); + } + /** * Set up before class * - * @return SocialMedia + * @return void */ public function setUp() { @@ -106,7 +125,32 @@ public function testFullBlownName() $this->assertEquals('mister-jeroen-desloovere-junior', $this->vcard->getFilename()); } - public function testAddAdress() +<<<<<<< HEAD + /** + * @test + * @dataProvider emailDataProvider + */ + public function testEmail($emails = array()) + { + foreach ($emails as $key => $email) { + if (is_string($key)) { + $this->vcard->addEmail($email, $key); + } else { + $this->vcard->addEmail($email); + } + } + + foreach ($emails as $key => $email) { + if (is_string($key)) { + $this->assertContains('EMAIL;INTERNET;' . $key . ':' . $email, $this->vcard->getOutput()); + } else { + $this->assertContains('EMAIL;INTERNET:' . $email, $this->vcard->getOutput()); + } + + } + } + + public function testAddAddress() { $this->assertEquals($this->vcard, $this->vcard->addAddress()); } @@ -126,7 +170,7 @@ public function testAddEmail() $this->assertEquals($this->vcard, $this->vcard->addEmail('')); } - public function testAddjobtitle() + public function testAddJobTitle() { $this->assertEquals($this->vcard, $this->vcard->addJobtitle('')); } @@ -146,15 +190,15 @@ public function testAddPhoneNumber() $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber('')); } - public function testAddUrl() - { - $this->assertEquals($this->vcard, $this->vcard->addUrl('')); - } - public function testAddPhotoWithJpgPhoto() { $return = $this->vcard->addPhoto(__DIR__.'/image.jpg', true); $this->assertEquals($this->vcard, $return); } + + public function testAddUrl() + { + $this->assertEquals($this->vcard, $this->vcard->addUrl('')); + } }