From 8ae7e7327d68ab2f87c3ccda122fbf782c20c5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Fri, 1 May 2015 00:23:32 +0200 Subject: [PATCH 1/3] - fixed wrong return type in test method setUp() - added abstraction of user agent property to be prevent errors on console - added test for new email behaviour - added phpunit requirement in the dev section of the composer file --- composer.json | 3 +++ src/VCard.php | 31 ++++++++++++++++++++++++++----- tests/VCardTest.php | 45 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 6 deletions(-) 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 614d248..b99062b 100644 --- a/src/VCard.php +++ b/src/VCard.php @@ -106,11 +106,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 void */ - public function addEmail($address) + public function addEmail($address, $type = '') { - $this->setProperty('EMAIL;INTERNET', $address); + $this->setProperty( + 'EMAIL;INTERNET' . (($type != '') ? ';' . $type : ''), + $address + ); } /** @@ -332,6 +338,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 * @@ -441,7 +462,7 @@ public function getHeaders($asAssociative) 'Connection' => $connection ); } - + return array( 'Content-type: ' . $contentType, 'Content-Disposition: ' . $contentDisposition, @@ -471,7 +492,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')); } @@ -558,7 +579,7 @@ private function setProperty($key, $value) */ protected function shouldAttachmentBeCal() { - $browser = strtolower($_SERVER['HTTP_USER_AGENT']); + $browser = $this->getUserAgent(); $matches = []; preg_match('/os (\d+)_(\d+)\s+/', $browser, $matches); diff --git a/tests/VCardTest.php b/tests/VCardTest.php index b944c2b..a382739 100644 --- a/tests/VCardTest.php +++ b/tests/VCardTest.php @@ -21,10 +21,15 @@ */ class VCardTest extends \PHPUnit_Framework_TestCase { + /** + * @var VCard + */ + protected $vcard = null; + /** * Set up before class * - * @return SocialMedia + * @return void */ public function setUp() { @@ -105,4 +110,42 @@ public function testFullBlownName() $this->assertEquals('mister-jeroen-desloovere-junior', $this->vcard->getFilename()); } + + /** + * @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($key . ':' . $email, $this->vcard->getOutput()); + } else { + $this->assertContains($email, $this->vcard->getOutput()); + } + + } + } + + /** + * 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')), + ); + } } From 0d06770b73e84c7e3519b84395770f8fd68c0234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Grundko=CC=88tter?= Date: Fri, 1 May 2015 00:31:58 +0200 Subject: [PATCH 2/3] - improved tests --- tests/VCardTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/VCardTest.php b/tests/VCardTest.php index a382739..b65cebe 100644 --- a/tests/VCardTest.php +++ b/tests/VCardTest.php @@ -127,9 +127,9 @@ public function testEmail($emails = array()) foreach ($emails as $key => $email) { if (is_string($key)) { - $this->assertContains($key . ':' . $email, $this->vcard->getOutput()); + $this->assertContains('EMAIL;INTERNET;' . $key . ':' . $email, $this->vcard->getOutput()); } else { - $this->assertContains($email, $this->vcard->getOutput()); + $this->assertContains('EMAIL;INTERNET:' . $email, $this->vcard->getOutput()); } } From d4f5939498c8395409b8a3e04a1285345a3ddc63 Mon Sep 17 00:00:00 2001 From: Jeroen Desloovere Date: Wed, 20 May 2015 16:38:40 +0200 Subject: [PATCH 3/3] Push change --- tests/VCardTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/VCardTest.php b/tests/VCardTest.php index b65cebe..c75b42d 100644 --- a/tests/VCardTest.php +++ b/tests/VCardTest.php @@ -136,7 +136,7 @@ public function testEmail($emails = array()) } /** - * data provider for testEmail() + * Data provider for testEmail() * * @return array */