Skip to content

Commit

Permalink
Unit tests added #347 #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jan 2, 2023
1 parent 0c53c6a commit 7611206
Show file tree
Hide file tree
Showing 15 changed files with 1,331 additions and 34 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Tests

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

permissions:
contents: read

jobs:
phpunit:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: ['8.0', 8.1, 8.2]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: openssl, json, mbstring, iconv, fileinfo, libxml, zip
coverage: none

- name: Install Composer dependencies
run: composer install --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ vendor
composer.lock
.idea
/build/
test.php
.phpunit.result.cache
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@
}
],
"require": {
"php": ">=7.0.0",
"php": "^8.0.2",
"ext-openssl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-iconv": "*",
"ext-libxml": "*",
"ext-zip": "*",
"ext-fileinfo": "*",
"nesbot/carbon": ">=1.0",
"nesbot/carbon": "^2.62.1",
"symfony/http-foundation": ">=2.8.0",
"illuminate/pagination": ">=5.0.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "^9.5.10",
"symfony/var-dumper": "^6.2"
},
"suggest": {
"symfony/mime": "Recomended for better extension support"
Expand Down
54 changes: 23 additions & 31 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="PHP-IMAP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="PHP-IMAP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>
72 changes: 72 additions & 0 deletions tests/AddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/*
* File: AddressTest.php
* Category: -
* Author: M.Goldenbaum
* Created: 28.12.22 18:11
* Updated: -
*
* Description:
* -
*/

namespace Tests;

use PHPUnit\Framework\TestCase;
use Webklex\PHPIMAP\Address;

class AddressTest extends TestCase {

/**
* Test data
*
* @var array|string[] $data
*/
protected array $data = [
"personal" => "Username",
"mailbox" => "info",
"host" => "domain.tld",
"mail" => "[email protected]",
"full" => "Username <[email protected]>",
];

/**
* Address test
*
* @return void
*/
public function testAddress(): void {
$address = new Address((object)$this->data);

self::assertSame("Username", $address->personal);
self::assertSame("info", $address->mailbox);
self::assertSame("domain.tld", $address->host);
self::assertSame("[email protected]", $address->mail);
self::assertSame("Username <[email protected]>", $address->full);
}

/**
* Test Address to string conversion
*
* @return void
*/
public function testAddressToStringConversion(): void {
$address = new Address((object)$this->data);

self::assertSame("Username <[email protected]>", (string)$address);
}

/**
* Test Address serialization
*
* @return void
*/
public function testAddressSerialization(): void {
$address = new Address((object)$this->data);

foreach($address as $key => $value) {
self::assertSame($this->data[$key], $value);
}

}
}
75 changes: 75 additions & 0 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/*
* File: AttributeTest.php
* Category: -
* Author: M.Goldenbaum
* Created: 28.12.22 18:11
* Updated: -
*
* Description:
* -
*/

namespace Tests;

use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Webklex\PHPIMAP\Attribute;

class AttributeTest extends TestCase {

/**
* String Attribute test
*
* @return void
*/
public function testStringAttribute(): void {
$attribute = new Attribute("foo", "bar");

self::assertSame("bar", $attribute->toString());
self::assertSame("foo", $attribute->getName());
self::assertSame("foos", $attribute->setName("foos")->getName());
}

/**
* Date Attribute test
*
* @return void
*/
public function testDateAttribute(): void {
$attribute = new Attribute("foo", "2022-12-26 08:07:14 GMT-0800");

self::assertInstanceOf(Carbon::class, $attribute->toDate());
self::assertSame("2022-12-26 08:07:14 GMT-0800", $attribute->toDate()->format("Y-m-d H:i:s T"));
}

/**
* Array Attribute test
*
* @return void
*/
public function testArrayAttribute(): void {
$attribute = new Attribute("foo", ["bar"]);

self::assertSame("bar", $attribute->toString());

$attribute->add("bars");
self::assertSame(true, $attribute->has(1));
self::assertSame("bars", $attribute->get(1));
self::assertSame(true, $attribute->contains("bars"));
self::assertSame("foo, bars", $attribute->set("foo", 0)->toString());

$attribute->remove(0);
self::assertSame("bars", $attribute->toString());

self::assertSame("bars, foos", $attribute->merge(["foos", "bars"], true)->toString());
self::assertSame("bars, foos, foos, donk", $attribute->merge(["foos", "donk"], false)->toString());

self::assertSame(4, $attribute->count());

self::assertSame("donk", $attribute->last());
self::assertSame("bars", $attribute->first());

self::assertSame(["bars", "foos", "foos", "donk"], array_values($attribute->all()));
}
}
91 changes: 91 additions & 0 deletions tests/ClientManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/*
* File: ClientManagerTest.php
* Category: -
* Author: M.Goldenbaum
* Created: 28.12.22 18:11
* Updated: -
*
* Description:
* -
*/

namespace Tests;

use PHPUnit\Framework\TestCase;
use Webklex\PHPIMAP\Client;
use Webklex\PHPIMAP\ClientManager;
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
use Webklex\PHPIMAP\IMAP;

class ClientManagerTest extends TestCase {

/** @var ClientManager $cm */
protected ClientManager $cm;

/**
* Setup the test environment.
*
* @return void
*/
public function setUp(): void {
$this->cm = new ClientManager();
}

/**
* Test if the config can be accessed
*
* @return void
*/
public function testConfigAccessorAccount(): void {
self::assertSame("default", ClientManager::get("default"));
self::assertSame("d-M-Y", ClientManager::get("date_format"));
self::assertSame(IMAP::FT_PEEK, ClientManager::get("options.fetch"));
self::assertSame([], ClientManager::get("options.open"));
}

/**
* Test creating a client instance
*
* @throws MaskNotFoundException
*/
public function testMakeClient(): void {
self::assertInstanceOf(Client::class, $this->cm->make([]));
}

/**
* Test accessing accounts
*
* @throws MaskNotFoundException
*/
public function testAccountAccessor(): void {
self::assertSame("default", $this->cm->getDefaultAccount());
self::assertNotEmpty($this->cm->account("default"));

$this->cm->setDefaultAccount("foo");
self::assertSame("foo", $this->cm->getDefaultAccount());
$this->cm->setDefaultAccount("default");
}

/**
* Test setting a config
*
* @throws MaskNotFoundException
*/
public function testSetConfig(): void {
$config = [
"default" => "foo",
"options" => [
"fetch" => IMAP::ST_MSGN,
"open" => "foo"
]
];
$cm = new ClientManager($config);

self::assertSame("foo", $cm->getDefaultAccount());
self::assertInstanceOf(Client::class, $cm->account("foo"));
self::assertSame(IMAP::ST_MSGN, $cm->get("options.fetch"));
self::assertSame(false, is_array($cm->get("options.open")));

}
}
Loading

0 comments on commit 7611206

Please sign in to comment.