ItemLoggerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Stash\Test;
  11. use Stash\Test\Stubs\LoggerStub;
  12. use Stash\Test\Stubs\DriverExceptionStub;
  13. use Stash\Test\Stubs\PoolGetDriverStub;
  14. use Stash\Item;
  15. use Stash\Driver\Ephemeral as Ephemeral;
  16. /**
  17. * @package Stash
  18. * @author Robert Hafner <tedivm@tedivm.com>
  19. */
  20. class ItemLoggerTest extends \PHPUnit_Framework_TestCase
  21. {
  22. protected function getItem($key, $exceptionDriver = false)
  23. {
  24. if ($exceptionDriver) {
  25. $fullDriver = 'Stash\Test\Stubs\DriverExceptionStub';
  26. } else {
  27. $fullDriver = 'Stash\Driver\Ephemeral';
  28. }
  29. $item = new Item();
  30. $poolStub = new PoolGetDriverStub();
  31. $poolStub->setDriver(new $fullDriver());
  32. $item->setPool($poolStub);
  33. $item->setKey($key);
  34. return $item;
  35. }
  36. public function testSetLogger()
  37. {
  38. $item = $this->getItem(array('path', 'to', 'constructor'));
  39. $logger = new LoggerStub();
  40. $item->setLogger($logger);
  41. $this->assertAttributeInstanceOf('Stash\Test\Stubs\LoggerStub', 'logger', $item, 'setLogger injects logger into Item.');
  42. }
  43. public function testGet()
  44. {
  45. $logger = new LoggerStub();
  46. $item = $this->getItem(array('path', 'to', 'get'), true);
  47. $item->setLogger($logger);
  48. // triggerlogging
  49. $item->get('test_key');
  50. $this->assertInstanceOf('Stash\Test\Exception\TestException',
  51. $logger->lastContext['exception'], 'Logger was passed exception in event context.');
  52. $this->assertTrue(strlen($logger->lastMessage) > 0, 'Logger message set after "get" exception.');
  53. $this->assertEquals('critical', $logger->lastLevel, 'Exceptions logged as critical.');
  54. }
  55. public function testSet()
  56. {
  57. $logger = new LoggerStub();
  58. $item = $this->getItem(array('path', 'to', 'set'), true);
  59. $item->setLogger($logger);
  60. // triggerlogging
  61. $item->set('test_key')->save();
  62. $this->assertInstanceOf('Stash\Test\Exception\TestException',
  63. $logger->lastContext['exception'], 'Logger was passed exception in event context.');
  64. $this->assertTrue(strlen($logger->lastMessage) > 0, 'Logger message set after "set" exception.');
  65. $this->assertEquals('critical', $logger->lastLevel, 'Exceptions logged as critical.');
  66. }
  67. public function testClear()
  68. {
  69. $logger = new LoggerStub();
  70. $item = $this->getItem(array('path', 'to', 'clear'), true);
  71. $item->setLogger($logger);
  72. // triggerlogging
  73. $item->clear();
  74. $this->assertInstanceOf('Stash\Test\Exception\TestException',
  75. $logger->lastContext['exception'], 'Logger was passed exception in event context.');
  76. $this->assertTrue(strlen($logger->lastMessage) > 0, 'Logger message set after "clear" exception.');
  77. $this->assertEquals('critical', $logger->lastLevel, 'Exceptions logged as critical.');
  78. }
  79. }