Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'remotes/github/develop' into change/PHP…
Browse files Browse the repository at this point in the history
…LIB-326/debug-output-nach-fehlgeschlagenem-test (#183)

* [change] (PHPLIB-326) Enable test output for failed tests even when verbose test logging is disabled.

* [change] (PHPLIB-326) Replace environment variable for test log output and set default to false (no output).

* [change] (PHPLIB-326) Replace environment variable for test log output and set default to false (no output).

* [change] (PHPLIB-326) Update CHANGELOG.md.

* [change] (PHPLIB-326) Fix style issues.

* [change] (PHPLIB-326) Add new line before and after log dump.

* [change] (PHPLIB-326) Fix version in deprecation notice.

Co-authored-by: sixer1182 <[email protected]>
  • Loading branch information
Simon Gabriel and sixer1182 committed Jul 24, 2020
1 parent 9dc91b6 commit b902ea8
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

### Change
* Mask auth header in debug log.
* Refactor test logging.
* Replace test environment variable.
* Apply minor changes.

### Fix
Expand Down
27 changes: 25 additions & 2 deletions src/Services/EnvironmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class EnvironmentService
const ENV_VAR_VALUE_DEVELOPMENT_ENVIRONMENT = 'DEV';
const ENV_VAR_VALUE_PROD_ENVIRONMENT = 'PROD';

/** @deprecated ENV_VAR_NAME_DISABLE_TEST_LOGGING since 1.2.7.3 replaced by ENV_VAR_NAME_VERBOSE_TEST_LOGGING */
const ENV_VAR_NAME_DISABLE_TEST_LOGGING = 'HEIDELPAY_MGW_DISABLE_TEST_LOGGING';
const ENV_VAR_NAME_VERBOSE_TEST_LOGGING = 'HEIDELPAY_MGW_VERBOSE_TEST_LOGGING';

const ENV_VAR_TEST_PRIVATE_KEY = 'HEIDELPAY_MGW_TEST_PRIVATE_KEY';
const ENV_VAR_TEST_PUBLIC_KEY = 'HEIDELPAY_MGW_TEST_PUBLIC_KEY';
Expand All @@ -45,6 +47,23 @@ class EnvironmentService

const ENV_VAR_NAME_CURL_VERBOSE = 'HEIDELPAY_MGW_CURL_VERBOSE';

/**
* Returns the value of the given env var as bool.
*
* @param string $varName
*
* @return bool
*/
protected static function getBoolEnvValue(string $varName): bool
{
/** @noinspection ProperNullCoalescingOperatorUsageInspection */
$envVar = $_SERVER[$varName] ?? false;
if (!is_bool($envVar)) {
$envVar = in_array(strtolower($envVar), [true, 'true', '1'], true);
}
return $envVar;
}

/**
* Returns the MGW environment set via environment variable or PROD es default.
*
Expand All @@ -62,8 +81,12 @@ public function getMgwEnvironment(): string
*/
public static function isTestLoggingActive(): bool
{
$testLoggingDisabled = strtolower($_SERVER[self::ENV_VAR_NAME_DISABLE_TEST_LOGGING] ?? 'false');
return in_array($testLoggingDisabled, ['false', '0'], true);
if (isset($_SERVER[self::ENV_VAR_NAME_VERBOSE_TEST_LOGGING])) {
$verboseLogging = self::getBoolEnvValue(self::ENV_VAR_NAME_VERBOSE_TEST_LOGGING);
} else {
$verboseLogging = !self::getBoolEnvValue(self::ENV_VAR_NAME_DISABLE_TEST_LOGGING);
}
return $verboseLogging;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/BasePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\BaseTestRunner;
use RuntimeException;

class BasePaymentTest extends TestCase
Expand All @@ -66,6 +67,25 @@ protected function setUp()
$this->childSetup();
}

/**
* If verbose test output is disabled echo debug log when test did not pass.
*
* {@inheritDoc}
*/
protected function tearDown()
{
/** @var TestDebugHandler $debugHandler */
$debugHandler = $this->heidelpay->getDebugHandler();

if ($this->getStatus() === BaseTestRunner::STATUS_PASSED) {
$debugHandler->clearTempLog();
} else {
echo "\n";
$debugHandler->dumpTempLog();
echo "\n";
}
}

/**
* Override this in the child test class to perform custom setup tasks e.g. setting a different Key.
*/
Expand Down
28 changes: 27 additions & 1 deletion test/TestDebugHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,39 @@

class TestDebugHandler implements DebugHandlerInterface
{
/** @var string $tempLog Stores the log messages until reset via clearTempLog() or echoed out via dumpTempLog(). */
private $tempLog = '';

/**
* {@inheritDoc}
*/
public function log(string $message)
{
$logMessage = 'heidelpay debug message: ' . $message . "\n";

if (EnvironmentService::isTestLoggingActive()) {
echo 'heidelpay debug message: ' . $message . "\n";
// Echo log messages directly.
echo $logMessage;
} else {
// Store log to echo it when needed.
$this->tempLog .= $logMessage;
}
}

/**
* Clears the temp log.
*/
public function clearTempLog(): void
{
$this->tempLog = '';
}

/**
* Echos the contents of tempLog and clears it afterwards.
*/
public function dumpTempLog(): void
{
echo $this->tempLog;
$this->clearTempLog();
}
}
1 change: 1 addition & 0 deletions test/unit/Resources/PaymentTypes/CardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function invalidExpiryDateDataProvider(): array
*/
protected function setUp()
{
parent::setUp();
$this->card = new Card($this->number, $this->expiryDate);
}

Expand Down
114 changes: 114 additions & 0 deletions test/unit/Services/EnvironmentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* This test is verifying that the set environment variables will lead to the correct configuration.
*
* Copyright (C) 2020 heidelpay GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://docs.heidelpay.com/
*
* @author Simon Gabriel <[email protected]>
*
* @package heidelpayPHP\test\unit\Services
*/
namespace heidelpayPHP\test\unit\Services;

use heidelpayPHP\Services\EnvironmentService;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class EnvironmentServiceTest extends TestCase
{
/**
* Verify test logging environment vars are correctly interpreted.
*
* @test
* @dataProvider envVarsShouldBeInterpretedAsExpectedDP
*
* @param mixed $logDisabled
* @param mixed $verboseLog
* @param bool $expectedLogEnabled
*
* @throws ExpectationFailedException
*/
public function envVarsShouldBeInterpretedAsExpected($logDisabled, $verboseLog, $expectedLogEnabled): void
{
unset(
$_SERVER[EnvironmentService::ENV_VAR_NAME_DISABLE_TEST_LOGGING],
$_SERVER[EnvironmentService::ENV_VAR_NAME_VERBOSE_TEST_LOGGING]
);

if ($logDisabled !== null) {
$_SERVER[EnvironmentService::ENV_VAR_NAME_DISABLE_TEST_LOGGING] = $logDisabled;
}

if ($verboseLog !== null) {
$_SERVER[EnvironmentService::ENV_VAR_NAME_VERBOSE_TEST_LOGGING] = $verboseLog;
}

$this->assertEquals($expectedLogEnabled, EnvironmentService::isTestLoggingActive());
}

/**
* Data provider for envVarsShouldBeInterpretedAsExpected.
*
* @return array
*/
public function envVarsShouldBeInterpretedAsExpectedDP(): array
{
return [
'#0' => [null, null, true],
'#1' => [0, null, true],
'#2' => [1, null, false],
'#3' => [null, 0, false],
'#4' => [null, 1, true],
'#5' => [0, 0, false],
'#6' => [0, 1, true],
'#7' => [1, 0, false],
'#8' => [1, 1, true],
'#9' => ["false", null, true],
'#10' => ["true", null, false],
'#11' => [null, "false", false],
'#12' => [null, "true", true],
'#13' => ["false", "false", false],
'#14' => ["false", "true", true],
'#15' => ["true", "false", false],
'#16' => ["true", "true", true],
'#17' => [false, null, true],
'#18' => [true, null, false],
'#19' => [null, false, false],
'#20' => [null, true, true],
'#21' => [false, false, false],
'#22' => [false, true, true],
'#23' => [true, false, false],
'#24' => [true, true, true],
'#25' => ['fals', null, true],
'#26' => ['tru', null, true],
'#27' => [null, 'fals', false],
'#28' => [null, 'tru', false],
'#29' => ['fals', 'fals', false],
'#30' => ['fals', 'tru', false],
'#31' => ['tru', 'fals', false],
'#32' => ['tru', 'tru', false],
'#33' => ['false', 'fals', false],
'#34' => ['false', 'tru', false],
'#35' => ['true', 'fals', false],
'#36' => ['true', 'tru', false],
'#37' => ['fals', 'false', false],
'#38' => ['fals', 'true', true],
'#39' => ['tru', 'false', false],
'#40' => ['tru', 'true', true],
];
}
}

0 comments on commit b902ea8

Please sign in to comment.