beingsane
5/13/2014 - 3:01 PM

test.php

use Psr\Log;

class TheTest extends \PHPUnit_Framework_TestCase
{
  /**
	 * The class we are testing.
	 */
	protected $instance;

  /**
   * Track the logger calls.
	 */
  protected $logs;

  /**
	 * Mocks the log method to track if logging is working.
	 *
	 * @param   Log\LogLevel  $level    The level.
	 * @param   string        $message  The message.
	 * @param   array         $context  The context.
	 *
	 * @return  void
	 */
	public function mockLog($level, $message, $context)
	{
		$this->logs[] = array(
			'level' => $level,
			'message' => $message,
			'context' => $context,
		);
	}

  /**
	 * Tests the log method.
	 *
	 * @return  void
	 */
	public function testLog()
	{
		$this->logs = array();

		$mockLogger = $this->getMock('Psr\Log\AbstractLogger', array('log'), array(), '', false);
		$mockLogger->expects($this->any())
			->method('log')
			->will($this->returnCallback(array($this, 'mockLog')));

		$this->instance->log(Log\LogLevel::DEBUG, 'Debug', array('sql' => true));

		$this->assertEmpty($this->logs, 'Logger not set up yet.');

		// Set the logger and try again.

		$this->instance->setLogger($mockLogger);

		$this->instance->log(Log\LogLevel::DEBUG, 'Debug', array('sql' => true));

		$this->assertEquals(Log\LogLevel::DEBUG, $this->logs[0]['level']);
		$this->assertEquals('Debug', $this->logs[0]['message']);
		$this->assertEquals(array('sql' => true), $this->logs[0]['context']);
	}

  /**
	 * Sets up the fixture.
   *
	 * This method is called before a test is executed.
	 *
	 * @return void
	 */
	protected function setUp()
	{
		$this->instance = new TheClass;
	}
}