DynamoDbHandlerTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. private $isV3;
  16. public function setUp(): void
  17. {
  18. if (!class_exists('Aws\DynamoDb\DynamoDbClient')) {
  19. $this->markTestSkipped('aws/aws-sdk-php not installed');
  20. }
  21. $this->isV3 = defined('Aws\Sdk::VERSION') && version_compare(\Aws\Sdk::VERSION, '3.0', '>=');
  22. $implementedMethods = ['__call'];
  23. $absentMethods = [];
  24. if (method_exists('Aws\DynamoDb\DynamoDbClient', 'formatAttributes')) {
  25. $implementedMethods[] = 'formatAttributes';
  26. } else {
  27. $absentMethods[] = 'formatAttributes';
  28. }
  29. $clientMockBuilder = $this->getMockBuilder('Aws\DynamoDb\DynamoDbClient')
  30. ->onlyMethods($implementedMethods)
  31. ->disableOriginalConstructor();
  32. if ($absentMethods) {
  33. $clientMockBuilder->addMethods($absentMethods);
  34. }
  35. $this->client = $clientMockBuilder->getMock();
  36. }
  37. public function testConstruct()
  38. {
  39. $this->assertInstanceOf('Monolog\Handler\DynamoDbHandler', new DynamoDbHandler($this->client, 'foo'));
  40. }
  41. public function testInterface()
  42. {
  43. $this->assertInstanceOf('Monolog\Handler\HandlerInterface', new DynamoDbHandler($this->client, 'foo'));
  44. }
  45. public function testGetFormatter()
  46. {
  47. $handler = new DynamoDbHandler($this->client, 'foo');
  48. $this->assertInstanceOf('Monolog\Formatter\ScalarFormatter', $handler->getFormatter());
  49. }
  50. public function testHandle()
  51. {
  52. $record = $this->getRecord();
  53. $formatter = $this->createMock('Monolog\Formatter\FormatterInterface');
  54. $formatted = ['foo' => 1, 'bar' => 2];
  55. $handler = new DynamoDbHandler($this->client, 'foo');
  56. $handler->setFormatter($formatter);
  57. if ($this->isV3) {
  58. $expFormatted = ['foo' => ['N' => 1], 'bar' => ['N' => 2]];
  59. } else {
  60. $expFormatted = $formatted;
  61. }
  62. $formatter
  63. ->expects($this->once())
  64. ->method('format')
  65. ->with($record)
  66. ->will($this->returnValue($formatted));
  67. $this->client
  68. ->expects($this->isV3 ? $this->never() : $this->once())
  69. ->method('formatAttributes')
  70. ->with($this->isType('array'))
  71. ->will($this->returnValue($formatted));
  72. $this->client
  73. ->expects($this->once())
  74. ->method('__call')
  75. ->with('putItem', [[
  76. 'TableName' => 'foo',
  77. 'Item' => $expFormatted,
  78. ]]);
  79. $handler->handle($record);
  80. }
  81. }