33namespace Tests \Feature \Departments \Api ;
44
55use App \Models \AssetModel ;
6+ use App \Models \Company ;
67use App \Models \Department ;
78use App \Models \Category ;
9+ use App \Models \Location ;
810use App \Models \User ;
911use Illuminate \Testing \Fluent \AssertableJson ;
1012use Tests \TestCase ;
@@ -13,30 +15,93 @@ class CreateDepartmentsTest extends TestCase
1315{
1416
1517
16- public function testRequiresPermissionToCreateDepartment ()
18+ public function test_requires_permission_to_create_department ()
1719 {
1820 $ this ->actingAsForApi (User::factory ()->create ())
1921 ->postJson (route ('api.departments.store ' ))
2022 ->assertForbidden ();
2123 }
2224
23- public function testCanCreateDepartment ()
25+ public function test_can_create_department_with_all_fields ()
2426 {
25- $ response = $ this ->actingAsForApi (User::factory ()->superuser ()->create ())
27+ $ company = Company::factory ()->create ();
28+ $ location = Location::factory ()->create ();
29+ $ manager = User::factory ()->create ();
30+ $ user = User::factory ()->superuser ()->create ();
31+ $ response = $ this ->actingAsForApi ($ user )
2632 ->postJson (route ('api.departments.store ' ), [
27- 'name ' => 'Test Department ' ,
28- 'notes ' => 'Test Note ' ,
33+ 'name ' => 'Test Department ' ,
34+ 'company_id ' => $ company ->id ,
35+ 'location_id ' => $ location ->id ,
36+ 'manager_id ' => $ manager ->id ,
37+ 'notes ' => 'Test Note ' ,
38+ 'phone ' => '1234567890 ' ,
39+ 'fax ' => '1234567890 ' ,
2940 ])
3041 ->assertOk ()
3142 ->assertStatusMessageIs ('success ' )
32- ->assertStatus (200 )
3343 ->json ();
3444
3545 $ this ->assertTrue (Department::where ('name ' , 'Test Department ' )->exists ());
3646
3747 $ department = Department::find ($ response ['payload ' ]['id ' ]);
3848 $ this ->assertEquals ('Test Department ' , $ department ->name );
3949 $ this ->assertEquals ('Test Note ' , $ department ->notes );
50+
51+ $ this ->assertDatabaseHas ('departments ' , [
52+ 'name ' => 'Test Department ' ,
53+ 'company_id ' => $ company ->id ,
54+ 'location_id ' => $ location ->id ,
55+ 'manager_id ' => $ manager ->id ,
56+ 'notes ' => 'Test Note ' ,
57+ 'phone ' => '1234567890 ' ,
58+ 'fax ' => '1234567890 ' ,
59+ 'created_by ' => $ user ->id ,
60+ ]);
61+ }
62+
63+ public function test_name_required_for_department ()
64+ {
65+ $ response = $ this ->actingAsForApi (User::factory ()->superuser ()->create ())
66+ ->postJson (route ('api.departments.store ' ), [
67+ 'company_id ' => Company::factory ()->create ()->id ,
68+ ])
69+ ->assertOk ()
70+ ->assertStatusMessageIs ('error ' );
71+ }
72+
73+ public function test_only_name_required_for_department ()
74+ {
75+ $ response = $ this ->actingAsForApi (User::factory ()->superuser ()->create ())
76+ ->postJson (route ('api.departments.store ' ), [
77+ 'name ' => 'Test Department ' ,
78+ ])
79+ ->assertOk ()
80+ ->assertStatusMessageIs ('success ' );
4081 }
4182
83+ public function test_arrays_not_allowed_for_numeric_fields ()
84+ {
85+ $ response = $ this ->actingAsForApi (User::factory ()->superuser ()->create ())
86+ ->postJson (route ('api.departments.store ' ), [
87+ 'name ' => 'Test Department ' ,
88+ 'company_id ' => [1 , 2 ],
89+ ])
90+ ->assertOk ()
91+ ->assertStatusMessageIs ('error ' );
92+ }
93+
94+ public function test_arrays_of_strings_are_not_allowed_for_numeric_fields ()
95+ {
96+ $ response = $ this ->actingAsForApi (User::factory ()->superuser ()->create ())
97+ ->postJson (route ('api.departments.store ' ), [
98+ 'name ' => 'Test Department ' ,
99+ 'company_id ' => ['1 ' , '2 ' ],
100+ ])
101+ ->assertOk ()
102+ ->assertStatusMessageIs ('error ' );
103+ }
104+
105+
106+
42107}
0 commit comments