Browse Source

Pass all test after project customer relation changed to Customer model

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
2c6041e502
  1. 21
      app/Entities/BaseRepository.php
  2. 16
      app/Entities/Projects/ProjectPresenter.php
  3. 40
      app/Entities/Projects/ProjectsRepository.php
  4. 20
      app/Entities/Subscriptions/Subscription.php
  5. 9
      database/factories/ModelFactory.php
  6. 4
      resources/lang/id/app.php
  7. 68
      tests/Feature/ManageProjectsTest.php
  8. 52
      tests/Feature/ManageSubscriptionsTest.php
  9. 32
      tests/Unit/Models/SubscriptionTest.php

21
app/Entities/BaseRepository.php

@ -2,42 +2,45 @@
namespace App\Entities;
use App\Entities\Partners\Customer;
use App\Entities\Partners\Vendor;
use App\Entities\Projects\Feature;
use App\Entities\Projects\Project;
use App\Entities\Users\User;
/**
* Base Repository Class
*/
abstract class BaseRepository extends EloquentRepository {
* Base Repository Class
*/
abstract class BaseRepository extends EloquentRepository
{
public function getCustomersList()
{
return User::orderBy('name')->hasRoles(['customer'])->pluck('name','id');
return Customer::orderBy('name')->pluck('name', 'id');
}
public function getCustomersAndVendorsList()
{
return User::orderBy('name')->hasRoles(['customer','vendor'])->pluck('name','id');
return User::orderBy('name')->hasRoles(['customer', 'vendor'])->pluck('name', 'id');
}
public function getWorkersList()
{
return User::orderBy('name')->hasRoles(['worker'])->pluck('name','id');
return User::orderBy('name')->hasRoles(['worker'])->pluck('name', 'id');
}
public function getVendorsList()
{
return User::orderBy('name')->hasRoles(['vendor'])->pluck('name','id');
return Vendor::orderBy('name')->pluck('name', 'id');
}
public function getProjectsList()
{
return Project::orderBy('name')->pluck('name','id');
return Project::orderBy('name')->pluck('name', 'id');
}
public function requireFeatureById($featureId)
{
return Feature::findOrFail($featureId);
}
}
}

16
app/Entities/Projects/ProjectPresenter.php

@ -8,7 +8,7 @@ class ProjectPresenter extends Presenter
{
public function customerNameAndEmail()
{
return $this->customer_id ? $this->customer->name . ' (' . $this->customer->email . ')' : '-';
return $this->customer_id ? $this->customer->name.' ('.$this->customer->email.')' : '-';
}
public function projectLink()
@ -23,14 +23,16 @@ class ProjectPresenter extends Presenter
public function workDuration()
{
if (is_null($this->entity->end_date))
if (is_null($this->entity->end_date)) {
return '-';
}
$workDuration = dateDifference($this->entity->start_date,$this->entity->end_date);
if ((int) $workDuration > 30)
return dateDifference($this->entity->start_date,$this->entity->end_date, '%m Bulan %d Hari');
$workDuration = dateDifference($this->entity->start_date, $this->entity->end_date);
if ((int) $workDuration > 30) {
return dateDifference($this->entity->start_date, $this->entity->end_date, '%m Bulan %d Hari');
}
return $workDuration . ' Hari';
return $workDuration.' Hari';
}
}
}

40
app/Entities/Projects/ProjectsRepository.php

@ -3,13 +3,12 @@
namespace App\Entities\Projects;
use App\Entities\BaseRepository;
use App\Entities\Users\User;
use App\Entities\Partners\Customer;
use DB;
use Option;
/**
* Projects Repository Class
*/
* Projects Repository Class
*/
class ProjectsRepository extends BaseRepository
{
protected $model;
@ -24,10 +23,12 @@ class ProjectsRepository extends BaseRepository
$statusIds = array_keys(getProjectStatusesList());
return $this->model->latest()
->where(function($query) use ($q, $statusId, $statusIds) {
$query->where('name','like','%'.$q.'%');
if ($statusId && in_array($statusId, $statusIds))
->where(function ($query) use ($q, $statusId, $statusIds) {
$query->where('name', 'like', '%'.$q.'%');
if ($statusId && in_array($statusId, $statusIds)) {
$query->where('status_id', $statusId);
}
})
->withCount('payments')
->with('customer')
@ -38,11 +39,11 @@ class ProjectsRepository extends BaseRepository
public function create($projectData)
{
$projectData['project_value'] = $projectData['proposal_value'] ?: 0;
$projectData['owner_id'] = auth()->id();
$projectData['owner_id'] = auth()->id();
DB::beginTransaction();
if (isset($projectData['customer_id']) == false || $projectData['customer_id'] == '') {
$customer = $this->createNewCustomer($projectData['customer_name'], $projectData['customer_email']);
$customer = $this->createNewCustomer($projectData['customer_name'], $projectData['customer_email']);
$projectData['customer_id'] = $customer->id;
}
unset($projectData['customer_name']);
@ -60,13 +61,11 @@ class ProjectsRepository extends BaseRepository
public function createNewCustomer($customerName, $customerEmail)
{
$newCustomer = new User;
$newCustomer->name = $customerName;
$newCustomer = new Customer;
$newCustomer->name = $customerName;
$newCustomer->email = $customerEmail;
$newCustomer->password = Option::get('default_password', 'member');
$newCustomer->remember_token = str_random(10);
$newCustomer->save();
$newCustomer->assignRole('customer');
return $newCustomer;
}
@ -95,17 +94,18 @@ class ProjectsRepository extends BaseRepository
public function getProjectFeatures($projectId, $type = null)
{
return Feature::where(function($query) use ($projectId, $type) {
return Feature::where(function ($query) use ($projectId, $type) {
$query->whereProjectId($projectId);
if ($type)
if ($type) {
$query->whereTypeId($type);
}
})->orderBy('position')->with('worker','tasks')->get();
})->orderBy('position')->with('worker', 'tasks')->get();
}
public function updateStatus($statusId, $projectId)
{
$project = $this->requireById($projectId);
$project = $this->requireById($projectId);
$project->status_id = $statusId;
$project->save();
@ -116,11 +116,11 @@ class ProjectsRepository extends BaseRepository
{
$featureOrder = explode(',', $sortedData);
foreach ($featureOrder as $order => $featureId) {
$feature = $this->requireFeatureById($featureId);
$feature = $this->requireFeatureById($featureId);
$feature->position = $order + 1;
$feature->save();
}
return $featureOrder;
}
}
}

20
app/Entities/Subscriptions/Subscription.php

@ -2,37 +2,33 @@
namespace App\Entities\Subscriptions;
use App\Entities\Projects\Project;
use App\Entities\Subscriptions\SubscriptionPresenter;
use App\Entities\Users\User;
use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;
class Subscription extends Model {
class Subscription extends Model
{
use PresentableTrait;
protected $presenter = SubscriptionPresenter::class;
protected $guarded = ['id','created_at','updated_at'];
protected $presenter = 'App\Entities\Subscriptions\SubscriptionPresenter';
protected $guarded = ['id', 'created_at', 'updated_at'];
public function project()
{
return $this->belongsTo(Project::class);
return $this->belongsTo('App\Entities\Projects\Project');
}
public function customer()
{
return $this->belongsTo(User::class,'customer_id');
return $this->belongsTo('App\Entities\Partners\Customer');
}
public function vendor()
{
return $this->belongsTo(User::class,'vendor_id');
return $this->belongsTo('App\Entities\Partners\Vendor', 'vendor_id');
}
public function status()
{
return $this->status_id ? 'Aktif' : 'Non Aktif';
return $this->status_id ? trans('app.active') : trans('app.in_active');
}
}

9
database/factories/ModelFactory.php

@ -2,6 +2,7 @@
use App\Entities\Invoices\Invoice;
use App\Entities\Partners\Customer;
use App\Entities\Partners\Vendor;
use App\Entities\Payments\Payment;
use App\Entities\Projects\Feature;
use App\Entities\Projects\Project;
@ -73,19 +74,19 @@ $factory->define(Subscription::class, function (Faker\Generator $faker) {
return factory(Project::class)->create()->id;
},
'status_id' => 1,
'domain_name' => 'www.' . str_random(10) . '.com',
'domain_name' => 'www.'.str_random(10).'.com',
'domain_price' => 125000,
'epp_code' => str_random(10),
'hosting_capacity' => rand(1, 3) . ' GB',
'hosting_capacity' => rand(1, 3).' GB',
'hosting_price' => rand(1, 5) * 100000,
'start_date' => $startDate->format('Y-m-d'),
'due_date' => $startDate->addYears(1)->format('Y-m-d'),
'remark' => $faker->paragraph,
'customer_id' => function () {
return factory(User::class)->create()->id;
return factory(Customer::class)->create()->id;
},
'vendor_id' => function () {
return factory(User::class)->create()->id;
return factory(Vendor::class)->create()->id;
},
];
});

4
resources/lang/id/app.php

@ -39,4 +39,6 @@ return [
'change_photo' => 'Ganti Foto',
'welcome' => 'Selamat Datang',
'to' => 'Kepada',
];
'active' => 'Aktif',
'in_active' => 'Non Aktif',
];

68
tests/Feature/ManageProjectsTest.php

@ -2,11 +2,11 @@
namespace Tests\Feature;
use App\Entities\Partners\Customer;
use App\Entities\Payments\Payment;
use App\Entities\Projects\Feature;
use App\Entities\Projects\Project;
use App\Entities\Projects\Task;
use App\Entities\Users\User;
use Tests\TestCase;
class ManageProjectsTest extends TestCase
@ -14,18 +14,15 @@ class ManageProjectsTest extends TestCase
/** @test */
public function admin_can_input_new_project_with_existing_customer()
{
$users = factory(User::class, 2)->create();
$users[0]->assignRole('admin');
$this->actingAs($users[0]);
$users[1]->assignRole('customer');
$user = $this->adminUserSigningIn();
$customer = factory(Customer::class)->create();
$this->visit(route('projects.index'));
$this->seePageIs(route('projects.index'));
$this->click(trans('project.create'));
$this->seePageIs(route('projects.create'));
$this->type('Project Baru', 'name');
$this->select($users[1]->id, 'customer_id');
$this->select($customer->id, 'customer_id');
$this->type('2016-04-15', 'proposal_date');
$this->type('2000000', 'proposal_value');
$this->type('Deskripsi project baru', 'description');
@ -59,11 +56,19 @@ class ManageProjectsTest extends TestCase
$this->type('Customer Baru', 'customer_name');
$this->type('email@customer.baru', 'customer_email');
$this->press(trans('project.create'));
$this->see(trans('project.created'));
$this->see('Project Baru');
$this->seeInDatabase('users', ['name' => 'Customer Baru', 'email' => 'email@customer.baru']);
$newCustomer = User::whereName('Customer Baru')->whereEmail('email@customer.baru')->first();
$this->seeInDatabase('projects', ['name' => 'Project Baru', 'proposal_value' => '2000000', 'customer_id' => $newCustomer->id]);
$this->seeInDatabase('customers', [
'name' => 'Customer Baru',
'email' => 'email@customer.baru',
]);
$newCustomer = Customer::whereName('Customer Baru')->whereEmail('email@customer.baru')->first();
$this->seeInDatabase('projects', [
'name' => 'Project Baru',
'proposal_value' => '2000000',
'customer_id' => $newCustomer->id,
]);
}
/** @test */
@ -73,7 +78,7 @@ class ManageProjectsTest extends TestCase
$project = factory(Project::class)->create(['owner_id' => $user->id]);
$feature = factory(Feature::class)->create(['project_id' => $project->id]);
$task = factory(Task::class)->create(['feature_id' => $feature->id]);
$task = factory(Task::class)->create(['feature_id' => $feature->id]);
$payment = factory(Payment::class)->create(['project_id' => $project->id]);
$this->visit('projects/'.$project->id);
@ -84,9 +89,9 @@ class ManageProjectsTest extends TestCase
$this->see(trans('project.deleted'));
$this->notSeeInDatabase('projects', [
'name' => $project->name,
'name' => $project->name,
'proposal_value' => $project->proposal_value,
'owner_id' => $user->id,
'owner_id' => $user->id,
]);
$this->notSeeInDatabase('payments', [
@ -105,12 +110,9 @@ class ManageProjectsTest extends TestCase
/** @test */
public function admin_can_edit_a_project()
{
$users = factory(User::class, 2)->create();
$users[0]->assignRole('admin');
$this->actingAs($users[0]);
$project = factory(Project::class)->create(['owner_id' => $users[0]->id]);
$users[1]->assignRole('customer');
$user = $this->adminUserSigningIn();
$customer = factory(Customer::class)->create();
$project = factory(Project::class)->create(['owner_id' => $user->id]);
$this->visit('projects/'.$project->id.'/edit');
$this->seePageIs('projects/'.$project->id.'/edit');
@ -122,36 +124,34 @@ class ManageProjectsTest extends TestCase
$this->type(2000000, 'proposal_value');
$this->type(2000000, 'project_value');
$this->select(4, 'status_id');
$this->select($users[1]->id, 'customer_id');
$this->select($customer->id, 'customer_id');
$this->type('Edit deskripsi project', 'description');
$this->press(trans('project.update'));
$this->seeInDatabase('projects', [
'id' => $project->id,
'name' => 'Edit Project',
'id' => $project->id,
'name' => 'Edit Project',
'proposal_date' => '2016-04-15',
'start_date' => '2016-04-25',
'end_date' => '2016-05-05',
'customer_id' => $users[1]->id,
'description' => 'Edit deskripsi project',
'start_date' => '2016-04-25',
'end_date' => '2016-05-05',
'customer_id' => $customer->id,
'description' => 'Edit deskripsi project',
]);
}
/** @test */
public function form_is_validated_on_invalid_project_entry()
{
$users = factory(User::class, 2)->create();
$users[0]->assignRole('admin');
$this->actingAs($users[0]);
$user = $this->adminUserSigningIn();
$users[1]->assignRole('customer');
$customer = factory(Customer::class)->create();
$this->visit(route('projects.index'));
$this->seePageIs(route('projects.index'));
$this->click(trans('project.create'));
$this->seePageIs(route('projects.create'));
$this->type('', 'name');
$this->select($users[1]->id, 'customer_id');
$this->select($customer->id, 'customer_id');
$this->type('2016-04-15aa', 'proposal_date');
$this->type('', 'proposal_value');
$this->type('Deskripsi project baru', 'description');
@ -163,9 +163,9 @@ class ManageProjectsTest extends TestCase
/** @test */
public function admin_can_update_project_status_on_project_detail_page()
{
$user = $this->adminUserSigningIn();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->id, 'status_id' => 1]);
$this->visit(route('projects.show', $project->id));
$this->seePageIs(route('projects.show', $project->id));
$this->select(2, 'status_id');
@ -174,7 +174,7 @@ class ManageProjectsTest extends TestCase
$this->seePageIs(route('projects.show', $project->id));
$this->seeInDatabase('projects', [
'id' => $project->id,
'id' => $project->id,
'status_id' => 2,
]);
}

52
tests/Feature/ManageSubscriptionsTest.php

@ -2,6 +2,8 @@
namespace Tests\Feature;
use App\Entities\Partners\Customer;
use App\Entities\Partners\Vendor;
use App\Entities\Projects\Project;
use App\Entities\Subscriptions\Subscription;
use Tests\TestCase;
@ -11,10 +13,10 @@ class ManageSubscriptionsTest extends TestCase
/** @test */
public function admin_can_entry_subscription()
{
$user = $this->adminUserSigningIn();
$vendor = $this->createUser('vendor');
$project = factory(Project::class)->create();
$customer = $this->createUser('customer');
$user = $this->adminUserSigningIn();
$vendor = factory(Vendor::class)->create();
$project = factory(Project::class)->create();
$customer = factory(Customer::class)->create();
$this->visit(route('subscriptions.index'));
$this->click(trans('subscription.create'));
@ -37,25 +39,25 @@ class ManageSubscriptionsTest extends TestCase
$this->see(trans('subscription.created'));
$this->seeInDatabase('subscriptions', [
'project_id' => $project->id,
'project_id' => $project->id,
'domain_price' => 100000,
'epp_code' => 'EPPCODE',
'status_id' => 1,
'start_date' => '2015-05-02',
'due_date' => '2016-05-02',
'customer_id' => $customer->id,
'vendor_id' => $vendor->id,
'epp_code' => 'EPPCODE',
'status_id' => 1,
'start_date' => '2015-05-02',
'due_date' => '2016-05-02',
'customer_id' => $customer->id,
'vendor_id' => $vendor->id,
]);
}
/** @test */
public function admin_can_edit_subscription_data()
{
$user = $this->adminUserSigningIn();
$vendor = $this->createUser('vendor');
$eppCode = str_random(10);
$project = factory(Project::class)->create();
$customer = $this->createUser('customer');
$user = $this->adminUserSigningIn();
$vendor = factory(Vendor::class)->create();
$eppCode = str_random(10);
$project = factory(Project::class)->create();
$customer = factory(Customer::class)->create();
$subscription = factory(Subscription::class)->create(['customer_id' => $customer->id, 'project_id' => $project->id]);
@ -77,16 +79,16 @@ class ManageSubscriptionsTest extends TestCase
$this->seePageIs(route('subscriptions.edit', $subscription->id));
$this->see(trans('subscription.updated'));
$this->seeInDatabase('subscriptions', [
'epp_code' => $eppCode,
'customer_id' => $customer->id,
'project_id' => $project->id,
'status_id' => 1,
'epp_code' => $eppCode,
'customer_id' => $customer->id,
'project_id' => $project->id,
'status_id' => 1,
'hosting_capacity' => '4GB',
'hosting_price' => '500000',
'start_date' => '2015-05-02',
'due_date' => '2016-05-02',
'customer_id' => $customer->id,
'vendor_id' => $vendor->id,
'hosting_price' => '500000',
'start_date' => '2015-05-02',
'due_date' => '2016-05-02',
'customer_id' => $customer->id,
'vendor_id' => $vendor->id,
]);
}

32
tests/Unit/Models/SubscriptionTest.php

@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Models;
use App\Entities\Partners\Customer;
use App\Entities\Partners\Vendor;
use App\Entities\Subscriptions\Subscription;
use Tests\TestCase as TestCase;
class SubscriptionTest extends TestCase
{
/** @test */
public function it_has_domain_name_attribute()
{
$subscription = factory(Subscription::class)->create(['domain_name' => 'Subscription 1 Domain Name']);
$this->assertEquals('Subscription 1 Domain Name', $subscription->domain_name);
}
/** @test */
public function it_has_customer_relation()
{
$subscription = factory(Subscription::class)->create();
$this->assertTrue($subscription->customer instanceof Customer);
}
/** @test */
public function it_has_vendor_relation()
{
$subscription = factory(Subscription::class)->create();
$this->assertTrue($subscription->vendor instanceof Vendor);
}
}
Loading…
Cancel
Save