CountryUnitTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Tests\Unit\Countries;
  3. use App\Shop\Countries\Exceptions\CountryInvalidArgumentException;
  4. use App\Shop\Countries\Exceptions\CountryNotFoundException;
  5. use App\Shop\Countries\Repositories\CountryRepository;
  6. use App\Shop\Provinces\Province;
  7. use App\Shop\Countries\Country;
  8. use App\Shop\States\State;
  9. use Illuminate\Support\Collection;
  10. use Tests\TestCase;
  11. class CountryUnitTest extends TestCase
  12. {
  13. /** @test */
  14. public function it_can_list_states()
  15. {
  16. $country = factory(Country::class)->create([
  17. 'iso' => 'US'
  18. ]);
  19. $usState = factory(State::class)->create([
  20. 'country_id' => $country->id
  21. ]);
  22. $countryRepo = new CountryRepository($country);
  23. $states = $countryRepo->listStates();
  24. $this->assertInstanceOf(Collection::class, $states);
  25. $states->each(function ($state) use ($usState) {
  26. $this->assertEquals($state->state, $usState->state);
  27. });
  28. }
  29. /** @test */
  30. public function it_can_create_the_country()
  31. {
  32. $data = [
  33. 'name' => $this->faker->unique()->country,
  34. 'iso' => $this->faker->unique()->countryISOAlpha3,
  35. 'iso3' => $this->faker->unique()->countryISOAlpha3,
  36. 'numcode' => $this->faker->randomDigit,
  37. 'phonecode' => $this->faker->randomDigit,
  38. 'status' => 1
  39. ];
  40. $countryRepo = new CountryRepository(new Country);
  41. $country = $countryRepo->createCountry($data);
  42. $this->assertEquals($data['name'], $country->name);
  43. $this->assertEquals($data['iso'], $country->iso);
  44. $this->assertEquals($data['iso3'], $country->iso3);
  45. $this->assertEquals($data['numcode'], $country->numcode);
  46. $this->assertEquals($data['phonecode'], $country->phonecode);
  47. $this->assertEquals($data['status'], $country->status);
  48. }
  49. /** @test */
  50. public function it_errors_when_updating_the_country()
  51. {
  52. $country = factory(Country::class)->create();
  53. $this->expectException(CountryInvalidArgumentException::class);
  54. $countryRepo = new CountryRepository($country);
  55. $countryRepo->updateCountry(['name' => null]);
  56. }
  57. /** @test */
  58. public function it_can_update_the_country()
  59. {
  60. $country = factory(Country::class)->create();
  61. $countryRepo = new CountryRepository($country);
  62. $update = ['name' => 'Zimbabwe'];
  63. $countryRepo->updateCountry($update);
  64. $this->assertEquals('Zimbabwe', $country->name);
  65. }
  66. /** @test */
  67. public function it_can_find_the_provinces_associated_with_the_country()
  68. {
  69. $country = factory(Country::class)->create();
  70. $prov = factory(Province::class)->create(['country_id' => $country->id]);
  71. $country->provinces()->save($prov);
  72. $countryRepo = new CountryRepository($country);
  73. $provinces = $countryRepo->findProvinces();
  74. foreach ($provinces as $province) {
  75. $this->assertEquals($prov->id, $province->id);
  76. }
  77. }
  78. /** @test */
  79. public function it_errors_when_the_country_is_not_found()
  80. {
  81. $this->expectException(CountryNotFoundException::class);
  82. $countryRepo = new CountryRepository(new Country);
  83. $countryRepo->findCountryById(999);
  84. }
  85. /** @test */
  86. public function it_can_find_the_country()
  87. {
  88. $country = factory(Country::class)->create();
  89. $countryRepo = new CountryRepository($country);
  90. $found = $countryRepo->findCountryById($country->id);
  91. $this->assertEquals($country->name, $found->name);
  92. }
  93. /** @test */
  94. public function it_can_list_all_countries()
  95. {
  96. $country = factory(Country::class)->create();
  97. $countryRepo = new CountryRepository($country);
  98. $this->assertCount(2, $countryRepo->listCountries());
  99. }
  100. }