Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/Optimizely/Logger/DefaultLogger.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, Optimizely
* Copyright 2016,2018 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,11 +36,12 @@ class DefaultLogger implements LoggerInterface
* DefaultLogger constructor.
*
* @param int $minLevel Minimum level of messages to be logged.
* @param string $stream The PHP stream to log output.
*/
public function __construct($minLevel = Logger::INFO)
public function __construct($minLevel = Logger::INFO, $stream = "stdout")
{
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message%\n");
$streamHandler = new StreamHandler('php://output', $minLevel);
$streamHandler = new StreamHandler("php://{$stream}", $minLevel);
$streamHandler->setFormatter($formatter);
$this->logger = new Logger('Optimizely');
$this->logger->pushHandler($streamHandler);
Expand Down
6 changes: 4 additions & 2 deletions src/Optimizely/Optimizely.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2018, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,7 +114,9 @@ public function __construct(

if (!$this->validateDatafile($datafile, $skipJsonValidation)) {
$this->_isValid = false;
$this->_logger = new DefaultLogger();
$defaultLogger = new DefaultLogger();

$defaultLogger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
$this->_logger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
return;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/LoggerTests/DefaultLoggerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, Optimizely
* Copyright 2016,2018 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@ class DefaultLoggerTest extends \PHPUnit_Framework_TestCase
{
public function testDefaultLogger()
{
$logger = new DefaultLogger();
$logger = new DefaultLogger(Logger::INFO, 'output');
$logger->log(Logger::INFO, 'Log me please.');

$this->expectOutputRegex('/Log me please./');
Expand Down
16 changes: 9 additions & 7 deletions tests/OptimizelyTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017, Optimizely
* Copyright 2016-2018, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,8 @@

class OptimizelyTest extends \PHPUnit_Framework_TestCase
{
const OUTPUT_STREAM = 'output';

private $datafile;
private $eventBuilderMock;
private $loggerMock;
Expand Down Expand Up @@ -138,7 +140,7 @@ public function testValidateDatafileInvalidFileJsonValidationNotSkipped()

$this->assertFalse(
$validateInputsMethod->invoke(
new Optimizely('Random datafile'),
new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM)),
'Random datafile',
false
)
Expand All @@ -164,7 +166,7 @@ public function testValidateDatafileInvalidFileJsonValidationSkipped()
public function testActivateInvalidOptimizelyObject()
{
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array('Random datafile', null, $this->loggerMock))
->setConstructorArgs(array('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM)))
->setMethods(array('sendImpressionEvent'))
->getMock();

Expand Down Expand Up @@ -452,7 +454,7 @@ public function testActivateExperimentNotRunning()

public function testGetVariationInvalidOptimizelyObject()
{
$optlyObject = new Optimizely('Random datafile');
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));
$optlyObject->getVariation('some_experiment', 'some_user');
$this->expectOutputRegex('/Datafile has invalid format. Failing "getVariation"./');
}
Expand Down Expand Up @@ -665,7 +667,7 @@ public function testValidatePreconditionsUserNotInForcedVariationInExperiment()

public function testTrackInvalidOptimizelyObject()
{
$optlyObject = new Optimizely('Random datafile');
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));

// Verify that sendNotifications isn't called
$this->notificationCenterMock->expects($this->never())
Expand Down Expand Up @@ -2157,7 +2159,7 @@ public function testGetVariationBucketingIdAttribute()

public function testIsFeatureEnabledGivenInvalidDataFile()
{
$optlyObject = new Optimizely('Random datafile', null, $this->loggerMock);
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));

$this->expectOutputRegex("/Datafile has invalid format. Failing 'isFeatureEnabled'./");
$optlyObject->isFeatureEnabled("boolean_feature", "user_id");
Expand Down Expand Up @@ -2365,7 +2367,7 @@ public function testIsFeatureEnabledGivenFeatureFlagIsEnabledAndUserIsNotBeingEx

public function testGetEnabledFeaturesGivenInvalidDataFile()
{
$optlyObject = new Optimizely('Random datafile');
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));

$this->expectOutputRegex("/Datafile has invalid format. Failing 'getEnabledFeatures'./");

Expand Down