AddressTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of ibrand/address.
  4. *
  5. * (c) iBrand <https://www.ibrand.cc>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Illuminate\Foundation\Testing\DatabaseMigrations;
  11. /**
  12. * Created by PhpStorm.
  13. * User: Administrator
  14. * Date: 2018/2/3
  15. * Time: 17:19.
  16. */
  17. class AddressTest extends \Orchestra\Testbench\TestCase
  18. {
  19. use DatabaseMigrations;
  20. public $repository;
  21. protected function getEnvironmentSetUp($app)
  22. {
  23. // Setup default database to use sqlite :memory:
  24. $app['config']->set('database.default', 'testing');
  25. $app['config']->set('database.connections.testing', [
  26. 'driver' => 'sqlite',
  27. 'database' => ':memory:',
  28. ]);
  29. $app['config']->set('repository.cache.enabled', true);
  30. }
  31. protected function getPackageProviders($app)
  32. {
  33. return [
  34. Prettus\Repository\Providers\RepositoryServiceProvider::class
  35. , Orchestra\Database\ConsoleServiceProvider::class
  36. , iBrand\Component\Address\ServiceProvider::class];
  37. }
  38. protected function setUp()
  39. {
  40. parent::setUp(); // TODO: Change the autogenerated stub
  41. $this->loadMigrationsFrom(__DIR__ . '/database');
  42. $this->repository = $this->app->make(\iBrand\Component\Address\RepositoryContract::class);
  43. }
  44. public function testCreate()
  45. {
  46. //1. test create default address.
  47. $attributes = ['user_id' => 1, 'accept_name' => '小李子', 'mobile' => '18933336666', 'province' => 320000,
  48. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区', 'address' => 'xxx大道xx小区502', 'is_default' => 1,];
  49. $result = $this->repository->create($attributes);
  50. $this->assertEquals(\iBrand\Component\Address\Address::class, get_class($result));
  51. $this->assertEquals(1, $result->is_default);
  52. //1. test new default address, old address is not default.
  53. $attributes = ['user_id' => 1, 'accept_name' => '小陈子', 'mobile' => '18933336677', 'province' => 320000,
  54. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区1', 'address' => 'xxx大道xx小区5023', 'is_default' => 1,];
  55. $result1 = $this->repository->create($attributes);
  56. $result = $this->repository->find($result->id);
  57. $this->assertEquals(0, $result->is_default);
  58. $this->assertEquals(1, $result1->is_default);
  59. //2. test user addresses
  60. $addresses = $this->repository->getByUser(1);
  61. $this->assertCount(2, $addresses);
  62. }
  63. public function testGetDefault()
  64. {
  65. //test get user empty address.
  66. $result = $this->repository->getDefaultByUser(1);
  67. $this->assertNull($result);
  68. //test user create two default address.
  69. $attributes = ['user_id' => 1, 'accept_name' => '小李子', 'mobile' => '18933336666', 'province' => 320000,
  70. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区', 'address' => 'xxx大道xx小区502', 'is_default' => 1,];
  71. $this->repository->create($attributes);
  72. $attributes = ['user_id' => 1, 'accept_name' => '小陈子', 'mobile' => '18933336677', 'province' => 320000,
  73. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区1', 'address' => 'xxx大道xx小区5023', 'is_default' => 1,];
  74. $result1 = $this->repository->create($attributes);
  75. $default = $this->repository->getDefaultByUser(1);
  76. $this->assertEquals($result1->id, $default->id);
  77. $this->assertEquals(1, $default->is_default);
  78. //test get user default from all is not default addresses.
  79. $attributes = ['user_id' => 2, 'accept_name' => '小李子', 'mobile' => '18933336666', 'province' => 320000,
  80. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区', 'address' => 'xxx大道xx小区502', 'is_default' => 0,];
  81. $result = $this->repository->create($attributes);
  82. $attributes = ['user_id' => 2, 'accept_name' => '小陈子', 'mobile' => '18933336677', 'province' => 320000,
  83. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区1', 'address' => 'xxx大道xx小区5023', 'is_default' => 0,];
  84. $result1 = $this->repository->create($attributes);
  85. $default = $this->repository->getDefaultByUser(2);
  86. $this->assertEquals($result->id, $default->id);
  87. $this->assertEquals(0, $default->is_default);
  88. }
  89. public function testUpdateByUser()
  90. {
  91. $attributes = ['user_id' => 1, 'accept_name' => '小李子', 'mobile' => '18933336666', 'province' => 320000,
  92. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区', 'address' => 'xxx大道xx小区502', 'is_default' => 1,];
  93. $result = $this->repository->create($attributes);
  94. //1. test update own address.
  95. $attributes = ['accept_name' => '小李子1', 'mobile' => '18933336661'];
  96. $result = $this->repository->updateByUser($attributes, $result->id, 1);
  97. $this->assertEquals('18933336661', $result->mobile);
  98. $this->assertEquals('小李子1', $result->accept_name);
  99. //1. test update other user address.
  100. $attributes = ['user_id' => 2, 'accept_name' => '小李子', 'mobile' => '18933336666', 'province' => 320000,
  101. 'city' => 320100, 'area' => '320102', 'address_name' => '江苏省 南京市 玄武区', 'address' => 'xxx大道xx小区502', 'is_default' => 1,];
  102. $result = $this->repository->create($attributes);
  103. $attributes = ['accept_name' => '小李子1', 'mobile' => '18933336661'];
  104. $result = $this->repository->updateByUser($attributes, $result->id, 1);
  105. $this->assertFalse($result);
  106. }
  107. }