Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring & Tests

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Refactoring & Tests

Using tests during refactoring process

Avatar for Gilberto  Bueno

Gilberto Bueno

June 01, 2017

More Decks by Gilberto Bueno

Other Decks in Programming

Transcript

  1. refactoring Definição da Web Refatoração é o processo de modificar

    um sistema de software para melhorar a estrutura interna do código sem alterar seu comportamento externo… http://pt.wikipedia.org/wiki/Refactoring
  2. protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  3. protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  4. protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  5. protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  6. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function save_new_customer_billing_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveBillingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
  7. /** * @return array */ private function customerAddressStub() { return

    [ 'address' => $this->faker->streetAddress, 'zipcode' => $this->faker->postcode, 'city' => $this->faker->city, 'country' => $this->faker->country, ]; } /** * @return Customer */ private function createdCustomer() { $customer = new Customer(); return $customer->create([ 'name' => $this->faker->name, 'email' => $this->faker->email, 'phone' => $this->faker->phoneNumber, 'birthday' => $this->faker->dateTimeThisCentury->format('Y-m-d'), ]); }
  8. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } }
  9. <?php namespace Hydra\Athena\Validator; /** * Class CustomerAddress * @package Hydra\Athena\Validator

    */ class CustomerAddress extends Validator { public $rules = [ 'address' => 'required' ]; }
  10. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] ]); $model->fill($attributes); $model->save(); return $model; } }
  11. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function update_customer_billing_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBillingAddress($customer, $attributes); $attributes['city'] = 'Orlando'; $billingAddress = $this->service->saveBillingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Orlando'); } }
  12. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] // new address ]); $model->fill($attributes); $model->save(); return $model; } }
  13. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] // new address ]); $model->fill($attributes); $model->save(); return $model; } }
  14. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } }
  15. protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  16. protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  17. protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  18. protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>

    $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
  19. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; }
 }
  20. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } public function saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; }
 }
  21. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } public function saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; }
 }
  22. public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }

    public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
  23. public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }

    public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
  24. public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }

    public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
  25. public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }

    public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
  26. /** * @test */ public function save_new_customer_shipping_address_if_not_exist() { $customer =

    $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveShippingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } /** * @test */ public function update_customer_shipping_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveShippingAddress($customer, $attributes); $attributes['city'] = 'Miami'; $billingAddress = $this->service->saveShippingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Miami'); }
  27. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { /** *

    @var array */ protected $addressTypes = [ 'billing', 'shipping' ];
  28. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { private function

    saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
  29. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    __call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/jxRwic
  30. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    __call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/lzRI4Q
  31. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    __call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/mdH68i
  32. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    __call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/mdH68i saveBilling billing
  33. <?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function

    __call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/Im7UHU
  34. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function save_new_customer_billing_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveBilling($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
  35. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function update_customer_billing_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBilling($customer, $attributes); $attributes['city'] = 'Toronto'; $billingAddress = $this->service->saveBilling($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Toronto'); } }
  36. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function save_new_customer_shipping_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveShipping($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
  37. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test */ public function update_customer_shipping_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBilling($customer, $attributes); $attributes['city'] = 'Vancouver'; $billingAddress = $this->service->saveShipping($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Vancouver'); } }
  38. <?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *

    @test * @expectedException \Hydra\Athena\Exceptions\InvalidAddressTypeException */ public function throw_exception_if_address_type_is_invalid() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveWork($customer, $attributes); } }
  39. protected function saveCustomer(Klarna_Checkout_Order $order) { . . . $billingAddress =

    $this->customerAddressRepository->findWhere([ 'customer_id' => $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' => $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } . . . } 49 loc 71 loc
  40. private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([

    'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; } 11 loc