DynamoDbHandlerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\Test\TestCase;
  12. class DynamoDbHandlerTest extends TestCase
  13. {
  14. private $client;
  15. public function setUp(): void
  16. {
  17. if (!class_exists('Aws\DynamoDb\DynamoDbClient')) {
  18. $this->markTestSkipped('aws/aws-sdk-php not installed');
  19. }
  20. $this->client = $this->getMockBuilder('Aws\DynamoDb\DynamoDbClient')
  21. ->onlyMethods(['formatAttributes', '__call'])
  22. ->disableOriginalConstructor()
  23. ->getMock();
  24. }
  25. public function testConstruct()
  26. {
  27. $this->assertInstanceOf('Monolog\Handler\DynamoDbHandler', new DynamoDbHandler($this->client, 'foo'));
  28. }
  29. public function testInterface()
  30. {
  31. $this->assertInstanceOf('Monolog\Handler\HandlerInterface', new DynamoDbHandler($this->client, 'foo'));
  32. }
  33. public function testGetFormatter()
  34. {
  35. $handler = new DynamoDbHandler($this->client, 'foo');
  36. $this->assertInstanceOf('Monolog\Formatter\ScalarFormatter', $handler->getFormatter());
  37. }
  38. public function testHandle()
  39. {
  40. $record = $this->getRecord();
  41. $formatter = $this->createMock('Monolog\Formatter\FormatterInterface');
  42. $formatted = ['foo' => 1, 'bar' => 2];
  43. $handler = new DynamoDbHandler($this->client, 'foo');
  44. $handler->setFormatter($formatter);
  45. $isV3 = defined('Aws\Sdk::VERSION') && version_compare(\Aws\Sdk::VERSION, '3.0', '>=');
  46. if ($isV3) {
  47. $expFormatted = array('foo' => array('N' => 1), 'bar' => array('N' => 2));
  48. } else {
  49. $expFormatted = $formatted;
  50. }
  51. $formatter
  52. ->expects($this->once())
  53. ->method('format')
  54. ->with($record)
  55. ->will($this->returnValue($formatted));
  56. $this->client
  57. ->expects($isV3 ? $this->never() : $this->once())
  58. ->method('formatAttributes')
  59. ->with($this->isType('array'))
  60. ->will($this->returnValue($formatted));
  61. $this->client
  62. ->expects($this->once())
  63. ->method('__call')
  64. ->with('putItem', [[
  65. 'TableName' => 'foo',
  66. 'Item' => $expFormatted,
  67. ]]);
  68. $handler->handle($record);
  69. }
  70. }