DynamoDbHandlerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 testGetFormatter()
  40. {
  41. $handler = new DynamoDbHandler($this->client, 'foo');
  42. $this->assertInstanceOf('Monolog\Formatter\ScalarFormatter', $handler->getFormatter());
  43. }
  44. public function testHandle()
  45. {
  46. $record = $this->getRecord();
  47. $formatter = $this->createMock('Monolog\Formatter\FormatterInterface');
  48. $formatted = ['foo' => 1, 'bar' => 2];
  49. $handler = new DynamoDbHandler($this->client, 'foo');
  50. $handler->setFormatter($formatter);
  51. if ($this->isV3) {
  52. $expFormatted = ['foo' => ['N' => 1], 'bar' => ['N' => 2]];
  53. } else {
  54. $expFormatted = $formatted;
  55. }
  56. $formatter
  57. ->expects($this->once())
  58. ->method('format')
  59. ->with($record)
  60. ->will($this->returnValue($formatted));
  61. $this->client
  62. ->expects($this->isV3 ? $this->never() : $this->once())
  63. ->method('formatAttributes')
  64. ->with($this->isType('array'))
  65. ->will($this->returnValue($formatted));
  66. $this->client
  67. ->expects($this->once())
  68. ->method('__call')
  69. ->with('putItem', [[
  70. 'TableName' => 'foo',
  71. 'Item' => $expFormatted,
  72. ]]);
  73. $handler->handle($record);
  74. }
  75. }