Skip to content

MFTF-33585: Eliminated AspectMock usage from TestLoggingUtil #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions dev/tests/unit/Util/TestLoggingUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Util;

use AspectMock\Test as AspectMock;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Util\Logger\MftfLogger;
use Monolog\Handler\TestHandler;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

class TestLoggingUtil extends Assert
class TestLoggingUtil extends TestCase
{
/**
* @var TestLoggingUtil
Expand All @@ -25,24 +26,23 @@ class TestLoggingUtil extends Assert
private $testLogHandler;

/**
* TestLoggingUtil constructor.
* Private constructor.
*/
private function __construct()
{
// private constructor
parent::__construct(null, [], '');
}

/**
* Static singleton get function
* Static singleton get function.
*
* @return TestLoggingUtil
*/
public static function getInstance()
public static function getInstance(): TestLoggingUtil
{
if (self::$instance == null) {
self::$instance = new TestLoggingUtil();
}

return self::$instance;
}

Expand All @@ -51,21 +51,28 @@ public static function getInstance()
*
* @return void
*/
public function setMockLoggingUtil()
public function setMockLoggingUtil(): void
{
$this->testLogHandler = new TestHandler();
$testLogger = new MftfLogger('testLogger');
$testLogger->pushHandler($this->testLogHandler);
$mockLoggingUtil = AspectMock::double(
LoggingUtil::class,
['getLogger' => $testLogger]
)->make();
$property = new \ReflectionProperty(LoggingUtil::class, 'instance');

$mockLoggingUtil = $this->createMock(LoggingUtil::class);
$mockLoggingUtil
->method('getLogger')
->willReturn($testLogger);

$property = new ReflectionProperty(LoggingUtil::class, 'instance');
$property->setAccessible(true);
$property->setValue($mockLoggingUtil);
}

public function validateMockLogEmpty()
/**
* Check if mock log is empty.
*
* @return void
*/
public function validateMockLogEmpty(): void
{
$records = $this->testLogHandler->getRecords();
$this->assertTrue(empty($records));
Expand All @@ -77,9 +84,10 @@ public function validateMockLogEmpty()
* @param string $type
* @param string $message
* @param array $context
*
* @return void
*/
public function validateMockLogStatement($type, $message, $context)
public function validateMockLogStatement(string $type, string $message, array $context): void
{
$records = $this->testLogHandler->getRecords();
$record = $records[count($records)-1]; // we assume the latest record is what requires validation
Expand All @@ -88,7 +96,16 @@ public function validateMockLogStatement($type, $message, $context)
$this->assertEquals($context, $record['context']);
}

public function validateMockLogStatmentRegex($type, $regex, $context)
/**
* Check mock log statement regular expression.
*
* @param string $type
* @param string $regex
* @param array $context
*
* @return void
*/
public function validateMockLogStatmentRegex(string $type, string $regex, array $context): void
{
$records = $this->testLogHandler->getRecords();
$record = $records[count($records)-1]; // we assume the latest record is what requires validation
Expand All @@ -103,8 +120,10 @@ public function validateMockLogStatmentRegex($type, $regex, $context)
*
* @return void
*/
public function clearMockLoggingUtil()
public function clearMockLoggingUtil(): void
{
AspectMock::clean(LoggingUtil::class);
$property = new ReflectionProperty(LoggingUtil::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);
}
}