AdapterExceptionTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\Exception;
  6. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  7. use Temando\Shipping\Rest\Response\DataObject\Error;
  8. use Temando\Shipping\Rest\Response\Document\Errors;
  9. /**
  10. * Temando Adapter Exception Component Test
  11. *
  12. * @package Temando\Shipping\Test\Unit
  13. * @author Nathan Wilson <nathan.wilson@temando.com>
  14. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link https://www.temando.com/
  16. */
  17. class AdapterExceptionTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var ObjectManager
  21. */
  22. private $objectManager;
  23. /**
  24. * Test setup
  25. */
  26. protected function setUp()
  27. {
  28. $this->objectManager = new ObjectManager($this);
  29. parent::setUp();
  30. }
  31. /**
  32. * Provide different input with expectations.
  33. *
  34. * @return mixed[]
  35. */
  36. public function exceptionMessageDataProvider()
  37. {
  38. $status = '400';
  39. $title = 'General error';
  40. $code = 'general-error';
  41. $detail = 'Detailed error';
  42. $errorBoth = new Error();
  43. $errorBoth->setStatus($status);
  44. $errorBoth->setTitle($title);
  45. $errorBoth->setCode($code);
  46. $errorBoth->setDetail($detail);
  47. $errorNoDetail = new Error();
  48. $errorNoDetail->setStatus($status);
  49. $errorNoDetail->setTitle($title);
  50. $errorNoDetail->setCode($code);
  51. $errorNoTitle = new Error();
  52. $errorNoTitle->setStatus($status);
  53. $errorNoTitle->setCode($code);
  54. $errorNoTitle->setDetail($detail);
  55. return [
  56. 'both' => [
  57. $errorBoth,
  58. "$code: $detail"
  59. ],
  60. 'no-detail' => [
  61. $errorNoDetail,
  62. "$code: $title"
  63. ],
  64. 'no-title' => [
  65. $errorNoTitle,
  66. "$code: $detail"
  67. ]
  68. ];
  69. }
  70. /**
  71. * Assert exception message contains error details.
  72. *
  73. * @test
  74. * @dataProvider exceptionMessageDataProvider
  75. *
  76. * @param Error $error
  77. * @param string $expected
  78. */
  79. public function extractMessageFromException($error, $expected)
  80. {
  81. /** @var Errors $errors */
  82. $errors = $this->objectManager->getObject(Errors::class);
  83. $errors->setErrors([$error]);
  84. $exception = AdapterException::errorResponse($errors);
  85. self::assertSame($expected, $exception->getMessage());
  86. }
  87. }