diff --git a/src/Apps/OxidLoggerTrait.php b/src/Apps/OxidLoggerTrait.php index 4d4a7bd..ed5b11d 100644 --- a/src/Apps/OxidLoggerTrait.php +++ b/src/Apps/OxidLoggerTrait.php @@ -33,10 +33,10 @@ trait OxidLoggerTrait public function getOxidLogPath(string $fileName): string { - if (!defined(OX_BASE_PATH)) { + if (!class_exists(Registry::class)) { throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); } - return OX_BASE_PATH . 'log' . DIRECTORY_SEPARATOR . $fileName; + return OX_BASE_PATH . '/log' . DIRECTORY_SEPARATOR . $fileName; } } diff --git a/tests/Apps/OxidLoggerTestTrait.php b/tests/Apps/OxidLoggerTestTrait.php new file mode 100644 index 0000000..29e58d9 --- /dev/null +++ b/tests/Apps/OxidLoggerTestTrait.php @@ -0,0 +1,108 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\GuzzleFactory\tests\Apps; + +use D3\GuzzleFactory\GuzzleFactory; +use Monolog\Logger; +use ReflectionException; +use RuntimeException; + +trait OxidLoggerTestTrait +{ + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::addOxidLogger + */ + public function testAddOxidLoggerWithoutOxid(): void + { + $sut = GuzzleFactory::create(); + + $this->expectException(RuntimeException::class); + + $this->callMethod( + $sut, + 'addOxidLogger', + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getOxidLogPath + */ + public function testGetOxidLogPathWithoutOxid(): void + { + $sut = GuzzleFactory::create(); + + $this->expectException(RuntimeException::class); + + $this->assertSame( + 'foo', + $this->callMethod( + $sut, + 'getOxidLogPath', + ['fixture.log'] + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::addOxidLogger + */ + public function testAddOxidLoggerInOxid(): void + { + require_once __DIR__.'/../Helpers/classAliases.php'; + + $sut = GuzzleFactory::create(); + + $this->callMethod( + $sut, + 'addOxidLogger', + ); + + $loggers = $this->getValue($sut, 'loggers'); + $this->assertArrayHasKey('oxid', $loggers); + $this->assertInstanceOf(Logger::class, $loggers['oxid']); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getOxidLogPath + */ + public function testGetOxidLogPathInOxid(): void + { + require_once __DIR__.'/../Helpers/classAliases.php'; + + $sut = GuzzleFactory::create(); + + $this->assertStringEndsWith( + 'tests/Helpers/log/fixture.log', + $this->callMethod( + $sut, + 'getOxidLogPath', + ['fixture.log'] + ) + ); + } +} diff --git a/tests/GuzzleFactoryTest.php b/tests/GuzzleFactoryTest.php index 4fe715c..25a1ada 100644 --- a/tests/GuzzleFactoryTest.php +++ b/tests/GuzzleFactoryTest.php @@ -18,6 +18,7 @@ declare(strict_types=1); namespace D3\GuzzleFactory\tests; use D3\GuzzleFactory\GuzzleFactory; +use D3\GuzzleFactory\tests\Apps\OxidLoggerTestTrait; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Monolog\Logger; @@ -31,6 +32,7 @@ class GuzzleFactoryTest extends ApiTestCase use HeaderTestTrait; use LoggerTestTrait; use MessageFormatterTestTrait; + use OxidLoggerTestTrait; /** * @test