|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Feature\MmexClient; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Category; |
|
6
|
|
|
use App\Models\Payee; |
|
7
|
|
|
|
|
8
|
|
|
class CategoryTest extends MmexTestCase |
|
9
|
|
|
{ |
|
10
|
|
View Code Duplication |
public function testDeleteAllCategories() |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
// Arrange |
|
13
|
|
|
$categorie = factory(Category::class)->create(['user_id' => $this->user->id]); |
|
14
|
|
|
|
|
15
|
|
|
$url = $this->buildUrl(['delete_category' => 'true']); |
|
16
|
|
|
|
|
17
|
|
|
// Act |
|
18
|
|
|
$response = $this->get($url); |
|
19
|
|
|
|
|
20
|
|
|
// Assert |
|
21
|
|
|
$this->assertSeeMmexSuccess($response); |
|
22
|
|
|
$this->assertIsSoftDeletedInDatabase('categories', ['user_id' => $this->user->id, 'name' => $categorie->name]); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testImportCategories() |
|
26
|
|
|
{ |
|
27
|
|
|
// Arrange |
|
28
|
|
|
$data = ['MMEX_Post' => '{ "Categories" : [ { "CategoryName" : "Bills", "SubCategoryName" : "Telecom" }, { "CategoryName" : "Bills", "SubCategoryName" : "Water" }, { "CategoryName" : "Automobile", "SubCategoryName" : "Maintenance" }, { "CategoryName" : "Automobile", "SubCategoryName" : "Parking" } ] }']; |
|
29
|
|
|
$url = $this->buildUrl(['import_category' => 'true']); |
|
30
|
|
|
|
|
31
|
|
|
// Act |
|
32
|
|
|
$response = $this->postJson($url, $data); |
|
33
|
|
|
|
|
34
|
|
|
// Assert |
|
35
|
|
|
$this->assertSeeMmexSuccess($response); |
|
36
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Bills']); |
|
37
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Telecom']); |
|
38
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Water']); |
|
39
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Automobile']); |
|
40
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Maintenance']); |
|
41
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Parking']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testImportSubCategories() |
|
45
|
|
|
{ |
|
46
|
|
|
// Arrange |
|
47
|
|
|
$data = ['MMEX_Post' => '{ "Categories" : [ { "CategoryName" : "Bills", "SubCategoryName" : "Telecom" }, { "CategoryName" : "Bills", "SubCategoryName" : "Water" }, { "CategoryName" : "Automobile", "SubCategoryName" : "Maintenance" }, { "CategoryName" : "Automobile", "SubCategoryName" : "Parking" } ] }']; |
|
48
|
|
|
$url = $this->buildUrl(['import_category' => 'true']); |
|
49
|
|
|
|
|
50
|
|
|
$bills = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Bills']); |
|
51
|
|
|
$automobile = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Automobile']); |
|
52
|
|
|
|
|
53
|
|
|
// Act |
|
54
|
|
|
$response = $this->postJson($url, $data); |
|
55
|
|
|
|
|
56
|
|
|
// Assert |
|
57
|
|
|
$this->assertSeeMmexSuccess($response); |
|
58
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Bills', 'id' => $bills->id]); |
|
59
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Telecom', 'parent_id' => $bills->id]); |
|
60
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Water', 'parent_id' => $bills->id]); |
|
61
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Automobile', 'id' => $automobile->id]); |
|
62
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Maintenance', 'parent_id' => $automobile->id]); |
|
63
|
|
|
$this->assertDatabaseHas('categories', ['user_id' => $this->user->id, 'name' => 'Parking', 'parent_id' => $automobile->id]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @test |
|
68
|
|
|
*/ |
|
69
|
|
|
public function it_preserves_default_categories_on_payees_table_after_importing_categories() |
|
70
|
|
|
{ |
|
71
|
|
|
// Arrange |
|
72
|
|
|
$category1 = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Bills']); |
|
73
|
|
|
$subcategory1 = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Services', 'parent_id' => $category1->id]); |
|
74
|
|
|
$category2 = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Food']); |
|
75
|
|
|
$subcategory2 = factory(Category::class)->create(['user_id' => $this->user->id, 'name' => 'Purchase', 'parent_id' => $category2->id]); |
|
76
|
|
|
|
|
77
|
|
|
$payee1 = factory(Payee::class)->create(['user_id' => $this->user->id, 'last_category_id' => $subcategory1->id]); |
|
78
|
|
|
$payee2 = factory(Payee::class)->create(['user_id' => $this->user->id, 'last_category_id' => $subcategory2->id]); |
|
79
|
|
|
|
|
80
|
|
|
$data = ['MMEX_Post' => '{ "Categories" : [ { "CategoryName" : "Bills", "SubCategoryName" : "Services" }, { "CategoryName" : "Food", "SubCategoryName" : "Purchase" } ] }']; |
|
81
|
|
|
|
|
82
|
|
|
// Act |
|
83
|
|
|
$deletePayeesUrl = $this->buildUrl(['delete_category' => 'true']); |
|
84
|
|
|
$this->getJson($deletePayeesUrl); |
|
85
|
|
|
|
|
86
|
|
|
$url = $this->buildUrl(['import_category' => 'true']); |
|
87
|
|
|
$response = $this->postJson($url, $data); |
|
88
|
|
|
|
|
89
|
|
|
// Assert |
|
90
|
|
|
$this->assertSeeMmexSuccess($response); |
|
91
|
|
|
$category1 = $this->assertDatabaseHasOnceAndReturnFirst('categories', ['id' => $category1->id, 'name' => 'Bills', 'deleted_at' => null]); |
|
92
|
|
|
$subcategory1 = $this->assertDatabaseHasOnceAndReturnFirst('categories', ['id' => $subcategory1->id, 'name' => 'Services', 'parent_id' => $category1->id, 'deleted_at' => null]); |
|
93
|
|
|
|
|
94
|
|
|
$category2 = $this->assertDatabaseHasOnceAndReturnFirst('categories', ['id' => $category2->id, 'name' => 'Food', 'deleted_at' => null]); |
|
95
|
|
|
$subcategory2 = $this->assertDatabaseHasOnceAndReturnFirst('categories', ['id' => $subcategory2->id, 'name' => 'Purchase', 'parent_id' => $category2->id, 'deleted_at' => null]); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertDatabaseHas('payees', ['id' => $payee1->id, 'last_category_id' => $subcategory1->id]); |
|
98
|
|
|
$this->assertDatabaseHas('payees', ['id' => $payee2->id, 'last_category_id' => $subcategory2->id]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.