From 4f081aaa71175612ad17a6fe978e1eac7675af71 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 20 Oct 2018 17:57:48 +0200 Subject: [PATCH] implement tests --- Message/Console/Header.php | 86 --------------- Message/Console/Request.php | 37 +++---- Message/Mail/ContentEncoding.php | 34 ++++++ Message/Mail/EmailAbstract.php | 118 +++++++++++--------- Message/Mail/Imap.php | 9 +- Message/Mail/Mail.php | 4 +- Message/Mail/Nntp.php | 57 ---------- Utils/IO/Zip/Tar.php | 5 + Utils/JobQueue/Job.php | 51 --------- Utils/JobQueue/JobQueue.php | 146 ------------------------- tests/Bootstrap.php | 16 +++ tests/Message/Console/HeaderTest.php | 78 +++++++++++++ tests/Message/Console/RequestTest.php | 108 ++++++++++++++++++ tests/Message/Console/ResponseTest.php | 39 +++++++ tests/Message/Mail/ImapTest.php | 37 ++++++- tests/Utils/IO/Zip/GzTest.php | 24 +++- tests/Utils/IO/Zip/TarGzTest.php | 75 ++++++++++++- tests/Utils/IO/Zip/TarTest.php | 75 ++++++++++++- tests/Utils/JobQueue/JobQueueTest.php | 24 ---- tests/Utils/JobQueue/JobTest.php | 24 ---- 20 files changed, 573 insertions(+), 474 deletions(-) create mode 100644 Message/Mail/ContentEncoding.php delete mode 100644 Message/Mail/Nntp.php delete mode 100644 Utils/JobQueue/Job.php delete mode 100644 Utils/JobQueue/JobQueue.php create mode 100644 tests/Message/Console/HeaderTest.php create mode 100644 tests/Message/Console/RequestTest.php create mode 100644 tests/Message/Console/ResponseTest.php delete mode 100644 tests/Utils/JobQueue/JobQueueTest.php delete mode 100644 tests/Utils/JobQueue/JobTest.php diff --git a/Message/Console/Header.php b/Message/Console/Header.php index 1b8882c71..569f9d7a7 100644 --- a/Message/Console/Header.php +++ b/Message/Console/Header.php @@ -73,10 +73,6 @@ final class Header extends HeaderAbstract return false; } - if (self::isSecurityHeader($key) && isset($this->header[$key])) { - return false; - } - $key = \strtolower($key); if (!$overwrite && isset($this->header[$key])) { @@ -94,25 +90,6 @@ final class Header extends HeaderAbstract return true; } - /** - * Is security header. - * - * @param string $key Header key - * - * @return bool - * - * @since 1.0.0 - */ - public static function isSecurityHeader(string $key) : bool - { - $key = \strtolower($key); - - return $key === 'content-security-policy' - || $key === 'x-xss-protection' - || $key === 'x-content-type-options' - || $key === 'x-frame-options'; - } - /** * {@inheritdoc} */ @@ -121,48 +98,6 @@ final class Header extends HeaderAbstract return self::VERSION; } - /** - * Get status code. - * - * @return int - * - * @since 1.0.0 - */ - public function getStatusCode() : int - { - if ($this->status === 0) { - $this->status = (int) \http_response_code(); - } - - return parent::getStatusCode(); - } - - /** - * Get all headers for apache and nginx - * - * @return array - * - * @since 1.0.0 - */ - public static function getAllHeaders() : array - { - if (\function_exists('getallheaders')) { - // @codeCoverageIgnoreStart - return getallheaders(); - // @codeCoverageIgnoreEnd - } - - $headers = []; - foreach ($_SERVER as $name => $value) { - $part = \substr($name, 5); - if ($part === 'HTTP_') { - $headers[\str_replace(' ', '-', \ucwords(\strtolower(\str_replace('_', ' ', $part))))] = $value; - } - } - - return $headers; - } - /** * Remove header by ID. * @@ -225,27 +160,6 @@ final class Header extends HeaderAbstract return isset($this->header[$key]); } - /** - * Push all headers. - * - * @return void - * - * @since 1.0.0 - * @codeCoverageIgnore - */ - public function push() : void - { - if (self::$isLocked) { - throw new \Exception('Already locked'); - } - - foreach ($this->header as $name => $arr) { - foreach ($arr as $ele => $value) { - \header($name . ': ' . $value); - } - } - } - /** * {@inheritdoc} */ diff --git a/Message/Console/Request.php b/Message/Console/Request.php index 3ac251ae6..95252dded 100644 --- a/Message/Console/Request.php +++ b/Message/Console/Request.php @@ -19,6 +19,7 @@ use phpOMS\Message\RequestAbstract; use phpOMS\Message\RequestSource; use phpOMS\Message\Http\RequestMethod; use phpOMS\Router\RouteVerb; +use phpOMS\Uri\Argument; use phpOMS\Uri\UriInterface; /** @@ -49,12 +50,12 @@ final class Request extends RequestAbstract * * @since 1.0.0 */ - public function __construct(UriInterface $uri, Localization $l11n = null) + public function __construct(UriInterface $uri = null, Localization $l11n = null) { $this->header = new Header(); $this->header->setL11n($l11n ?? new Localization()); - $this->uri = $uri; + $this->uri = $uri ?? new Argument(); $this->init(); } @@ -71,20 +72,6 @@ final class Request extends RequestAbstract { $lang = \explode('_', $_SERVER['LANG'] ?? ''); $this->header->getL11n()->setLanguage($lang[0] ?? 'en'); - - $this->cleanupGlobals(); - } - - /** - * Clean up globals that musn't be used any longer - * - * @return void - * - * @since 1.0.0 - */ - private function cleanupGlobals() : void - { - unset($_SERVER); } /** @@ -123,18 +110,31 @@ final class Request extends RequestAbstract public function getOS() : string { if ($this->os === null) { - $this->os = PHP_OS; + $this->os = \strtolower(PHP_OS); } return $this->os; } + /** + * Set OS type + * + * @param string $os OS type + * + * @return void + * + * @since 1.0.0 + */ + public function setOS(string $os) : void + { + $this->os = $os; + } + /** * {@inheritdoc} */ public function getOrigin() : string { - // todo: maybe return execution path? return '127.0.0.1'; } @@ -159,7 +159,6 @@ final class Request extends RequestAbstract */ public function getBody() : string { - // todo: implement return ''; } diff --git a/Message/Mail/ContentEncoding.php b/Message/Mail/ContentEncoding.php new file mode 100644 index 000000000..5eccdc42e --- /dev/null +++ b/Message/Mail/ContentEncoding.php @@ -0,0 +1,34 @@ +timeout = $timeout; $this->ssl = $ssl; - imap_timeout(IMAP_OPENTIMEOUT, $timeout); - imap_timeout(IMAP_READTIMEOUT, $timeout); - imap_timeout(IMAP_WRITETIMEOUT, $timeout); - imap_timeout(IMAP_CLOSETIMEOUT, $timeout); + \imap_timeout(IMAP_OPENTIMEOUT, $timeout); + \imap_timeout(IMAP_READTIMEOUT, $timeout); + \imap_timeout(IMAP_WRITETIMEOUT, $timeout); + \imap_timeout(IMAP_CLOSETIMEOUT, $timeout); } /** @@ -99,13 +107,13 @@ class EmailAbstract */ public static function decode(string $content, int $encoding) { - if ($encoding == 3) { - return imap_base64($content); - } elseif ($encoding == 1) { - return imap_8bit($content); - } else { - return imap_qprint($content); + if ($encoding === ContentEncoding::BASE64) { + return \imap_base64($content); + } elseif ($encoding === ContentEncoding::EIGHTBIT) { + return \imap_8bit($content); } + + return \imap_qprint($content); } /** @@ -127,8 +135,8 @@ class EmailAbstract */ public function disconnect() : void { - if ($this->con === null) { - imap_close($this->con); + if ($this->con !== null) { + \imap_close($this->con); $this->con = null; } } @@ -139,17 +147,22 @@ class EmailAbstract * @param string $user Username * @param string $pass Password * - * @return void + * @return bool * * @since 1.0.0 */ - public function connect(string $user = '', string $pass = '') : void + public function connect(string $user = '', string $pass = '') : bool { $this->mailbox = \substr($this->mailbox, 0, -1) . ($this->ssl ? '/ssl/validate-cert' : '/novalidate-cert') . '}'; - // /novalidate-cert - if ($this->con === null) { - $this->con = imap_open($this->mailbox . 'INBOX', $user, $pass); + try { + $this->con = \imap_open($this->mailbox . 'INBOX', $user, $pass); + + return true; + } catch (\Throwable $t) { + $this->con = null; + + return false; } } @@ -162,7 +175,7 @@ class EmailAbstract */ public function isConnected() : bool { - return imap_ping($this->con); + return $this->con === null ? false : \imap_ping($this->con); } /** @@ -176,36 +189,49 @@ class EmailAbstract */ public function getBoxes(string $pattern = '*') : array { - return imap_list($this->con, $this->host, $pattern); + $list = \imap_list($this->con, $this->host, $pattern); + + return $list === false ? [] : $list; } /** * Get inbox quota. * - * @return mixed + * @return array * * @since 1.0.0 */ - public function getQuota() + public function getQuota() : array { - return imap_get_quotaroot($this->con, "INBOX"); + $quota = []; + + try { + $quota = \imap_get_quotaroot($this->con, "INBOX"); + } finally { + return $quota === false ? [] : $quota; + } } /** * Get email. * - * @param mixed $id mail id + * @param string $id mail id * * @return Mail * * @since 1.0.0 */ - public function getEmail($id) : Mail + public function getEmail(string $id) : Mail { $mail = new Mail($id); - $mail->setOverview(imap_fetch_overview($this->con, $id)); - $mail->setBody(imap_fetchbody($this->con, $id, 2)); - $mail->setEncoding(imap_fetchstructure($this->con, $id)); + + if ((int) $id > $this->countMessages()) { + return $mail; + } + + $mail->setOverview(\imap_fetch_overview($this->con, $id)); + $mail->setBody(\imap_fetchbody($this->con, (int) $id, '1.1')); + //$mail->setEncoding(\imap_fetchstructure($this->con, (int) $id)); return $mail; } @@ -233,9 +259,9 @@ class EmailAbstract */ public function getInboxOverview(string $option = 'ALL') : array { - $ids = imap_search($this->con, $option, SE_FREE, 'UTF-8'); + $ids = \imap_search($this->con, $option, SE_FREE, 'UTF-8'); - return is_array($ids) ? imap_fetch_overview($this->con, \implode(',', $ids)) : []; + return \is_array($ids) ? \imap_fetch_overview($this->con, \implode(',', $ids)) : []; } /** @@ -396,18 +422,6 @@ class EmailAbstract return $this->getInboxOverview('TEXT "' . $text . '"'); } - /** - * List mailboxes - * - * @return array - * - * @since 1.0.0 - */ - public function listMailbox() : array - { - return imap_listmailbox($this->con, $this->mailbox, '*'); - } - /** * Create mailbox * @@ -419,7 +433,7 @@ class EmailAbstract */ public function createMailbox(string $mailbox) : bool { - return imap_createmailbox($this->con, $mailbox); + return \imap_createmailbox($this->con, $mailbox); } /** @@ -434,7 +448,7 @@ class EmailAbstract */ public function renameMailbox(string $old, string $new) : bool { - return imap_renamemailbox($this->con, $old, $new); + return \imap_renamemailbox($this->con, $old, $new); } /** @@ -448,7 +462,7 @@ class EmailAbstract */ public function deleteMailbox(string $mailbox) : bool { - return imap_deletemailbox($this->con, $mailbox); + return \imap_deletemailbox($this->con, $mailbox); } /** @@ -462,7 +476,7 @@ class EmailAbstract */ public function deleteMessage(int $id) : bool { - return imap_delete($this->con, $id); + return \imap_delete($this->con, $id); } /** @@ -474,7 +488,7 @@ class EmailAbstract */ public function deleteMarkedMessages() : bool { - return imap_expunge($this->con); + return \imap_expunge($this->con); } /** @@ -490,11 +504,11 @@ class EmailAbstract public function getMessageOverview(int $length = 0, int $start = 1) : array { if ($length === 0) { - $info = imap_check($this->con); + $info = \imap_check($this->con); $length = $info->Nmsgs; } - return imap_fetch_overview($mbox, $start . ':' . ($length + $start), 0); + return \imap_fetch_overview($this->con, $start . ':' . ($length - 1 + $start), 0); } /** @@ -506,7 +520,7 @@ class EmailAbstract */ public function countMessages() : int { - return imap_num_msg($this->con); + return \imap_num_msg($this->con); } /** @@ -520,6 +534,10 @@ class EmailAbstract */ public function getMessageHeader(int $id) : string { - return imap_fetchheader($this->con, $id); + if ($id > $this->countMessages()) { + return ''; + } + + return \imap_fetchheader($this->con, $id); } } diff --git a/Message/Mail/Imap.php b/Message/Mail/Imap.php index 109b43668..d26ec8576 100644 --- a/Message/Mail/Imap.php +++ b/Message/Mail/Imap.php @@ -34,7 +34,7 @@ class Imap extends EmailAbstract * * @since 1.0.0 */ - public function __construct(string $host = 'localhost', int $port = 25, int $timeout = 30, bool $ssl = false) + public function __construct(string $host = 'localhost', int $port = 143, int $timeout = 30, bool $ssl = false) { parent::__construct($host, $port, $timeout, $ssl); } @@ -45,13 +45,14 @@ class Imap extends EmailAbstract * @param string $user Username * @param string $pass Password * - * @return void + * @return bool * * @since 1.0.0 */ - public function connect(string $user = '', string $pass = '') : void + public function connect(string $user = '', string $pass = '') : bool { $this->mailbox = '{' . $this->host . ':' . $this->port . '/imap}'; - parent::connect(); + + return parent::connect($user, $pass); } } diff --git a/Message/Mail/Mail.php b/Message/Mail/Mail.php index 4582d7e95..6950de1bc 100644 --- a/Message/Mail/Mail.php +++ b/Message/Mail/Mail.php @@ -197,13 +197,13 @@ class Mail /** * Set body. * - * @param string $overview Mail overview + * @param array $overview Mail overview * * @return void * * @since 1.0.0 */ - public function setOverview(string $overview) : void + public function setOverview(array $overview) : void { $this->overview = $overview; } diff --git a/Message/Mail/Nntp.php b/Message/Mail/Nntp.php deleted file mode 100644 index f724fa51b..000000000 --- a/Message/Mail/Nntp.php +++ /dev/null @@ -1,57 +0,0 @@ -mailbox = '{' . $this->host . ':' . $this->port . '/nntp}'; - parent::connect(); - } -} diff --git a/Utils/IO/Zip/Tar.php b/Utils/IO/Zip/Tar.php index 8b9eb8820..6210f4799 100644 --- a/Utils/IO/Zip/Tar.php +++ b/Utils/IO/Zip/Tar.php @@ -43,6 +43,11 @@ class Tar implements ArchiveInterface /** @var array $sources */ foreach ($sources as $source => $relative) { + if (\is_numeric($source) && \realpath($relative) !== false) { + $source = $relative; + $relative = ''; + } + $source = \realpath($source); if ($source === false) { diff --git a/Utils/JobQueue/Job.php b/Utils/JobQueue/Job.php deleted file mode 100644 index 2e3a30552..000000000 --- a/Utils/JobQueue/Job.php +++ /dev/null @@ -1,51 +0,0 @@ -priority = $priority; - $this->callback = $callback; - } - - public function execute() - { - $this->callback(); - } - - public function getPriority() : float - { - return $this->priority; - } - - public function setPriority(float $priority) : void - { - $this->priority = $priority; - } -} diff --git a/Utils/JobQueue/JobQueue.php b/Utils/JobQueue/JobQueue.php deleted file mode 100644 index 7f274d418..000000000 --- a/Utils/JobQueue/JobQueue.php +++ /dev/null @@ -1,146 +0,0 @@ -queue = new PriorityQueue(); - } - - public function dispatch(Job $job) - { - $this->queue->insert($job, $job->getPriority()); - } - - public function run() - { - $this->run = true; - $this->suspended = false; - - if ($this->isDeamonized) { - if ($pid = pcntl_fork()) { - return $pid; - } - - $this->runAsDeamon(); - - if (posix_setsid() < 0 || $pid = pcntl_fork()) { - return 0; - } - } - - while ($this->run) { - while (!$this->suspended) { - if ($this->deamonized) { - // todo: see if still unsuspended and still running (db, file etc) - } - } - - $job = $this->queue->pop(); - - $this->queue->increaseAll(); - $job['job']->execute(); - - if ($this->isTerminating && $this->queue->count() < 1) { - $this->suspended = true; - $this->run = false; - } - - \sleep(1); - } - - \sleep(1); - - return -1; - } - - private function runAsDeamon() - { - \ob_end_clean(); - \fclose(STDIN); - \fclose(STDOUT); - \fclose(STDERR); - - \register_shutdown_function(function() { \posix_kill(\posix_getpid(), SIGHUP); }); - } - - public function setRunning(bool $run = true) : void - { - $this->run = $run; - $this->suspended = $run; - } - - public function isRunning() : bool - { - return $this->run; - } - - public function isSuspended() : bool - { - return $this->suspended; - } - - public function setSuspended(bool $suspended = true) : void - { - $this->suspended = $suspended; - } - - public function isTerminating() : bool - { - return $this->isTerminating; - } - - public function setTerminating(bool $terminating = true) : void - { - $this->isTerminating = $terminating; - } - - public function isDeamonized() : bool - { - return $this->isDeamonized; - } - - public function setDeamonized(bool $deamonized) : void - { - $this->isDeamonized = $deamonized; - } - - private function savePid() - { - // todo: save pid somewhere for kill - } -} diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index a4ec2c4b0..3a1c76e21 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -254,6 +254,22 @@ $CONFIG = [ 'port' => 11211, ], ], + 'mail' => [ + 'imap' => [ + 'host' => '127.0.0.1', + 'port' => 143, + 'ssl' => false, + 'user' => 'testuser', + 'password' => 'testuser', + ], + 'pop3' => [ + 'host' => '127.0.0.1', + 'port' => 25, + 'ssl' => false, + 'user' => 'testuser', + 'password' => 'testuser', + ], + ], 'log' => [ 'file' => [ 'path' => __DIR__ . '/Logs', diff --git a/tests/Message/Console/HeaderTest.php b/tests/Message/Console/HeaderTest.php new file mode 100644 index 000000000..298223aeb --- /dev/null +++ b/tests/Message/Console/HeaderTest.php @@ -0,0 +1,78 @@ +isLocked()); + self::assertEquals(0, $header->getStatusCode()); + self::assertEquals('1.0', $header->getProtocolVersion()); + self::assertEquals('', $header->getReasonPhrase()); + self::assertEquals([], $header->get('key')); + self::assertFalse($header->has('key')); + self::assertInstanceOf(Localization::class, $header->getL11n()); + self::assertEquals(0, $header->getAccount()); + } + + public function testGetSet() + { + $header = new Header(); + + self::assertTrue($header->set('key', 'header')); + self::assertEquals(['header'], $header->get('key')); + self::assertTrue($header->has('key')); + + self::assertFalse($header->set('key', 'header2')); + self::assertEquals(['header'], $header->get('key')); + + self::assertTrue($header->set('key', 'header3', true)); + self::assertEquals(['header3'], $header->get('key')); + + self::assertTrue($header->remove('key')); + self::assertFalse($header->has('key')); + self::assertFalse($header->remove('key')); + + $header->setAccount(2); + self::AssertEquals(2, $header->getAccount(2)); + } + + public function testLockedHeaderSet() + { + $header = new Header(); + Header::lock(); + self::assertTrue(Header::isLocked()); + self::assertFalse($header->set('key', 'value')); + + TestUtils::setMember('phpOMS\Message\Console\Header', 'isLocked', false); + } + + public function testLockedHeaderRemove() + { + $header = new Header(); + Header::lock(); + self::assertTrue(Header::isLocked()); + self::assertFalse($header->remove('key')); + + TestUtils::setMember('phpOMS\Message\Console\Header', 'isLocked', false); + } +} diff --git a/tests/Message/Console/RequestTest.php b/tests/Message/Console/RequestTest.php new file mode 100644 index 000000000..a8cf99b5d --- /dev/null +++ b/tests/Message/Console/RequestTest.php @@ -0,0 +1,108 @@ +getHeader()->getL11n()->getLanguage()); + self::assertEquals(OSType::LINUX, $request->getOS()); + self::assertEquals('127.0.0.1', $request->getOrigin()); + self::assertEquals([], $request->getHash()); + self::assertEmpty($request->getBody()); + self::assertEquals(RouteVerb::GET, $request->getRouteVerb()); + self::assertEquals(RequestMethod::GET, $request->getMethod()); + self::assertInstanceOf('\phpOMS\Message\Console\Header', $request->getHeader()); + self::assertEquals('', $request->__toString()); + self::assertFalse($request->hasData('key')); + self::assertEquals(null, $request->getData('key')); + } + + public function testSetGet() + { + $request = new Request(new Argument('get:some/test/path'), $l11n = new Localization()); + + $request->setOS(OSType::WINDOWS_XP); + self::assertEquals(OSType::WINDOWS_XP, $request->getOS()); + + $request->setMethod(RequestMethod::PUT); + self::assertEquals(RequestMethod::PUT, $request->getMethod()); + self::assertEquals(RouteVerb::PUT, $request->getRouteVerb()); + + $request->setMethod(RequestMethod::DELETE); + self::assertEquals(RequestMethod::DELETE, $request->getMethod()); + self::assertEquals(RouteVerb::DELETE, $request->getRouteVerb()); + + $request->setMethod(RequestMethod::POST); + self::assertEquals(RequestMethod::POST, $request->getMethod()); + self::assertEquals(RouteVerb::SET, $request->getRouteVerb()); + + self::assertEquals('get:some/test/path', $request->getUri()->__toString()); + + $request->createRequestHashs(0); + self::assertEquals([ + 'eb875812858d27b22cb2b75f992dffadc1b05c66', + 'fa423b369272e7e19b2a5fa4eeba560e74c0d457', + '3e353695aa5bfa63bc373702a75865b3619c4c96', + ], $request->getHash()); + self::assertEquals($l11n, $request->getHeader()->getL11n()); + + self::assertTrue($request->setData('key', 'value')); + self::assertFalse($request->setData('key', 'value2', false)); + self::assertEquals('value', $request->getData('key')); + self::assertTrue($request->hasData('key')); + self::assertEquals(['key' => 'value'], $request->getData()); + + $request->setUri(new Argument('get:some/test/path2')); + $request->createRequestHashs(0); + + self::assertEquals('get:some/test/path2', $request->__toString()); + } + + public function testToString() + { + $request = new Request(new Argument('get:some/test/path')); + self::assertEquals('get:some/test/path', $request->__toString()); + + $request->setData('test', 'data'); + $request->setData('test2', 3); + self::assertEquals('get:some/test/path', $request->__toString()); + + $request = new Request(new Argument('get:some/test/path?test=var')); + self::assertEquals('get:some/test/path?test=var', $request->__toString()); + } + + /** + * @expectedException \Exception + */ + public function testInvalidRouteVerb() + { + $request = new Request(new Argument('get:some/test/path')); + $request->setMethod('failure'); + $request->getRouteVerb(); + } +} diff --git a/tests/Message/Console/ResponseTest.php b/tests/Message/Console/ResponseTest.php new file mode 100644 index 000000000..73c7c1fc4 --- /dev/null +++ b/tests/Message/Console/ResponseTest.php @@ -0,0 +1,39 @@ +getBody()); + self::assertEquals('', $response->render()); + self::assertEquals([], $response->toArray()); + self::assertInstanceOf('\phpOMS\Localization\Localization', $response->getHeader()->getL11n()); + self::assertInstanceOf('\phpOMS\Message\Console\Header', $response->getHeader()); + } + + public function testSetGet() + { + $response = new Response(new Localization()); + + $response->setResponse(['a' => 1]); + self::assertTrue($response->remove('a')); + self::assertFalse($response->remove('a')); + } +} diff --git a/tests/Message/Mail/ImapTest.php b/tests/Message/Mail/ImapTest.php index 381553877..09d5f1a4b 100644 --- a/tests/Message/Mail/ImapTest.php +++ b/tests/Message/Mail/ImapTest.php @@ -17,8 +17,41 @@ use phpOMS\Message\Mail\Imap; class ImapTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testDefault() { - self::markTestIncomplete(); + $email = new Imap( + $GLOBALS['CONFIG']['mail']['imap']['host'], + $GLOBALS['CONFIG']['mail']['imap']['port'], + 30, + $GLOBALS['CONFIG']['mail']['imap']['ssl'] + ); + + self::assertFalse($email->isConnected()); + self::assertTrue($email->connect( + $GLOBALS['CONFIG']['mail']['imap']['user'], + $GLOBALS['CONFIG']['mail']['imap']['password'] + )); + + self::assertTrue($email->isConnected()); + self::assertEquals([], $email->getBoxes()); + self::assertEquals([], $email->getQuota()); + self::assertInstanceOf('\phpOMS\Message\Mail\Mail', $email->getEmail('1')); + self::assertEquals([], $email->getInboxAll()); + self::assertEquals([], $email->getInboxOverview()); + self::assertEquals([], $email->getInboxNew()); + self::assertEquals([], $email->getInboxFrom('')); + self::assertEquals([], $email->getInboxTo('')); + self::assertEquals([], $email->getInboxCc('')); + self::assertEquals([], $email->getInboxBcc('')); + self::assertEquals([], $email->getInboxAnswered()); + self::assertEquals([], $email->getInboxSubject('')); + self::assertEquals([], $email->getInboxSince(new \DateTime('now'))); + self::assertEquals([], $email->getInboxUnseen()); + self::assertEquals([], $email->getInboxSeen()); + self::assertEquals([], $email->getInboxDeleted()); + self::assertEquals([], $email->getInboxText('')); + self::assertEquals([], $email->getMessageOverview(1, 1)); + self::assertEquals(0, $email->countMessages()); + self::assertEquals('', $email->getMessageHeader(1)); } } diff --git a/tests/Utils/IO/Zip/GzTest.php b/tests/Utils/IO/Zip/GzTest.php index 6d140966d..6d6e48215 100644 --- a/tests/Utils/IO/Zip/GzTest.php +++ b/tests/Utils/IO/Zip/GzTest.php @@ -11,14 +11,32 @@ * @link http://website.orange-management.de */ -namespace phpOMS\tests\Utils\IO\Zip; +namespace phpOMS\tests\Utils\IO\Gz; use phpOMS\Utils\IO\Zip\Gz; class GzTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testGz() { - self::markTestIncomplete(); + self::assertTrue(Gz::pack( + 'test a.txt', + __DIR__ . '/test.gz' + )); + + self::assertTrue(\file_exists(__DIR__ . '/test.gz')); + + $a = \file_get_contents(__DIR__ . '/test a.txt'); + + \unlink(__DIR__ . '/test a.txt'); + + self::assertFalse(\file_exists(__DIR__ . '/test a.txt')); + self::assertTrue(Gz::unpack(__DIR__ . '/test.gz', __DIR__)); + self::assertTrue(\file_exists(__DIR__ . '/test a.txt')); + self::assertEquals($a, \file_get_contents(__DIR__ . '/test a.txt')); + + \unlink(__DIR__ . '/test.gz'); + self::assertFalse(\file_exists(__DIR__ . '/test.gz')); + self::assertFalse(Gz::unpack(__DIR__ . '/test.gz', __DIR__)); } } diff --git a/tests/Utils/IO/Zip/TarGzTest.php b/tests/Utils/IO/Zip/TarGzTest.php index bf316eae2..69264a113 100644 --- a/tests/Utils/IO/Zip/TarGzTest.php +++ b/tests/Utils/IO/Zip/TarGzTest.php @@ -11,14 +11,83 @@ * @link http://website.orange-management.de */ -namespace phpOMS\tests\Utils\IO\Zip; +namespace phpOMS\tests\Utils\IO\TarGz; use phpOMS\Utils\IO\Zip\TarGz; class TarGzTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + protected function setUp() { - self::markTestIncomplete(); + if (!extension_loaded('phar')) { + $this->markTestSkipped( + 'The Phar extension is not available.' + ); + } + } + + public function testTarGz() + { + self::assertTrue(TarGz::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test.tar.gz' + )); + + self::assertTrue(\file_exists(__DIR__ . '/test.tar.gz')); + + self::assertFalse(TarGz::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.txt' => 'test b.txt', + ], + __DIR__ . '/test.tar.gz', + false + )); + + $a = \file_get_contents(__DIR__ . '/test a.txt'); + $b = \file_get_contents(__DIR__ . '/test b.md'); + $c = \file_get_contents(__DIR__ . '/test/test c.txt'); + $d = \file_get_contents(__DIR__ . '/test/test d.txt'); + $e = \file_get_contents(__DIR__ . '/test/sub/test e.txt'); + + \unlink(__DIR__ . '/test a.txt'); + \unlink(__DIR__ . '/test b.md'); + \unlink(__DIR__ . '/test/test c.txt'); + \unlink(__DIR__ . '/test/test d.txt'); + \unlink(__DIR__ . '/test/sub/test e.txt'); + \rmdir(__DIR__ . '/test/sub'); + \rmdir(__DIR__ . '/test'); + + self::assertFalse(\file_exists(__DIR__ . '/test a.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test b.md')); + self::assertFalse(\file_exists(__DIR__ . '/test/test c.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/test d.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/sub/test e.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/sub')); + self::assertFalse(\file_exists(__DIR__ . '/test')); + + self::assertTrue(TarGz::unpack(__DIR__ . '/test.tar.gz', __DIR__)); + + self::assertTrue(\file_exists(__DIR__ . '/test a.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test b.md')); + self::assertTrue(\file_exists(__DIR__ . '/test/test c.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/test d.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/sub/test e.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/sub')); + self::assertTrue(\file_exists(__DIR__ . '/test')); + + self::assertEquals($a, \file_get_contents(__DIR__ . '/test a.txt')); + self::assertEquals($b, \file_get_contents(__DIR__ . '/test b.md')); + self::assertEquals($c, \file_get_contents(__DIR__ . '/test/test c.txt')); + self::assertEquals($d, \file_get_contents(__DIR__ . '/test/test d.txt')); + self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt')); + + \unlink(__DIR__ . '/test.tar.gz'); + self::assertFalse(\file_exists(__DIR__ . '/test.tar.gz')); + self::assertFalse(TarGz::unpack(__DIR__ . '/test.tar.gz', __DIR__)); } } diff --git a/tests/Utils/IO/Zip/TarTest.php b/tests/Utils/IO/Zip/TarTest.php index a5eac4a84..2c394e794 100644 --- a/tests/Utils/IO/Zip/TarTest.php +++ b/tests/Utils/IO/Zip/TarTest.php @@ -11,14 +11,83 @@ * @link http://website.orange-management.de */ -namespace phpOMS\tests\Utils\IO\Zip; +namespace phpOMS\tests\Utils\IO\Tar; use phpOMS\Utils\IO\Zip\Tar; class TarTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + protected function setUp() { - self::markTestIncomplete(); + if (!extension_loaded('phar')) { + $this->markTestSkipped( + 'The Phar extension is not available.' + ); + } + } + + public function testTar() + { + self::assertTrue(Tar::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.md' => 'test b.md', + __DIR__ . '/test' => 'test', + ], + __DIR__ . '/test.tar' + )); + + self::assertTrue(\file_exists(__DIR__ . '/test.tar')); + + self::assertFalse(Tar::pack( + [ + __DIR__ . '/test a.txt' => 'test a.txt', + __DIR__ . '/test b.txt' => 'test b.txt', + ], + __DIR__ . '/test.tar', + false + )); + + $a = \file_get_contents(__DIR__ . '/test a.txt'); + $b = \file_get_contents(__DIR__ . '/test b.md'); + $c = \file_get_contents(__DIR__ . '/test/test c.txt'); + $d = \file_get_contents(__DIR__ . '/test/test d.txt'); + $e = \file_get_contents(__DIR__ . '/test/sub/test e.txt'); + + \unlink(__DIR__ . '/test a.txt'); + \unlink(__DIR__ . '/test b.md'); + \unlink(__DIR__ . '/test/test c.txt'); + \unlink(__DIR__ . '/test/test d.txt'); + \unlink(__DIR__ . '/test/sub/test e.txt'); + \rmdir(__DIR__ . '/test/sub'); + \rmdir(__DIR__ . '/test'); + + self::assertFalse(\file_exists(__DIR__ . '/test a.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test b.md')); + self::assertFalse(\file_exists(__DIR__ . '/test/test c.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/test d.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/sub/test e.txt')); + self::assertFalse(\file_exists(__DIR__ . '/test/sub')); + self::assertFalse(\file_exists(__DIR__ . '/test')); + + self::assertTrue(Tar::unpack(__DIR__ . '/test.tar', __DIR__)); + + self::assertTrue(\file_exists(__DIR__ . '/test a.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test b.md')); + self::assertTrue(\file_exists(__DIR__ . '/test/test c.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/test d.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/sub/test e.txt')); + self::assertTrue(\file_exists(__DIR__ . '/test/sub')); + self::assertTrue(\file_exists(__DIR__ . '/test')); + + self::assertEquals($a, \file_get_contents(__DIR__ . '/test a.txt')); + self::assertEquals($b, \file_get_contents(__DIR__ . '/test b.md')); + self::assertEquals($c, \file_get_contents(__DIR__ . '/test/test c.txt')); + self::assertEquals($d, \file_get_contents(__DIR__ . '/test/test d.txt')); + self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt')); + + \unlink(__DIR__ . '/test.tar'); + self::assertFalse(\file_exists(__DIR__ . '/test.tar')); + self::assertFalse(Tar::unpack(__DIR__ . '/test.tar', __DIR__)); } } diff --git a/tests/Utils/JobQueue/JobQueueTest.php b/tests/Utils/JobQueue/JobQueueTest.php deleted file mode 100644 index ef9befc4e..000000000 --- a/tests/Utils/JobQueue/JobQueueTest.php +++ /dev/null @@ -1,24 +0,0 @@ -