From 033418030d570f104d49f69b42602700e6e5ded4 Mon Sep 17 00:00:00 2001 From: Jonny Schmid Date: Tue, 3 Apr 2018 13:44:32 +0100 Subject: [PATCH] Prepare bundle for unit testing (#14) * Add phpunit * Apply fixes from StyleCI * Fix version matrix * Compatibility with Swiftmailer 6.0.1 * Upgrade phpunit * Make style ci happy --- .travis.yml | 12 +++++++ Factory/GmailMessageFactory.php | 3 ++ Model/Collection/GmailDomain.php | 4 +-- Model/Collection/GmailLabelCollection.php | 4 +-- Model/Collection/GmailMessageCollection.php | 4 +-- Model/GmailHistoryInterface.php | 6 ++-- Model/GmailIdsInterface.php | 6 ++-- Model/GmailLabelInterface.php | 6 ++-- Model/GmailMessageInterface.php | 37 +++++++++++---------- Model/GmailUserInterface.php | 8 ++--- Services/Directory.php | 5 +++ Services/Email.php | 6 ++-- Services/SyncMessages.php | 4 +-- Services/Thread.php | 3 +- Swift/GmailApiTransport.php | 10 +++++- Tests/bootstrap.php | 24 +++++++++++++ composer.json | 7 ++-- phpunit.xml.dist | 21 ++++++++++++ 18 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 .travis.yml create mode 100644 Tests/bootstrap.php create mode 100644 phpunit.xml.dist diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c625955 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +matrix: + include: + - php: 7.1 + env: SWIFT_VERSION=~6.0.0 + - php: 7.1 + env: SWIFT_VERSION=~5.4.0 + +before_script: + - composer require "swiftmailer/swiftmailer:${SWIFT_VERSION}" --no-update + - composer install --no-interaction --dev diff --git a/Factory/GmailMessageFactory.php b/Factory/GmailMessageFactory.php index bbbc203..02296f8 100644 --- a/Factory/GmailMessageFactory.php +++ b/Factory/GmailMessageFactory.php @@ -60,12 +60,15 @@ public function createFromGmailApiMessage(\Google_Service_Gmail_Message $gmailAp switch ($header->getName()) { case 'From': $message->setFrom($header->getValue() ?? ''); + break; case 'To': $message->setTo($header->getValue() ?? ''); + break; case 'Subject': $message->setSubject($header->getValue() ?? ''); + break; } $headerNames[] = $header->getName(); diff --git a/Model/Collection/GmailDomain.php b/Model/Collection/GmailDomain.php index c8e0663..b59e50b 100644 --- a/Model/Collection/GmailDomain.php +++ b/Model/Collection/GmailDomain.php @@ -39,7 +39,7 @@ public function __construct(string $domain) * * @return GmailDomain */ - public function addGmailUser(GmailUserInterface $user): GmailDomain + public function addGmailUser(GmailUserInterface $user): self { $this->gmailUsers->attach($user); @@ -51,7 +51,7 @@ public function addGmailUser(GmailUserInterface $user): GmailDomain * * @return GmailDomain */ - public function removeGmailUser(GmailUserInterface $user): GmailDomain + public function removeGmailUser(GmailUserInterface $user): self { $this->gmailUsers->detach($user); diff --git a/Model/Collection/GmailLabelCollection.php b/Model/Collection/GmailLabelCollection.php index 8c53c9f..4de06cb 100644 --- a/Model/Collection/GmailLabelCollection.php +++ b/Model/Collection/GmailLabelCollection.php @@ -30,7 +30,7 @@ public function __construct() * * @return GmailLabelCollection */ - public function addLabel(GmailLabelInterface $label): GmailLabelCollection + public function addLabel(GmailLabelInterface $label): self { $this->labels->attach($label); @@ -42,7 +42,7 @@ public function addLabel(GmailLabelInterface $label): GmailLabelCollection * * @return GmailLabelCollection */ - public function removeLabel(GmailLabelInterface $label): GmailLabelCollection + public function removeLabel(GmailLabelInterface $label): self { $this->labels->detach($label); diff --git a/Model/Collection/GmailMessageCollection.php b/Model/Collection/GmailMessageCollection.php index 7959de7..bab35e1 100644 --- a/Model/Collection/GmailMessageCollection.php +++ b/Model/Collection/GmailMessageCollection.php @@ -29,7 +29,7 @@ public function __construct() * * @return GmailMessageCollection */ - public function addMessage(GmailMessageInterface $message): GmailMessageCollection + public function addMessage(GmailMessageInterface $message): self { $this->messages->attach($message); @@ -41,7 +41,7 @@ public function addMessage(GmailMessageInterface $message): GmailMessageCollecti * * @return GmailMessageCollection */ - public function removeMessage(GmailMessageInterface $message): GmailMessageCollection + public function removeMessage(GmailMessageInterface $message): self { $this->messages->detach($message); diff --git a/Model/GmailHistoryInterface.php b/Model/GmailHistoryInterface.php index dc198d7..4224856 100644 --- a/Model/GmailHistoryInterface.php +++ b/Model/GmailHistoryInterface.php @@ -15,7 +15,7 @@ interface GmailHistoryInterface * * @return GmailHistoryInterface */ - public function setUserId(string $userId): GmailHistoryInterface; + public function setUserId(string $userId): self; /** * @return string @@ -27,7 +27,7 @@ public function getUserId(): string; * * @return GmailHistoryInterface */ - public function setDomain(string $domain): GmailHistoryInterface; + public function setDomain(string $domain): self; /** * @return string @@ -39,7 +39,7 @@ public function getDomain(): string; * * @return GmailHistoryInterface */ - public function setHistoryId(int $historyId): GmailHistoryInterface; + public function setHistoryId(int $historyId): self; /** * @return int diff --git a/Model/GmailIdsInterface.php b/Model/GmailIdsInterface.php index d6e8df3..fbc586e 100644 --- a/Model/GmailIdsInterface.php +++ b/Model/GmailIdsInterface.php @@ -15,7 +15,7 @@ interface GmailIdsInterface * * @return GmailIdsInterface */ - public function setUserId(string $userId): GmailIdsInterface; + public function setUserId(string $userId): self; /** * @return string @@ -27,7 +27,7 @@ public function getUserId(): string; * * @return GmailIdsInterface */ - public function setDomain(string $domain): GmailIdsInterface; + public function setDomain(string $domain): self; /** * @return string @@ -42,7 +42,7 @@ public function getDomain(): string; * * @return GmailIdsInterface */ - public function setGmailIds(array $gmailIds): GmailIdsInterface; + public function setGmailIds(array $gmailIds): self; /** * The limit parameter allows you to retrieve a slice of the gmailIds. diff --git a/Model/GmailLabelInterface.php b/Model/GmailLabelInterface.php index 0b5aa61..3f3e08f 100644 --- a/Model/GmailLabelInterface.php +++ b/Model/GmailLabelInterface.php @@ -15,7 +15,7 @@ interface GmailLabelInterface * * @return GmailLabelInterface */ - public function setUserId(string $userId): GmailLabelInterface; + public function setUserId(string $userId): self; /** * @return string @@ -27,7 +27,7 @@ public function getUserId(): string; * * @return GmailLabelInterface */ - public function setDomain(string $domain): GmailLabelInterface; + public function setDomain(string $domain): self; /** * @return string @@ -39,7 +39,7 @@ public function getDomain(): string; * * @return GmailLabelInterface */ - public function setName(string $name): GmailLabelInterface; + public function setName(string $name): self; /** * @return string diff --git a/Model/GmailMessageInterface.php b/Model/GmailMessageInterface.php index 989de01..c03a23a 100644 --- a/Model/GmailMessageInterface.php +++ b/Model/GmailMessageInterface.php @@ -16,8 +16,11 @@ interface GmailMessageInterface * @see https://developers.google.com/gmail/api/guides/labels */ const LABEL_UNREAD = 'UNREAD'; + const LABEL_INBOX = 'INBOX'; + const LABEL_SENT = 'SENT'; + const LABEL_TRASH = 'TRASH'; /** @@ -25,7 +28,7 @@ interface GmailMessageInterface * * @return GmailMessageInterface */ - public function setGmailId(string $gmailId): GmailMessageInterface; + public function setGmailId(string $gmailId): self; /** * @return string @@ -37,7 +40,7 @@ public function getGmailId(): string; * * @return GmailMessageInterface */ - public function setDomain(string $domain): GmailMessageInterface; + public function setDomain(string $domain): self; /** * @return string @@ -49,7 +52,7 @@ public function getDomain(): string; * * @return GmailMessageInterface */ - public function setThreadId(string $threadId): GmailMessageInterface; + public function setThreadId(string $threadId): self; /** * @return string @@ -61,7 +64,7 @@ public function getThreadId(): string; * * @return GmailMessageInterface */ - public function setHistoryId(string $historyId): GmailMessageInterface; + public function setHistoryId(string $historyId): self; /** * @return string @@ -73,7 +76,7 @@ public function getHistoryId(): string; * * @return GmailMessageInterface */ - public function setUserId(string $userId): GmailMessageInterface; + public function setUserId(string $userId): self; /** * @return string @@ -88,7 +91,7 @@ public function getUserId(): string; * * @return GmailMessageInterface */ - public function setTo(string $to): GmailMessageInterface; + public function setTo(string $to): self; /** * @return string @@ -111,7 +114,7 @@ public function getToCanonical(): string; * * @return GmailMessageInterface */ - public function setFrom(string $from): GmailMessageInterface; + public function setFrom(string $from): self; /** * @return string @@ -138,7 +141,7 @@ public function getReplyAllRecipients(): string; * * @return GmailMessageInterface */ - public function setSentAt(\DateTimeInterface $sentAt): GmailMessageInterface; + public function setSentAt(\DateTimeInterface $sentAt): self; /** * @return \DateTimeInterface @@ -150,7 +153,7 @@ public function getSentAt(): \DateTimeInterface; * * @return GmailMessageInterface */ - public function setSubject(string $subject): GmailMessageInterface; + public function setSubject(string $subject): self; /** * @return string @@ -162,7 +165,7 @@ public function getSubject(): string; * * @return GmailMessageInterface */ - public function setSnippet(string $snippet): GmailMessageInterface; + public function setSnippet(string $snippet): self; /** * @return string @@ -180,7 +183,7 @@ public function getSnippet(): string; * * @return GmailMessageInterface */ - public function setBodyPlainTextFromApiMessage(\Google_Service_Gmail_Message $gmailApiMessage): GmailMessageInterface; + public function setBodyPlainTextFromApiMessage(\Google_Service_Gmail_Message $gmailApiMessage): self; /** * @return string @@ -192,7 +195,7 @@ public function getBodyPlainText(): string; * * @return GmailMessageInterface */ - public function setBodyPlainText(string $bodyPlainText): GmailMessageInterface; + public function setBodyPlainText(string $bodyPlainText): self; /** * Set from a \Google_Service_Gmail_Message (from the API). @@ -205,14 +208,14 @@ public function setBodyPlainText(string $bodyPlainText): GmailMessageInterface; * * @return GmailMessageInterface */ - public function setBodyHtmlFromApiMessage(\Google_Service_Gmail_Message $gmailApiMessage): GmailMessageInterface; + public function setBodyHtmlFromApiMessage(\Google_Service_Gmail_Message $gmailApiMessage): self; /** * @param string $bodyHtml * * @return GmailMessageInterface */ - public function setBodyHtml(string $bodyHtml): GmailMessageInterface; + public function setBodyHtml(string $bodyHtml): self; /** * @return string @@ -224,7 +227,7 @@ public function getBodyHtml(): string; * * @return GmailMessageInterface */ - public function addLabel(GmailLabelInterface $label): GmailMessageInterface; + public function addLabel(GmailLabelInterface $label): self; /** * @return \Traversable @@ -236,12 +239,12 @@ public function getLabels(): \Traversable; * * @return GmailMessageInterface */ - public function removeLabel(GmailLabelInterface $label): GmailMessageInterface; + public function removeLabel(GmailLabelInterface $label): self; /** * @return GmailMessageInterface */ - public function clearLabels(): GmailMessageInterface; + public function clearLabels(): self; /** * @param string $name diff --git a/Model/GmailUserInterface.php b/Model/GmailUserInterface.php index e595328..971f07a 100644 --- a/Model/GmailUserInterface.php +++ b/Model/GmailUserInterface.php @@ -20,7 +20,7 @@ public function getUserId(): string; * * @return GmailUserInterface */ - public function setUserId(string $userId): GmailUserInterface; + public function setUserId(string $userId): self; /** * @return string @@ -32,7 +32,7 @@ public function getPrimaryEmailAddress(): string; * * @return GmailUserInterface */ - public function setPrimaryEmailAddress(string $primaryEmailAddress): GmailUserInterface; + public function setPrimaryEmailAddress(string $primaryEmailAddress): self; /** * @return string[] @@ -44,14 +44,14 @@ public function getEmailAliases(): array; * * @return GmailUserInterface */ - public function addEmailAlias(string $emailAlias): GmailUserInterface; + public function addEmailAlias(string $emailAlias): self; /** * @param string $emailAlias * * @return GmailUserInterface */ - public function removeEmailAlias(string $emailAlias): GmailUserInterface; + public function removeEmailAlias(string $emailAlias): self; /** * Should return email aliases, plus primary email address. diff --git a/Services/Directory.php b/Services/Directory.php index d7882bc..f2da4aa 100644 --- a/Services/Directory.php +++ b/Services/Directory.php @@ -17,7 +17,9 @@ class Directory { const MODE_RESOLVE_PRIMARY_ONLY = 0; + const MODE_RESOLVE_ALIASES_ONLY = 1; + const MODE_RESOLVE_PRIMARY_PLUS_ALIASES = 2; /** @@ -178,14 +180,17 @@ public function resolveUserIdFromEmail(string $email, int $mode) switch ($mode) { case self::MODE_RESOLVE_PRIMARY_ONLY: $gmailUser = $this->getGmailDomain()->findGmailUserByPrimaryEmail($email); + break; case self::MODE_RESOLVE_ALIASES_ONLY: $gmailUser = $this->getGmailDomain()->findGmailUserByEmailAlias($email); + break; case self::MODE_RESOLVE_PRIMARY_PLUS_ALIASES: $gmailUser = $this->getGmailDomain()->findGmailUserByEmail($email); + break; default: diff --git a/Services/Email.php b/Services/Email.php index e335cd5..301eeb6 100644 --- a/Services/Email.php +++ b/Services/Email.php @@ -67,7 +67,7 @@ public function get(string $userId, string $emailId, array $options = []): ?\Goo return $this->googleServices->getGoogleServiceGmailForUserId($userId)->users_messages->get($userId, $emailId, $options); } catch (\Google_Service_Exception $exception) { // message does not exist - if ($exception->getCode() === 404) { + if (404 === $exception->getCode()) { return null; } else { throw $exception; @@ -112,17 +112,19 @@ public function getBatch(string $userId, array $emailIds, array $options = []): foreach ($batchResponses as $response) { if ($response instanceof \Google_Service_Gmail_Message) { $foundApiMessages[] = $response; + continue; } if ( $response instanceof \Google_Service_Exception && - $response->getCode() === 404 + 404 === $response->getCode() ) { continue; } if ($response instanceof \Google_Service_Exception) { throw $response; } + throw new \RuntimeException(sprintf( 'Expected response to be of class %s or %s, but instead got %s', \Google_Service_Gmail_Message::class, diff --git a/Services/SyncMessages.php b/Services/SyncMessages.php index 3cdf6a6..ac0ff71 100644 --- a/Services/SyncMessages.php +++ b/Services/SyncMessages.php @@ -163,8 +163,8 @@ private function processApiMessage(string $userId, string $domain, \Google_Servi $headerNames = []; foreach ($headers as $header) { if ( - ($header->getName() === 'From' || $header->getName() === 'To') && - ($header->getValue() === null || $header->getValue() === '') + ('From' === $header->getName() || 'To' === $header->getName()) && + (null === $header->getValue() || '' === $header->getValue()) ) { return; } diff --git a/Services/Thread.php b/Services/Thread.php index c8dbf3d..be7149f 100644 --- a/Services/Thread.php +++ b/Services/Thread.php @@ -85,9 +85,10 @@ private function modifyThread(string $userId, string $threadId, \Google_Service_ return $this->googleServices->getGoogleServiceGmailForUserId($userId)->users_threads->modify($userId, $threadId, $postBody); } catch (\Google_Service_Exception $exception) { // thread does not exist - if ($exception->getCode() === 404) { + if (404 === $exception->getCode()) { return; } + throw $exception; } } diff --git a/Swift/GmailApiTransport.php b/Swift/GmailApiTransport.php index d0195c6..869d6d3 100644 --- a/Swift/GmailApiTransport.php +++ b/Swift/GmailApiTransport.php @@ -57,7 +57,14 @@ public function stop() /** * {@inheritdoc} */ - public function send(\Swift_Mime_Message $swiftMessage, &$failedRecipients = null) + public function ping() + { + } + + /** + * {@inheritdoc} + */ + public function send(\Swift_Mime_SimpleMessage $swiftMessage, &$failedRecipients = null) { $gmailMessage = new \Google_Service_Gmail_Message(); $gmailMessage->setRaw($this->base64UrlEncode($swiftMessage)); @@ -145,6 +152,7 @@ private function fromArrayToString(array $fromArray): string return $fromString; } + throw new \InvalidArgumentException('Set at least one \'from\' address when using '.self::class); } diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100644 index 0000000..c1bc09a --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,24 @@ +=2.1" + "swiftmailer/swiftmailer": "~5.4.0|~6.0.0", + "google/apiclient": "~2.2.0" + }, + "require-dev": { + "phpunit/phpunit": "~7.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..99331a1 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,21 @@ + + + + + + + ./Tests + + + + + + ./ + + ./Resources + ./Tests + ./vendor + + + +