Skip to content

Commit 7611206

Browse files
committed
Unit tests added Webklex#347 Webklex#242
1 parent 0c53c6a commit 7611206

15 files changed

+1331
-34
lines changed

.github/workflows/tests.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
phpunit:
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
fail-fast: true
18+
matrix:
19+
php: ['8.0', 8.1, 8.2]
20+
21+
name: PHP ${{ matrix.php }}
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v3
26+
27+
- name: Setup PHP
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: ${{ matrix.php }}
31+
extensions: openssl, json, mbstring, iconv, fileinfo, libxml, zip
32+
coverage: none
33+
34+
- name: Install Composer dependencies
35+
run: composer install --prefer-dist --no-interaction --no-progress
36+
37+
- name: Execute tests
38+
run: vendor/bin/phpunit

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor
22
composer.lock
33
.idea
44
/build/
5+
test.php
6+
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">=7.0.0",
22+
"php": "^8.0.2",
2323
"ext-openssl": "*",
2424
"ext-json": "*",
2525
"ext-mbstring": "*",
2626
"ext-iconv": "*",
27+
"ext-libxml": "*",
28+
"ext-zip": "*",
2729
"ext-fileinfo": "*",
28-
"nesbot/carbon": ">=1.0",
30+
"nesbot/carbon": "^2.62.1",
2931
"symfony/http-foundation": ">=2.8.0",
3032
"illuminate/pagination": ">=5.0.0"
3133
},
3234
"require-dev": {
33-
"phpunit/phpunit": "~4.0"
35+
"phpunit/phpunit": "^9.5.10",
36+
"symfony/var-dumper": "^6.2"
3437
},
3538
"suggest": {
3639
"symfony/mime": "Recomended for better extension support"

phpunit.xml

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="PHP-IMAP Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
29-
<php>
30-
<env name="APP_ENV" value="testing"/>
31-
</php>
32-
</phpunit>
2+
<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">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="build/logs/clover.xml"/>
9+
<html outputDirectory="build/coverage"/>
10+
<text outputFile="build/coverage.txt"/>
11+
</report>
12+
</coverage>
13+
<testsuites>
14+
<testsuite name="PHP-IMAP Test Suite">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<logging>
19+
<junit outputFile="build/report.junit.xml"/>
20+
</logging>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
</php>
24+
</phpunit>

tests/AddressTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/*
3+
* File: AddressTest.php
4+
* Category: -
5+
* Author: M.Goldenbaum
6+
* Created: 28.12.22 18:11
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Tests;
14+
15+
use PHPUnit\Framework\TestCase;
16+
use Webklex\PHPIMAP\Address;
17+
18+
class AddressTest extends TestCase {
19+
20+
/**
21+
* Test data
22+
*
23+
* @var array|string[] $data
24+
*/
25+
protected array $data = [
26+
"personal" => "Username",
27+
"mailbox" => "info",
28+
"host" => "domain.tld",
29+
"mail" => "[email protected]",
30+
"full" => "Username <[email protected]>",
31+
];
32+
33+
/**
34+
* Address test
35+
*
36+
* @return void
37+
*/
38+
public function testAddress(): void {
39+
$address = new Address((object)$this->data);
40+
41+
self::assertSame("Username", $address->personal);
42+
self::assertSame("info", $address->mailbox);
43+
self::assertSame("domain.tld", $address->host);
44+
self::assertSame("[email protected]", $address->mail);
45+
self::assertSame("Username <[email protected]>", $address->full);
46+
}
47+
48+
/**
49+
* Test Address to string conversion
50+
*
51+
* @return void
52+
*/
53+
public function testAddressToStringConversion(): void {
54+
$address = new Address((object)$this->data);
55+
56+
self::assertSame("Username <[email protected]>", (string)$address);
57+
}
58+
59+
/**
60+
* Test Address serialization
61+
*
62+
* @return void
63+
*/
64+
public function testAddressSerialization(): void {
65+
$address = new Address((object)$this->data);
66+
67+
foreach($address as $key => $value) {
68+
self::assertSame($this->data[$key], $value);
69+
}
70+
71+
}
72+
}

tests/AttributeTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/*
3+
* File: AttributeTest.php
4+
* Category: -
5+
* Author: M.Goldenbaum
6+
* Created: 28.12.22 18:11
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Tests;
14+
15+
use Carbon\Carbon;
16+
use PHPUnit\Framework\TestCase;
17+
use Webklex\PHPIMAP\Attribute;
18+
19+
class AttributeTest extends TestCase {
20+
21+
/**
22+
* String Attribute test
23+
*
24+
* @return void
25+
*/
26+
public function testStringAttribute(): void {
27+
$attribute = new Attribute("foo", "bar");
28+
29+
self::assertSame("bar", $attribute->toString());
30+
self::assertSame("foo", $attribute->getName());
31+
self::assertSame("foos", $attribute->setName("foos")->getName());
32+
}
33+
34+
/**
35+
* Date Attribute test
36+
*
37+
* @return void
38+
*/
39+
public function testDateAttribute(): void {
40+
$attribute = new Attribute("foo", "2022-12-26 08:07:14 GMT-0800");
41+
42+
self::assertInstanceOf(Carbon::class, $attribute->toDate());
43+
self::assertSame("2022-12-26 08:07:14 GMT-0800", $attribute->toDate()->format("Y-m-d H:i:s T"));
44+
}
45+
46+
/**
47+
* Array Attribute test
48+
*
49+
* @return void
50+
*/
51+
public function testArrayAttribute(): void {
52+
$attribute = new Attribute("foo", ["bar"]);
53+
54+
self::assertSame("bar", $attribute->toString());
55+
56+
$attribute->add("bars");
57+
self::assertSame(true, $attribute->has(1));
58+
self::assertSame("bars", $attribute->get(1));
59+
self::assertSame(true, $attribute->contains("bars"));
60+
self::assertSame("foo, bars", $attribute->set("foo", 0)->toString());
61+
62+
$attribute->remove(0);
63+
self::assertSame("bars", $attribute->toString());
64+
65+
self::assertSame("bars, foos", $attribute->merge(["foos", "bars"], true)->toString());
66+
self::assertSame("bars, foos, foos, donk", $attribute->merge(["foos", "donk"], false)->toString());
67+
68+
self::assertSame(4, $attribute->count());
69+
70+
self::assertSame("donk", $attribute->last());
71+
self::assertSame("bars", $attribute->first());
72+
73+
self::assertSame(["bars", "foos", "foos", "donk"], array_values($attribute->all()));
74+
}
75+
}

tests/ClientManagerTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/*
3+
* File: ClientManagerTest.php
4+
* Category: -
5+
* Author: M.Goldenbaum
6+
* Created: 28.12.22 18:11
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Tests;
14+
15+
use PHPUnit\Framework\TestCase;
16+
use Webklex\PHPIMAP\Client;
17+
use Webklex\PHPIMAP\ClientManager;
18+
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
19+
use Webklex\PHPIMAP\IMAP;
20+
21+
class ClientManagerTest extends TestCase {
22+
23+
/** @var ClientManager $cm */
24+
protected ClientManager $cm;
25+
26+
/**
27+
* Setup the test environment.
28+
*
29+
* @return void
30+
*/
31+
public function setUp(): void {
32+
$this->cm = new ClientManager();
33+
}
34+
35+
/**
36+
* Test if the config can be accessed
37+
*
38+
* @return void
39+
*/
40+
public function testConfigAccessorAccount(): void {
41+
self::assertSame("default", ClientManager::get("default"));
42+
self::assertSame("d-M-Y", ClientManager::get("date_format"));
43+
self::assertSame(IMAP::FT_PEEK, ClientManager::get("options.fetch"));
44+
self::assertSame([], ClientManager::get("options.open"));
45+
}
46+
47+
/**
48+
* Test creating a client instance
49+
*
50+
* @throws MaskNotFoundException
51+
*/
52+
public function testMakeClient(): void {
53+
self::assertInstanceOf(Client::class, $this->cm->make([]));
54+
}
55+
56+
/**
57+
* Test accessing accounts
58+
*
59+
* @throws MaskNotFoundException
60+
*/
61+
public function testAccountAccessor(): void {
62+
self::assertSame("default", $this->cm->getDefaultAccount());
63+
self::assertNotEmpty($this->cm->account("default"));
64+
65+
$this->cm->setDefaultAccount("foo");
66+
self::assertSame("foo", $this->cm->getDefaultAccount());
67+
$this->cm->setDefaultAccount("default");
68+
}
69+
70+
/**
71+
* Test setting a config
72+
*
73+
* @throws MaskNotFoundException
74+
*/
75+
public function testSetConfig(): void {
76+
$config = [
77+
"default" => "foo",
78+
"options" => [
79+
"fetch" => IMAP::ST_MSGN,
80+
"open" => "foo"
81+
]
82+
];
83+
$cm = new ClientManager($config);
84+
85+
self::assertSame("foo", $cm->getDefaultAccount());
86+
self::assertInstanceOf(Client::class, $cm->account("foo"));
87+
self::assertSame(IMAP::ST_MSGN, $cm->get("options.fetch"));
88+
self::assertSame(false, is_array($cm->get("options.open")));
89+
90+
}
91+
}

0 commit comments

Comments
 (0)