DynamoDbHandlerTest.php 2.2 KB

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