DynamoDbHandlerTest.php 3.0 KB

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