ProvincesUnitTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Tests\Unit\Provinces;
  3. use App\Shop\Countries\Country;
  4. use App\Shop\Provinces\Exceptions\ProvinceNotFoundException;
  5. use App\Shop\Provinces\Province;
  6. use App\Shop\Provinces\Repositories\ProvinceRepository;
  7. use App\Shop\Cities\City;
  8. use Tests\TestCase;
  9. class ProvincesUnitTest extends TestCase
  10. {
  11. /** @test */
  12. public function it_should_show_the_country_of_the_province()
  13. {
  14. $country = factory(Country::class)->create();
  15. $province = factory(Province::class)->create([
  16. 'country_id' => $country->id
  17. ]);
  18. $repo = new ProvinceRepository($province);
  19. $found = $repo->findCountry();
  20. $this->assertInstanceOf(Country::class, $found);
  21. $this->assertEquals($country->name, $found->name);
  22. }
  23. /** @test */
  24. public function it_error_updating_the_province_without_the_country()
  25. {
  26. $this->expectException(ProvinceNotFoundException::class);
  27. $province = factory(Province::class)->create();
  28. $data = [
  29. 'name' => $this->faker->name,
  30. 'country_id' => null
  31. ];
  32. $repo = new ProvinceRepository($province);
  33. $repo->updateProvince($data);
  34. }
  35. /** @test */
  36. public function it_can_list_cities()
  37. {
  38. $province = factory(Province::class)->create();
  39. $city = factory(City::class)->create([
  40. 'province_id' => $province->id
  41. ]);
  42. $repo = new ProvinceRepository(new Province());
  43. $collection = $repo->listCities($province->id);
  44. $collection->each(function ($item) use ($city) {
  45. $this->assertEquals($item->name, $city->name);
  46. });
  47. }
  48. /** @test */
  49. public function it_can_update_the_province()
  50. {
  51. $province = factory(Province::class)->create();
  52. $data = [
  53. 'name' => $this->faker->name
  54. ];
  55. $repo = new ProvinceRepository($province);
  56. $repo->updateProvince($data);
  57. $this->assertEquals($data['name'], $province->name);
  58. }
  59. /** @test */
  60. public function it_will_error_when_the_province_is_not_found()
  61. {
  62. $this->expectException(ProvinceNotFoundException::class);
  63. $this->expectExceptionMessage('Province not found.');
  64. $provinceRepo = new ProvinceRepository(new Province);
  65. $provinceRepo->findProvinceById(999);
  66. }
  67. /** @test */
  68. public function it_can_show_the_province()
  69. {
  70. $province = factory(Province::class)->create();
  71. $provinceRepo = new ProvinceRepository(new Province);
  72. $found = $provinceRepo->findProvinceById($province->id);
  73. $this->assertEquals($province->name, $found->name);
  74. }
  75. /** @test */
  76. public function it_can_list_all_the_cities_within_the_province()
  77. {
  78. $province = factory(Province::class)->create();
  79. $city = new City(['name' => $this->faker->city]);
  80. $city->province()->associate($province);
  81. $province->cities()->save($city);
  82. $cities = $province->cities()->get();
  83. $this->assertCount(1, $cities);
  84. }
  85. /** @test */
  86. public function it_can_list_all_the_provinces()
  87. {
  88. factory(Province::class, 5)->create();
  89. $provinceRepo = new ProvinceRepository(new Province);
  90. $provinces = $provinceRepo->listProvinces();
  91. foreach ($provinces as $province) {
  92. $this->assertDatabaseHas('provinces', $province->toArray());
  93. }
  94. }
  95. }