Skip to content

Commit

Permalink
[phalcon#13438] - Correction to the logger trait; Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niden authored and CameronHall committed Dec 8, 2018
1 parent 9ea4da1 commit f3670b5
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 76 deletions.
5 changes: 1 addition & 4 deletions tests/_data/fixtures/Traits/LoggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Phalcon\Logger;
use Phalcon\Logger\Adapter\File;
use Phalcon\Logger\Exception;
use Phalcon\Logger\Formatter\Line;
use UnitTester;
use function outputFolder;

Expand All @@ -34,9 +33,7 @@ protected function runLoggerFile(UnitTester $I, string $level)
{
$logPath = outputFolder('tests/logs/');
$fileName = $I->getNewFileName('log', 'log');

$adapter = new File($logPath . $fileName, ['mode' => 'ab']);
$adapter->setFormatter(new Line());
$adapter = new File($logPath . $fileName);

$logger = new Logger('my-logger', ['one' => $adapter]);
$logger->{$level}('Hello');
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/Logger/AddAdapterCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace Phalcon\Test\Unit\Logger;

use Phalcon\Logger;
use Phalcon\Logger\Adapter\File;
use function outputFolder;
use UnitTester;

/**
Expand All @@ -32,6 +35,34 @@ class AddAdapterCest
public function loggerAddAdapter(UnitTester $I)
{
$I->wantToTest('Logger - addAdapter()');
$I->skipTest('Need implementation');

$fileName1 = $I->getNewFileName('log', 'log');
$fileName2 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');
$adapter1 = new File($outputPath . $fileName1);
$adapter2 = new File($outputPath . $fileName2);

$logger = new Logger('my-logger', ['one' => $adapter1]);

$expected = 1;
$actual = $logger->getAdapters();
$I->assertCount($expected, $actual);

$logger->addAdapter('two', $adapter2);
$expected = 2;
$actual = $logger->getAdapters();
$I->assertCount($expected, $actual);

$logger->debug('Hello');

$I->amInPath($outputPath);
$I->openFile($fileName1);
$I->seeInThisFile('Hello');
$I->safeDeleteFile($fileName1);

$I->amInPath($outputPath);
$I->openFile($fileName2);
$I->seeInThisFile('Hello');
$I->safeDeleteFile($fileName2);
}
}
68 changes: 0 additions & 68 deletions tests/unit/Logger/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,72 +62,4 @@ public function loggerConstructConstants(UnitTester $I)
$I->assertEquals(4, Logger::WARNING);
$I->assertEquals(8, Logger::CUSTOM);
}

// /**
// * Tests Phalcon\Logger :: __construct()
// *
// * @param UnitTester $I
// *
// * @author Phalcon Team <team@phalconphp.com>
// * @since 2018-11-13
// */
// public function loggerConstruct(UnitTester $I)
// {
// $I->wantToTest('Logger - __construct()');
// $I->skipTest('Need implementation');
// }
//
// public function checkSetAdaptersInConstructor(\UnitTester $I)
// {
// $fileName1 = $I->getNewFileName('log', 'log');
// $fileName2 = $I->getNewFileName('log', 'log');
//
// $adapter1 = new File($this->logPath . $fileName1);
// $adapter2 = new File($this->logPath . $fileName2);
// $adapter1->setFormatter(new Line('%type%|%date%|%message%'));
// $adapter2->setFormatter(new Line('%type%|%date%|%message%'));
//
// $logger = new Logger(
// 'my-logger',
// [
// 'one' => $adapter1,
// 'two' => $adapter2,
// ]
// );
//
// $adapters = $logger->getAdapters();
// $I->assertEquals(2, count($adapters));
// $I->assertTrue($adapters['one'] instanceof File);
// $I->assertTrue($adapters['two'] instanceof File);
// unset($logger, $adapter1, $adapter2);
// }
//
// public function checkAddGetRemoveAdapter(\UnitTester $I)
// {
// $fileName1 = $I->getNewFileName('log', 'log');
// $fileName2 = $I->getNewFileName('log', 'log');
//
// $adapter1 = new File($this->logPath . $fileName1);
// $adapter2 = new File($this->logPath . $fileName2);
// $adapter1->setFormatter(new Line('%type%|%date%|%message%'));
// $adapter2->setFormatter(new Line('%type%|%date%|%message%'));
//
// $logger = new Logger('my-logger');
// $logger
// ->addAdapter('one', $adapter1)
// ->addAdapter('two', $adapter2);
//
// $adapters = $logger->getAdapters();
// $I->assertEquals(2, count($adapters));
// $I->assertTrue($adapters['one'] instanceof File);
// $I->assertTrue($adapters['two'] instanceof File);
//
// $logger->removeAdapter('one');
// $adapter = $logger->getAdapter('two');
// $I->assertTrue($adapter instanceof File);
//
// $adapters = $logger->getAdapters();
// $I->assertEquals(1, count($adapters));
// unset($logger, $adapter);
// }
}
82 changes: 82 additions & 0 deletions tests/unit/Logger/ExcludeAdaptersCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Logger;

use Phalcon\Logger;
use Phalcon\Logger\Adapter\File;
use UnitTester;

/**
* Class ExcludeAdaptersCest
*
* @package Phalcon\Test\Unit\Logger
*/
class ExcludeAdaptersCest
{
/**
* Tests Phalcon\Logger :: excludeAdapters()
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function loggerExcludeAdapters(UnitTester $I)
{
$I->wantToTest('Logger - excludeAdapters()');

$fileName1 = $I->getNewFileName('log', 'log');
$fileName2 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');
$adapter1 = new File($outputPath . $fileName1);
$adapter2 = new File($outputPath . $fileName2);

$logger = new Logger(
'my-logger',
[
'one' => $adapter1,
'two' => $adapter2,
]
);

/**
* Log into both
*/
$logger->debug('Hello');

$I->amInPath($outputPath);
$I->openFile($fileName1);
$I->seeInThisFile('Hello');

$I->amInPath($outputPath);
$I->openFile($fileName2);
$I->seeInThisFile('Hello');

/**
* Exclude a logger
*/
$logger
->excludeAdapters(['two'])
->debug('Goodbye');

$I->amInPath($outputPath);
$I->openFile($fileName1);
$I->seeInThisFile('Goodbye');
$I->safeDeleteFile($fileName1);

$I->amInPath($outputPath);
$I->openFile($fileName2);
$I->seeInThisFile('Goodbye');
$I->safeDeleteFile($fileName2);
}
}
2 changes: 1 addition & 1 deletion tests/unit/Logger/NoticeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class NoticeCest
public function loggerNotice(UnitTester $I)
{
$I->wantToTest('Logger - notice()');
$I->skipTest('Need implementation');
$this->runLoggerFile($I, 'notice');
}
}
70 changes: 69 additions & 1 deletion tests/unit/Logger/RemoveAdapterCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

namespace Phalcon\Test\Unit\Logger;

use Phalcon\Logger;
use Phalcon\Logger\Adapter\File;
use Phalcon\Logger\Exception;
use function print_r;
use UnitTester;

/**
Expand All @@ -32,6 +36,70 @@ class RemoveAdapterCest
public function loggerRemoveAdapter(UnitTester $I)
{
$I->wantToTest('Logger - removeAdapter()');
$I->skipTest('Need implementation');

$fileName1 = $I->getNewFileName('log', 'log');
$fileName2 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');
$adapter1 = new File($outputPath . $fileName1);
$adapter2 = new File($outputPath . $fileName2);

$logger = new Logger(
'my-logger',
[
'one' => $adapter1,
'two' => $adapter2,
]
);

$expected = 2;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$logger->removeAdapter('one');
$expected = 1;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$I->safeDeleteFile($outputPath . $fileName1);
$I->safeDeleteFile($outputPath . $fileName2);
}

/**
* Tests Phalcon\Logger :: removeAdapter() - unknown
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function loggerRemoveAdapterUnknown(UnitTester $I)
{
$I->wantToTest('Logger - removeAdapter() - unknown');

$fileName1 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');

try {
$adapter1 = new File($outputPath . $fileName1);

$logger = new Logger(
'my-logger',
[
'one' => $adapter1,
]
);

$expected = 1;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$logger->removeAdapter('unknown');
} catch (Exception $ex) {
$expected = 'Adapter does not exist for this logger';
$actual = $ex->getMessage();
$I->assertEquals($expected, $actual);
}

$I->safeDeleteFile($outputPath . $fileName1);
}
}
69 changes: 68 additions & 1 deletion tests/unit/Logger/SetAdaptersCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Phalcon\Test\Unit\Logger;

use Phalcon\Logger;
use Phalcon\Logger\Adapter\File;
use UnitTester;

/**
Expand All @@ -32,6 +34,71 @@ class SetAdaptersCest
public function loggerSetAdapters(UnitTester $I)
{
$I->wantToTest('Logger - setAdapters()');
$I->skipTest('Need implementation');
$fileName1 = $I->getNewFileName('log', 'log');
$fileName2 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');
$adapter1 = new File($outputPath . $fileName1);
$adapter2 = new File($outputPath . $fileName2);

$logger = new Logger('my-logger');

$expected = 0;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$logger->setAdapters(
[
'one' => $adapter1,
'two' => $adapter2,
]
);

$expected = 2;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$class = File::class;
$I->assertInstanceOf($class, $adapters['one']);
$I->assertInstanceOf($class, $adapters['two']);

$I->safeDeleteFile($outputPath . $fileName1);
$I->safeDeleteFile($outputPath . $fileName2);
}

/**
* Tests Phalcon\Logger :: setAdapters() - constructor
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function loggerSetAdaptersConstructor(UnitTester $I)
{
$I->wantToTest('Logger :: setAdapters() - constructor');
$fileName1 = $I->getNewFileName('log', 'log');
$fileName2 = $I->getNewFileName('log', 'log');
$outputPath = outputFolder('tests/logs/');
$adapter1 = new File($outputPath . $fileName1);
$adapter2 = new File($outputPath . $fileName2);

$logger = new Logger(
'my-logger',
[
'one' => $adapter1,
'two' => $adapter2,
]
);

$expected = 2;
$adapters = $logger->getAdapters();
$I->assertCount($expected, $adapters);

$class = File::class;
$I->assertInstanceOf($class, $adapters['one']);
$I->assertInstanceOf($class, $adapters['two']);

$I->safeDeleteFile($outputPath . $fileName1);
$I->safeDeleteFile($outputPath . $fileName2);
}
}

0 comments on commit f3670b5

Please sign in to comment.