ExchangeRatesTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. use Webkul\Core\Models\Currency;
  3. use Webkul\Core\Models\CurrencyExchangeRate;
  4. use function Pest\Laravel\deleteJson;
  5. use function Pest\Laravel\get;
  6. use function Pest\Laravel\postJson;
  7. use function Pest\Laravel\putJson;
  8. it('should returns the exchange rate index page', function () {
  9. // Act and Assert.
  10. $this->loginAsAdmin();
  11. get(route('admin.settings.exchange_rates.index'))
  12. ->assertOk()
  13. ->assertSeeText(trans('admin::app.settings.exchange-rates.index.title'))
  14. ->assertSeeText(trans('admin::app.settings.exchange-rates.index.create-btn'))
  15. ->assertSeeText(trans('admin::app.settings.exchange-rates.index.update-rates'));
  16. });
  17. it('should fail the validation with errors when certain field not provided when store the exchange rates', function () {
  18. // Act and Assert.
  19. $this->loginAsAdmin();
  20. postJson(route('admin.settings.exchange_rates.store'))
  21. ->assertJsonValidationErrorFor('target_currency')
  22. ->assertJsonValidationErrorFor('rate')
  23. ->assertUnprocessable();
  24. });
  25. it('should store the newly created exchange rates', function () {
  26. // Arrange.
  27. $currency = Currency::factory()->create([
  28. 'code' => 'EUR',
  29. 'name' => 'Euro',
  30. ]);
  31. // Act and Assert.
  32. $this->loginAsAdmin();
  33. postJson(route('admin.settings.exchange_rates.store'), [
  34. 'rate' => $rate = rand(1, 100),
  35. 'target_currency' => $currency->id,
  36. ])
  37. ->assertOk()
  38. ->assertSeeText(trans('admin::app.settings.exchange-rates.index.create-success'));
  39. $this->assertModelWise([
  40. CurrencyExchangeRate::class => [
  41. [
  42. 'rate' => $rate,
  43. 'target_currency' => $currency->id,
  44. ],
  45. ],
  46. ]);
  47. });
  48. it('should return the exchange rate data for edit', function () {
  49. // Arrange.
  50. $exchangeRate = CurrencyExchangeRate::factory()->create([
  51. 'target_currency' => Currency::factory()->create()->id,
  52. ]);
  53. // Act and Assert.
  54. $this->loginAsAdmin();
  55. get(route('admin.settings.exchange_rates.edit', $exchangeRate->id))
  56. ->assertOk()
  57. ->assertJsonPath('data.exchangeRate.id', $exchangeRate->id)
  58. ->assertJsonPath('data.exchangeRate.target_currency', $exchangeRate->target_currency);
  59. });
  60. it('should fail the validation with errors when certain field not provided when update the exchange rates', function () {
  61. // Arrange.
  62. $exchangeRate = CurrencyExchangeRate::factory()->create([
  63. 'target_currency' => Currency::factory()->create()->id,
  64. ]);
  65. // Act and Assert.
  66. $this->loginAsAdmin();
  67. putJson(route('admin.settings.exchange_rates.update'), [
  68. 'id' => $exchangeRate->id,
  69. ])
  70. ->assertJsonValidationErrorFor('target_currency')
  71. ->assertJsonValidationErrorFor('rate')
  72. ->assertUnprocessable();
  73. });
  74. it('should update the currency exchange rate', function () {
  75. // Arrange.
  76. $exchangeRate = CurrencyExchangeRate::factory()->create([
  77. 'target_currency' => Currency::factory()->create()->id,
  78. ]);
  79. // Act and Assert.
  80. $this->loginAsAdmin();
  81. putJson(route('admin.settings.exchange_rates.update'), [
  82. 'id' => $exchangeRate->id,
  83. 'rate' => $rate = rand(1, 100),
  84. 'target_currency' => $exchangeRate->target_currency,
  85. ])
  86. ->assertOk()
  87. ->assertJsonPath('message', trans('admin::app.settings.exchange-rates.index.update-success'));
  88. $this->assertModelWise([
  89. CurrencyExchangeRate::class => [
  90. [
  91. 'rate' => $rate,
  92. 'target_currency' => $exchangeRate->target_currency,
  93. ],
  94. ],
  95. ]);
  96. });
  97. it('should delete the exchange rate', function () {
  98. // Arrange.
  99. $currencyExchangeRate = CurrencyExchangeRate::factory()->create([
  100. 'target_currency' => Currency::factory()->create()->id,
  101. ]);
  102. // Act and Assert.
  103. $this->loginAsAdmin();
  104. deleteJson(route('admin.settings.exchange_rates.delete', $currencyExchangeRate->id))
  105. ->assertOk()
  106. ->assertSeeText(trans('admin::app.settings.exchange-rates.index.delete-success'));
  107. $this->assertDatabaseMissing('locales', [
  108. 'id' => $currencyExchangeRate->id,
  109. ]);
  110. });