diff --git a/.github/actions/test_tests-imap.sh b/.github/actions/test_tests-imap.sh
index 154b7aa923e..23de9f72b8c 100755
--- a/.github/actions/test_tests-imap.sh
+++ b/.github/actions/test_tests-imap.sh
@@ -1,23 +1,14 @@
#!/bin/bash
set -e -u -x -o pipefail
-ATOUM_ADDITIONNAL_OPTIONS=""
+PHPUNIT_ADDITIONNAL_OPTIONS=""
if [[ "$CODE_COVERAGE" = true ]]; then
export COVERAGE_DIR="coverage-imap"
+ PHPUNIT_ADDITIONNAL_OPTIONS="--coverage-filter src --coverage-clover phpunit/$COVERAGE_DIR/clover.xml"
else
- ATOUM_ADDITIONNAL_OPTIONS="--no-code-coverage";
+ PHPUNIT_ADDITIONNAL_OPTIONS="--no-coverage";
fi
-vendor/bin/atoum \
- -p 'php -d memory_limit=512M' \
- --debug \
- --force-terminal \
- --use-dot-report \
- --bootstrap-file tests/bootstrap.php \
- --fail-if-void-methods \
- --fail-if-skipped-methods \
- $ATOUM_ADDITIONNAL_OPTIONS \
- --max-children-number 1 \
- -d tests/imap
+vendor/bin/phpunit $PHPUNIT_ADDITIONNAL_OPTIONS phpunit/imap
unset COVERAGE_DIR
diff --git a/tests/imap/MailCollector.php b/phpunit/imap/MailCollectorTest.php
similarity index 81%
rename from tests/imap/MailCollector.php
rename to phpunit/imap/MailCollectorTest.php
index b207549a710..303aabf5084 100644
--- a/tests/imap/MailCollector.php
+++ b/phpunit/imap/MailCollectorTest.php
@@ -42,49 +42,50 @@
use NotificationTarget;
use NotificationTargetSoftwareLicense;
use NotificationTargetTicket;
+use PHPUnit\Framework\Attributes\DataProvider;
use SoftwareLicense;
use Ticket;
use Psr\Log\LogLevel;
-class MailCollector extends DbTestCase
+class MailCollectorTest extends DbTestCase
{
private $collector;
private $mailgate_id;
public function testGetEmpty()
{
- $this
- ->if($this->newTestedInstance)
- ->then
- ->array($this->testedInstance->fields)
- ->isIdenticalTo([])
- ->boolean($this->testedInstance->getEmpty())
- ->array($this->testedInstance->fields)
- ->isIdenticalTo([
- 'id' => '',
- 'name' => '',
- 'host' => '',
- 'login' => '',
- 'filesize_max' => '2097152',
- 'is_active' => 1,
- 'date_mod' => '',
- 'comment' => '',
- 'passwd' => '',
- 'accepted' => '',
- 'refused' => '',
- 'errors' => '',
- 'use_mail_date' => '',
- 'date_creation' => '',
- 'requester_field' => '',
- 'add_to_to_observer' => '',
- 'add_cc_to_observer' => '',
- 'collect_only_unread' => '',
- 'create_user_from_email' => '',
- 'last_collect_date' => '',
- ]);
+ $instance = new \MailCollector();
+ $this->assertSame([], $instance->fields);
+
+ $this->assertTrue($instance->getEmpty());
+ $this->assertSame(
+ [
+ 'id' => '',
+ 'name' => '',
+ 'host' => '',
+ 'login' => '',
+ 'filesize_max' => '2097152',
+ 'is_active' => 1,
+ 'date_mod' => '',
+ 'comment' => '',
+ 'passwd' => '',
+ 'accepted' => '',
+ 'refused' => '',
+ 'errors' => '',
+ 'use_mail_date' => '',
+ 'date_creation' => '',
+ 'requester_field' => '',
+ 'add_to_to_observer' => '',
+ 'add_cc_to_observer' => '',
+ 'collect_only_unread' => '',
+ 'create_user_from_email' => '',
+ 'last_collect_date' => '',
+ ],
+ $instance->fields
+ );
}
- protected function subjectProvider()
+ public static function subjectProvider()
{
return [
[
@@ -103,106 +104,109 @@ protected function subjectProvider()
];
}
- /**
- * @dataProvider subjectProvider
- */
+ #[DataProvider('subjectProvider')]
public function testCleanSubject($raw, $expected)
{
- $this
- ->if($this->newTestedInstance)
- ->then
- ->string($this->testedInstance->cleanSubject($raw))
- ->isIdenticalTo($expected);
+ $instance = new \MailCollector();
+ $this->assertSame($expected, $instance->cleanSubject($raw));
}
public function testPrepareInput()
{
$_SESSION['glpicronuserrunning'] = 'cron_phpunit';
- $this->newTestedInstance();
+ $instance = new \MailCollector();
$oinput = [
'passwd' => 'Ph34r',
'is_active' => true
];
- $prepared = $this->testedInstance->prepareInput($oinput, 'add');
- $this->array($prepared)
- ->boolean['is_active']->isTrue()
- ->string['passwd']->isNotEqualTo($oinput['passwd']);
+ $prepared = $instance->prepareInput($oinput, 'add');
+ $this->assertIsArray($prepared);
+ $this->assertTrue($prepared['is_active']);
+ $this->assertNotEquals($oinput['passwd'], $prepared['passwd']);
- //empty password means no password.
+ //empty password means no password.
$oinput = [
'passwd' => '',
'is_active' => true
];
- $this->array($this->testedInstance->prepareInput($oinput, 'add'))
- ->isIdenticalTo(['is_active' => true]);
+ $this->assertSame(
+ ['is_active' => true],
+ $instance->prepareInput($oinput, 'add')
+ );
- //manage host
+ //manage host
$oinput = [
'mail_server' => 'mail.example.com'
];
- $this->array($this->testedInstance->prepareInput($oinput, 'add'))
- ->isIdenticalTo(['mail_server' => 'mail.example.com', 'host' => '{mail.example.com}']);
+ $this->assertSame(
+ ['mail_server' => 'mail.example.com', 'host' => '{mail.example.com}'],
+ $instance->prepareInput($oinput, 'add')
+ );
- //manage host
+ //manage host
$oinput = [
'mail_server' => 'mail.example.com',
'server_port' => 143,
'server_mailbox' => 'bugs'
];
- $this->array($this->testedInstance->prepareInput($oinput, 'add'))
- ->isIdenticalTo([
- 'mail_server' => 'mail.example.com',
- 'server_port' => 143,
- 'server_mailbox' => 'bugs',
- 'host' => '{mail.example.com:143}bugs'
- ]);
+ $this->assertSame(
+ [
+ 'mail_server' => 'mail.example.com',
+ 'server_port' => 143,
+ 'server_mailbox' => 'bugs',
+ 'host' => '{mail.example.com:143}bugs'
+ ],
+ $instance->prepareInput($oinput, 'add')
+ );
$oinput = [
'passwd' => 'Ph34r',
'_blank_passwd' => true
];
- $this->array($this->testedInstance->prepareInputForUpdate($oinput))
- ->isIdenticalTo(['passwd' => '', '_blank_passwd' => true]);
+ $this->assertSame(
+ ['passwd' => '', '_blank_passwd' => true],
+ $instance->prepareInputForUpdate($oinput)
+ );
}
public function testCounts()
{
$_SESSION['glpicronuserrunning'] = 'cron_phpunit';
- $this->newTestedInstance();
+ $instance = new \MailCollector();
- $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(0);
- $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(0);
- $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(0);
+ $this->assertEquals(0, $instance->countActiveCollectors());
+ $this->assertEquals(0, $instance->countCollectors(true));
+ $this->assertEquals(0, $instance->countCollectors());
- //Add an active collector
- $nid = (int)$this->testedInstance->add([
+ //Add an active collector
+ $nid = (int)$instance->add([
'name' => 'Maille name',
'is_active' => true
]);
- $this->integer($nid)->isGreaterThan(0);
+ $this->assertGreaterThan(0, $nid);
- $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(1);
- $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(1);
- $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(1);
+ $this->assertEquals(1, $instance->countActiveCollectors());
+ $this->assertEquals(1, $instance->countCollectors(true));
+ $this->assertEquals(1, $instance->countCollectors());
- $this->boolean(
- $this->testedInstance->update([
- 'id' => $this->testedInstance->fields['id'],
+ $this->assertTrue(
+ $instance->update([
+ 'id' => $instance->fields['id'],
'is_active' => false
])
- )->isTrue();
+ );
- $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(0);
- $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(0);
- $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(1);
+ $this->assertEquals(0, $instance->countActiveCollectors());
+ $this->assertEquals(0, $instance->countCollectors(true));
+ $this->assertEquals(1, $instance->countCollectors());
}
- protected function messageIdHeaderProvider()
+ public static function messageIdHeaderProvider()
{
$root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true);
@@ -311,12 +315,10 @@ protected function messageIdHeaderProvider()
];
}
- /**
- * @dataProvider messageIdHeaderProvider
- */
+ #[DataProvider('messageIdHeaderProvider')]
public function testIsMessageSentByGlpi(array $headers, bool $expected)
{
- $this->newTestedInstance();
+ $instance = new \MailCollector();
$message = new Message(
[
@@ -325,10 +327,10 @@ public function testIsMessageSentByGlpi(array $headers, bool $expected)
]
);
- $this->boolean($this->testedInstance->isMessageSentByGlpi($message))->isEqualTo($expected);
+ $this->assertEquals($expected, $instance->isMessageSentByGlpi($message));
}
- protected function itemReferenceHeaderProvider()
+ public static function itemReferenceHeaderProvider()
{
$root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true);
@@ -529,16 +531,14 @@ protected function itemReferenceHeaderProvider()
];
}
- /**
- * @dataProvider itemReferenceHeaderProvider
- */
+ #[DataProvider('itemReferenceHeaderProvider')]
public function testGetItemFromHeader(
array $headers,
?string $expected_itemtype,
?int $expected_items_id,
bool $accepted
) {
- $this->newTestedInstance();
+ $instance = new \MailCollector();
$message = new Message(
[
@@ -547,26 +547,24 @@ public function testGetItemFromHeader(
]
);
- $item = $this->testedInstance->getItemFromHeaders($message);
+ $item = $instance->getItemFromHeaders($message);
if ($expected_itemtype === null) {
- $this->variable($item)->isNull();
+ $this->assertNull($item);
} else {
- $this->object($item)->isInstanceOf($expected_itemtype);
- $this->integer($item->getId())->isEqualTo($expected_items_id);
+ $this->assertInstanceOf($expected_itemtype, $item);
+ $this->assertEquals($expected_items_id, $item->getId());
}
}
- /**
- * @dataProvider itemReferenceHeaderProvider
- */
+ #[DataProvider('itemReferenceHeaderProvider')]
public function testIsResponseToMessageSentByAnotherGlpi(
array $headers,
?string $expected_itemtype,
?int $expected_items_id,
bool $accepted
) {
- $this->newTestedInstance();
+ $instance = new \MailCollector();
$message = new Message(
[
@@ -575,14 +573,13 @@ public function testIsResponseToMessageSentByAnotherGlpi(
]
);
- $this->boolean($this->testedInstance->isResponseToMessageSentByAnotherGlpi($message))->isEqualTo(!$accepted);
+ $this->assertEquals(!$accepted, $instance->isResponseToMessageSentByAnotherGlpi($message));
}
private function doConnect()
{
if (null === $this->collector) {
- $this->newTestedInstance();
- $collector = $this->testedInstance;
+ $collector = new \MailCollector();
$this->collector = $collector;
} else {
$collector = $this->collector;
@@ -604,12 +601,12 @@ private function doConnect()
'requester_field' => \MailCollector::REQUESTER_FIELD_REPLY_TO,
]);
- $this->integer($this->mailgate_id)->isGreaterThan(0);
+ $this->assertGreaterThan(0, $this->mailgate_id);
- $this->boolean($collector->getFromDB($this->mailgate_id))->isTrue();
- $this->string($collector->fields['host'])->isIdenticalTo('{dovecot:143/imap/novalidate-cert}');
+ $this->assertTrue($collector->getFromDB($this->mailgate_id));
+ $this->assertSame('{dovecot:143/imap/novalidate-cert}', $collector->fields['host']);
$collector->connect();
- $this->variable($collector->fields['errors'])->isEqualTo(0);
+ $this->assertEquals(0, $collector->fields['errors']);
}
public function testCollect()
@@ -617,38 +614,41 @@ public function testCollect()
global $DB;
$_SESSION['glpicronuserrunning'] = 'cron_phpunit';
- // Force notification_uuid
+ // Force notification_uuid
Config::setConfigurationValues('core', ['notification_uuid' => 't3StN0t1f1c4tiOnUUID']);
- //assign email to user
+ //assign email to user
$nuid = getItemByTypeName('User', 'normal', true);
$uemail = new \UserEmail();
- $this->integer(
+ $this->assertGreaterThan(
+ 0,
(int)$uemail->add([
'users_id' => $nuid,
'is_default' => 1,
'email' => 'normal@glpi-project.org'
])
- )->isGreaterThan(0);
+ );
$tuid = getItemByTypeName('User', 'tech', true);
- $this->integer(
+ $this->assertGreaterThan(
+ 0,
(int)$uemail->add([
'users_id' => $tuid,
'is_default' => 1,
'email' => 'tech@glpi-project.org'
])
- )->isGreaterThan(0);
+ );
- // Hack to allow documents named "1234567890", "1234567890_2", ... (cf 28-multiple-attachments-no-extension.eml)
+ // Hack to allow documents named "1234567890", "1234567890_2", ... (cf 28-multiple-attachments-no-extension.eml)
$doctype = new \DocumentType();
- $this->integer(
+ $this->assertGreaterThan(
+ 0,
$doctype->add([
'name' => 'Type test',
'ext' => '/^1234567890(_\d+)?$/'
])
- )->isGreaterThan(0);
+ );
- // Collect all mails
+ // Collect all mails
$this->doConnect();
$this->collector->maxfetch_emails = 1000; // Be sure to fetch all mails from test suite
@@ -659,16 +659,11 @@ public function testCollect()
'Header with Name date or date not found' => LogLevel::CRITICAL,
];
- $msg = null;
- $this->when(
- function () use (&$msg) {
- $msg = $this->collector->collect($this->mailgate_id);
- }
- )
- ->error()
- ->withType(E_USER_WARNING)
- ->withMessage('Invalid header "X-Invalid-Encoding"')
- ->exists();
+ $msg = $this->collector->collect($this->mailgate_id);
+ $this->hasPhpLogRecordThatContains(
+ 'Invalid header "X-Invalid-Encoding"',
+ LogLevel::WARNING
+ );
// Check error log and clean it (to prevent test failure, see GLPITestCase::afterTestMethod()).
foreach ($expected_logged_errors as $error_message => $error_level) {
@@ -681,7 +676,7 @@ function () use (&$msg) {
$expected_blacklist_count = 3;
$expected_expected_already_seen = 0;
- $this->variable($msg)->isIdenticalTo(
+ $this->assertSame(
sprintf(
'Number of messages: available=%1$s, already imported=%2$d, retrieved=%3$s, refused=%4$s, errors=%5$s, blacklisted=%6$s',
$total_count,
@@ -690,10 +685,11 @@ function () use (&$msg) {
$expected_refused_count,
$expected_error_count,
$expected_blacklist_count
- )
+ ),
+ $msg
);
- // Check not imported emails
+ // Check not imported emails
$not_imported_specs = [
[
'subject' => 'Have a problem, can you help me?',
@@ -715,7 +711,7 @@ function () use (&$msg) {
]
];
$iterator = $DB->request(['FROM' => \NotImportedEmail::getTable()]);
- $this->integer(count($iterator))->isIdenticalTo(count($not_imported_specs));
+ $this->assertEquals(count($not_imported_specs), count($iterator));
$not_imported_values = [];
foreach ($iterator as $data) {
@@ -725,13 +721,13 @@ function () use (&$msg) {
'to' => $data['to'],
'reason' => $data['reason'],
];
- $this->integer($data['mailcollectors_id'])->isIdenticalTo($this->mailgate_id);
+ $this->assertSame($this->mailgate_id, $data['mailcollectors_id']);
}
- $this->array($not_imported_values)->isIdenticalTo($not_imported_specs);
+ $this->assertSame($not_imported_specs, $not_imported_values);
- // Check created tickets and their actors
+ // Check created tickets and their actors
$actors_specs = [
- // Mails having "tech" user as requester
+ // Mails having "tech" user as requester
[
'users_id' => $tuid,
'actor_type' => \CommonITILActor::REQUESTER,
@@ -743,7 +739,7 @@ function () use (&$msg) {
'Ticket with multiple to',
]
],
- // Mails having "normal" user as requester
+ // Mails having "normal" user as requester
[
'users_id' => $nuid,
'actor_type' => \CommonITILActor::REQUESTER,
@@ -795,9 +791,9 @@ function () use (&$msg) {
],
];
- // Tickets on which content should be checked (key is ticket name)
+ // Tickets on which content should be checked (key is ticket name)
$tickets_contents = [
- // Plain text on mono-part email
+ // Plain text on mono-part email
'PHP fatal error' => <<