CustomerUnitTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Tests\Unit\Customers;
  3. use App\Shop\Addresses\Address;
  4. use App\Shop\Customers\Customer;
  5. use App\Shop\Customers\Exceptions\CreateCustomerInvalidArgumentException;
  6. use App\Shop\Customers\Exceptions\CustomerNotFoundException;
  7. use App\Shop\Customers\Exceptions\UpdateCustomerInvalidArgumentException;
  8. use App\Shop\Customers\Repositories\CustomerRepository;
  9. use App\Shop\Customers\Transformations\CustomerTransformable;
  10. use App\Shop\Orders\Order;
  11. use Illuminate\Database\Eloquent\Collection;
  12. use Illuminate\Support\Facades\Hash;
  13. use Tests\TestCase;
  14. class CustomerUnitTest extends TestCase
  15. {
  16. use CustomerTransformable;
  17. /** @test */
  18. public function it_can_search_for_customers()
  19. {
  20. $name = $this->faker->name;
  21. $customer = factory(Customer::class)->create([
  22. 'name' => $name,
  23. 'email' => $this->faker->email
  24. ]);
  25. $repo = new CustomerRepository($customer);
  26. $result = $repo->searchCustomer($name);
  27. $this->assertGreaterThan(0, $result->count());
  28. }
  29. /** @test */
  30. public function it_can_return_all_the_orders_of_the_customer()
  31. {
  32. $customer = factory(Customer::class)->create();
  33. $order = factory(Order::class)->create([
  34. 'customer_id' => $customer->id
  35. ]);
  36. $repo = new CustomerRepository($customer);
  37. $orders = $repo->findOrders();
  38. $this->assertInstanceOf(Collection::class, $orders);
  39. foreach ($orders as $o) {
  40. $this->assertEquals($order->id, $o->id);
  41. $this->assertEquals($customer->id, $o->customer_id);
  42. }
  43. }
  44. /** @test */
  45. public function it_can_transform_the_customer()
  46. {
  47. $customer = factory(Customer::class)->create();
  48. $repo = new CustomerRepository($customer);
  49. $customerFromDb = $repo->findCustomerById($customer->id);
  50. $cust = $this->transformCustomer($customer);
  51. $this->assertInternalType('string', $customerFromDb->status);
  52. $this->assertInternalType('int', $cust->status);
  53. }
  54. /** @test */
  55. public function it_errors_updating_the_customer_name_with_null_value()
  56. {
  57. $this->expectException(UpdateCustomerInvalidArgumentException::class);
  58. $cust = factory(Customer::class)->create();
  59. $customer = new CustomerRepository($cust);
  60. $customer->updateCustomer(['name' => null]);
  61. }
  62. /** @test */
  63. public function it_errors_creating_the_customer()
  64. {
  65. $this->expectException(CreateCustomerInvalidArgumentException::class);
  66. $this->expectExceptionCode(500);
  67. $customer = new CustomerRepository(new Customer);
  68. $customer->createCustomer([]);
  69. }
  70. /** @test */
  71. public function it_can_update_customers_password()
  72. {
  73. $cust = factory(Customer::class)->create();
  74. $customer = new CustomerRepository($cust);
  75. $customer->updateCustomer([
  76. 'name' => $this->faker->name,
  77. 'email' => $this->faker->email,
  78. 'status' => 1,
  79. 'password' => 'unknown'
  80. ]);
  81. $this->assertTrue(Hash::check('unknown', bcrypt($cust->password)));
  82. }
  83. /** @test */
  84. public function it_can_retrieve_the_address_attached_to_the_customer()
  85. {
  86. $cust = factory(Customer::class)->create();
  87. $address = factory(Address::class)->create();
  88. $customerRepo = new CustomerRepository($cust);
  89. $customerRepo->attachAddress($address);
  90. $lists = $customerRepo->findAddresses();
  91. $this->assertCount(1, $lists);
  92. }
  93. /** @test */
  94. public function it_can_attach_the_address()
  95. {
  96. $cust = factory(Customer::class)->create();
  97. $address = factory(Address::class)->create();
  98. $customer = new CustomerRepository($cust);
  99. $attachedAddress = $customer->attachAddress($address);
  100. $this->assertEquals($address->alias, $attachedAddress->alias);
  101. $this->assertEquals($address->address_1, $attachedAddress->address_1);
  102. }
  103. /** @test */
  104. public function it_can_soft_delete_a_customer()
  105. {
  106. $customer = factory(Customer::class)->create();
  107. $customerRepo = new CustomerRepository($customer);
  108. $delete = $customerRepo->deleteCustomer();
  109. $this->assertTrue($delete);
  110. $this->assertDatabaseHas('customers', $customer->toArray());
  111. }
  112. /** @test */
  113. public function it_fails_when_the_customer_is_not_found()
  114. {
  115. $this->expectException(CustomerNotFoundException::class);
  116. $customer = new CustomerRepository(new Customer);
  117. $customer->findCustomerById(999);
  118. }
  119. /** @test */
  120. public function it_can_find_a_customer()
  121. {
  122. $data = [
  123. 'name' => $this->faker->name,
  124. 'email' => $this->faker->email,
  125. 'password' => 'secret'
  126. ];
  127. $customer = new CustomerRepository(new Customer);
  128. $created = $customer->createCustomer($data);
  129. $found = $customer->findCustomerById($created->id);
  130. $this->assertInstanceOf(Customer::class, $found);
  131. $this->assertEquals($data['name'], $found->name);
  132. $this->assertEquals($data['email'], $found->email);
  133. }
  134. /** @test */
  135. public function it_can_update_the_customer()
  136. {
  137. $cust = factory(Customer::class)->create();
  138. $customer = new CustomerRepository($cust);
  139. $update = [
  140. 'name' => $this->faker->name,
  141. ];
  142. $updated = $customer->updateCustomer($update);
  143. $this->assertTrue($updated);
  144. $this->assertEquals($update['name'], $cust->name);
  145. $this->assertDatabaseHas('customers', $update);
  146. }
  147. /** @test */
  148. public function it_can_create_a_customer()
  149. {
  150. $data = [
  151. 'name' => $this->faker->name,
  152. 'email' => $this->faker->email,
  153. 'password' => 'secret'
  154. ];
  155. $customer = new CustomerRepository(new Customer);
  156. $created = $customer->createCustomer($data);
  157. $this->assertInstanceOf(Customer::class, $created);
  158. $this->assertEquals($data['name'], $created->name);
  159. $this->assertEquals($data['email'], $created->email);
  160. $collection = collect($data)->except('password');
  161. $this->assertDatabaseHas('customers', $collection->all());
  162. }
  163. }