connection class test case added

This commit is contained in:
Clivern 2017-08-09 18:28:44 +02:00
commit 868cf02463
2 changed files with 81 additions and 3 deletions

View file

@ -12,42 +12,103 @@ namespace Clivern\Imap\Core;
*/
class Connection
{
/**
* @var string
*/
protected $server;
/**
* @var integer
*/
protected $port;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $password;
/**
* @var string
*/
protected $flag;
/**
* @var string
*/
protected $folder;
/**
* @var mixed
*/
protected $stream;
public function __construct($server, $port, $email, $password, $flag = "/novalidate-cert", $folder = "INBOX")
/**
* Class Constructor
*
* @param string $server
* @param string $port
* @param string $email
* @param string $password
* @param string $flag
* @param string $folder
* @return void
*/
public function __construct($server, $port, $email, $password, $flag = "/ssl", $folder = "INBOX")
{
$this->server = $server;
$this->port = $port;
$this->email = $email;
$this->password = $password;
$this->flag = $flag;
$this->folder = $folder;
}
/**
* Connect to IMAP Email
*
* @return Connection
*/
public function connect()
{
$this->stream = imap_open("{{$this->server}:{$this->port}{$this->flag}}{$this->folder}", $this->email, $this->password);
$this->stream = imap_open("{" . $this->server . ":" . $this->port . $this->flag . "}" . $this->folder, $this->email, $this->password);
return $this;
}
/**
* Get Stream
*
* @return mixed
*/
public function getStream()
{
return $this->stream;
}
/**
* Check Connection
*
* @return boolean
*/
public function checkConnection()
{
return (!is_null($this->stream) && imap_ping($this->stream));
}
/**
* Disconnect
*
* @param integer $flag
* @return boolean
*/
public function disconnect($flag = 0)
{
if( !is_null($this->stream) && !imap_ping($this->stream) ){
if( !is_null($this->stream) && imap_ping($this->stream) ){
if( imap_close($this->stream, $flag) ){
$this->stream = null;
return true;
@ -56,6 +117,7 @@ class Connection
}
}
var_dump("dd");
return false;
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Tests\Core;
use PHPUnit\Framework\TestCase;
class ConnectionTest extends TestCase
{
public function testConnect()
{
$email = getenv("TEST_EMAIL");
$password = getenv("TEST_EMAIL_PASS");
$connection = new \Clivern\Imap\Core\Connection("imap.gmail.com", "993", $email, $password, "/ssl", "INBOX");
$this->assertTrue($connection->connect()->checkConnection());
$this->assertTrue($connection->connect()->disconnect());
}
}