Skip to content

Commit

Permalink
QA: Use new unittest coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Dec 28, 2013
1 parent 4163319 commit a324c7a
Showing 1 changed file with 44 additions and 140 deletions.
184 changes: 44 additions & 140 deletions src/test/php/net/xp_framework/unittest/logging/LogCategoryTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class LogCategoryTest extends TestCase {
/**
* Setup method. Creates logger and cat member for easier access to
* the Logger instance
*
*/
public function setUp() {
$this->cat= new \util\log\LogCategory('test');
Expand Down Expand Up @@ -45,8 +44,8 @@ public function append(LoggingEvent $event) {
/**
* Helper method
*
* @param string method
* @param mixed[] args default ["Argument"]
* @param string $method
* @param var $func A function to be called with `$this->cat` as argument
* @throws unittest.AssertionFailedError
*/
protected function assertLogged($result, $func) {
Expand All @@ -55,74 +54,50 @@ protected function assertLogged($result, $func) {
$this->assertEquals($result, $app->messages);
}

/**
* Ensure the logger category initially has no appenders
*
*/
#[@test]
public function initiallyNoAppenders() {
public function logger_category_initially_has_no_appenders() {
$this->assertFalse($this->cat->hasAppenders());
}

/**
* Tests adding an appender returns the added appender
*
*/
#[@test]
public function addAppenderReturnsAddedAppender() {
public function addAappender_returns_added_appender() {
$appender= $this->mockAppender();
$this->assertEquals($appender, $this->cat->addAppender($appender));
}

/**
* Tests adding an appender returns the log category
*
*/
#[@test]
public function withAppenderReturnsCategory() {
public function withAppender_returns_category() {
$this->assertEquals($this->cat, $this->cat->withAppender($this->mockAppender()));
}

/**
* Tests hasAppenders() and addAppender() methods
*
*/
#[@test]
public function hasAppendersAfterAdding() {
public function hasAppenders_initially_returns_false() {
$this->assertFalse($this->cat->hasAppenders());
}

#[@test]
public function hasAppenders_returns_true_after_adding_an_appender() {
$this->cat->addAppender($this->mockAppender());
$this->assertTrue($this->cat->hasAppenders());
}

/**
* Tests hasAppenders() and removeAppender() methods
*
*/
#[@test]
public function hasNoMoreAppendersAfterRemoving() {
public function hasAppenders_returns_false_after_removing_added_appender() {
$a= $this->cat->addAppender($this->mockAppender());
$this->cat->removeAppender($a);
$this->assertFalse($this->cat->hasAppenders());
}

/**
* Tests addAppender() method
*
*/
#[@test]
public function addAppenderTwice() {
public function adding_appender_twice_with_same_flags_has_no_effect() {
$a= $this->mockAppender();
$this->cat->addAppender($a);
$this->cat->addAppender($a);
$this->cat->removeAppender($a);
$this->assertFalse($this->cat->hasAppenders());
$this->assertEquals(array($a), $this->cat->getAppenders());
}

/**
* Tests addAppender() and removeAppender() methods
*
*/
#[@test]
public function addAppenderTwiceWithDifferentFlags() {
public function adding_appender_twice_with_differing_flags() {
$a= $this->mockAppender();
$this->cat->addAppender($a, \util\log\LogLevel::INFO);
$this->cat->addAppender($a, \util\log\LogLevel::WARN);
Expand All @@ -132,100 +107,66 @@ public function addAppenderTwiceWithDifferentFlags() {
$this->assertFalse($this->cat->hasAppenders());
}

/**
* Tests adding an appender sets default layout if appender does not
* have a layout.
*
*/
#[@test]
public function addAppenderSetsDefaultLayout() {
public function addAppender_sets_layout_if_appender_does_not_have_layout() {
$appender= newinstance('util.log.Appender', array(), '{
public function append(LoggingEvent $event) { }
}');
$this->cat->addAppender($appender);
$this->assertClass($appender->getLayout(), 'util.log.layout.DefaultLayout');
$this->assertInstanceOf('util.log.layout.DefaultLayout', $appender->getLayout());
}

/**
* Tests adding an appender does not overwrite layout
*
*/
#[@test]
public function addAppenderDoesNotOverwriteLayout() {
public function addAppender_does_not_overwrite_layout() {
$appender= newinstance('util.log.Appender', array(), '{
public function append(LoggingEvent $event) { }
}');
$this->cat->addAppender($appender->withLayout(new PatternLayout('%m')));
$this->assertClass($appender->getLayout(), 'util.log.layout.PatternLayout');
$this->assertInstanceOf('util.log.layout.PatternLayout', $appender->getLayout());
}

/**
* Tests adding an appender sets default layout if appender does not
* have a layout.
*
*/
#[@test]
public function withAppenderSetsLayout() {
public function withAppender_sets_layout_if_appender_does_not_have_layout() {
$appender= newinstance('util.log.Appender', array(), '{
public function append(LoggingEvent $event) { }
}');
$this->cat->withAppender($appender);
$this->assertClass($appender->getLayout(), 'util.log.layout.DefaultLayout');
}

/**
* Tests adding an appender does not overwrite layout
*
*/
#[@test]
public function withAppenderDoesNotOverwriteLayout() {
public function withAppender_does_not_overwrite_layout() {
$appender= newinstance('util.log.Appender', array(), '{
public function append(LoggingEvent $event) { }
}');
$this->cat->withAppender($appender->withLayout(new PatternLayout('%m')));
$this->assertClass($appender->getLayout(), 'util.log.layout.PatternLayout');
$this->assertInstanceOf('util.log.layout.PatternLayout', $appender->getLayout());
}

/**
* Tests equals() method
*
*/
#[@test]
public function logCategoriesWithSameIdentifierAreEqual() {
public function log_categories_with_same_identifiers_are_equal() {
$this->assertEquals(new \util\log\LogCategory('test'), $this->cat);
}

/**
* Tests equals() method
*
*/
#[@test]
public function logCategoriesDifferingAppendersNotEqual() {
public function log_categories_with_differing_appenders_are_not_equal() {
$this->assertNotEquals(
new \util\log\LogCategory('test'),
$this->cat->withAppender($this->mockAppender())
);
}

/**
* Tests equals() method
*
*/
#[@test]
public function logCategoriesAppendersDifferingInFlagsNotEqual() {
public function log_categories_with_appenders_differing_in_flags_are_not_equal() {
$appender= $this->mockAppender();
$this->assertNotEquals(
create(new \util\log\LogCategory('test'))->withAppender($appender, \util\log\LogLevel::WARN),
$this->cat->withAppender($appender)
);
}

/**
* Tests equals() method
*
*/
#[@test]
public function logCategoriesSameAppendersEqual() {
public function log_categories_with_same_appenders_are_equal() {
$appender= $this->mockAppender();
$this->assertEquals(
create(new \util\log\LogCategory('test'))->withAppender($appender),
Expand Down Expand Up @@ -313,110 +254,73 @@ function($cat) { $cat->log(LogLevel::INFO, array('Test', '123')); }
);
}

/**
* Tests flags
*
*/
#[@test]
public function warningMessageOnlyGetsAppendedToWarnAppender() {
public function warning_message_only_gets_appended_to_warn_appender() {
$app1= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::INFO);
$app2= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::WARN);
$this->cat->warn('Test');
$this->assertEquals(array(), $app1->messages);
$this->assertEquals(array(array('warn', 'Test')), $app2->messages);
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getAppenders() {
public function getAppenders_initially_returns_empty_array() {
$this->assertEquals(array(), $this->cat->getAppenders());
}

#[@test]
public function getAppenders_returns_added_appender() {
$appender= $this->mockAppender();
$this->cat->addAppender($appender);
$this->assertEquals(array($appender), $this->cat->getAppenders());
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getAppendersWithoutAppendersAdded() {
$this->assertEquals(array(), $this->cat->getAppenders());
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getAllAppendersWhenErrorLevelAppenderExists() {
public function getAppenders_returns_added_appender_with_error_flags() {
$appender= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::ERROR);
$this->assertEquals(array($appender), $this->cat->getAppenders());
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getErrorLevelAppendersWhenErrorLevelAppendersExist() {
public function getAppenders_with_error_flags_returns_added_appender_with_error_flags() {
$appender= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::ERROR);
$this->assertEquals(array($appender), $this->cat->getAppenders(\util\log\LogLevel::ERROR));
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getInfoLevelAppendersWhenOnlyErrorLevelAppendersExist() {
public function getAppenders_with_info_flags_does_not_return_added_appender_with_error_flags() {
$appender= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::ERROR);
$this->assertEquals(array(), $this->cat->getAppenders(\util\log\LogLevel::INFO));
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getInfoLevelAppendersWhenErrorAndInfoLevelAppenderExists() {
public function getAppenders_with_info_flags_returns_added_appender_with_info_and_error_flags() {
$appender= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::ERROR | \util\log\LogLevel::INFO);
$this->assertEquals(array($appender), $this->cat->getAppenders(\util\log\LogLevel::INFO));
}

/**
* Tests getAppenders()
*
*/
#[@test]
public function getAllAppenders() {
public function getAppenders_returns_appenders_with_flags() {
$app1= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::ERROR);
$app2= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::WARN);
$app3= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::INFO);
$app4= $this->cat->addAppender($this->mockAppender(), \util\log\LogLevel::DEBUG);
$this->assertEquals(array($app1, $app2, $app3, $app4), $this->cat->getAppenders());
}


/**
* Tests LogCategory::hasContext()
*
*/
#[@test]
public function hasContext() {
public function hasContext_initially_returns_false() {
$this->assertFalse($this->cat->hasContext());
}

#[@test]
public function hasContext_returns_true_after_setting_context() {
$this->cat->setContext(new NestedLogContext());
$this->assertTrue($this->cat->hasContext());
}

/**
* Tests LogCategory::getContext()
*
*/
#[@test]
public function getContext() {
public function getContext_returns_context_previously_set_with_setContext() {
$context= new NestedLogContext();
$this->cat->setContext($context);
$this->assertEquals($context, $this->cat->getContext());
Expand Down

0 comments on commit a324c7a

Please sign in to comment.