DynamoDbHandlerTest.php 2.8 KB

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