DynamoDbHandlerTest.php 2.2 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 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. $clientMockBuilder = $this->getMockBuilder(DynamoDbClient::class)
  26. ->onlyMethods($implementedMethods)
  27. ->disableOriginalConstructor();
  28. $this->client = $clientMockBuilder->getMock();
  29. }
  30. public function tearDown(): void
  31. {
  32. parent::tearDown();
  33. unset($this->client);
  34. }
  35. public function testGetFormatter()
  36. {
  37. $handler = new DynamoDbHandler($this->client, 'foo');
  38. $this->assertInstanceOf('Monolog\Formatter\ScalarFormatter', $handler->getFormatter());
  39. }
  40. public function testHandle()
  41. {
  42. $record = $this->getRecord();
  43. $formatter = $this->createMock('Monolog\Formatter\FormatterInterface');
  44. $formatted = ['foo' => 1, 'bar' => 2];
  45. $handler = new DynamoDbHandler($this->client, 'foo');
  46. $handler->setFormatter($formatter);
  47. if ($this->isV3) {
  48. $expFormatted = ['foo' => ['N' => 1], 'bar' => ['N' => 2]];
  49. } else {
  50. $expFormatted = $formatted;
  51. }
  52. $formatter
  53. ->expects($this->once())
  54. ->method('format')
  55. ->with($record)
  56. ->willReturn($formatted);
  57. $this->client
  58. ->expects($this->once())
  59. ->method('__call')
  60. ->with('putItem', [[
  61. 'TableName' => 'foo',
  62. 'Item' => $expFormatted,
  63. ]]);
  64. $handler->handle($record);
  65. }
  66. }