Completed
Push — task/file-cleanup ( 35de75...35de75 )
by Grant
07:58 queued 03:00
created

JobControllerTest::testManagerCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 59
rs 9.2
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Unit;
4
5
use Tests\TestCase;
6
use Illuminate\Foundation\Testing\WithFaker;
7
use Illuminate\Foundation\Testing\RefreshDatabase;
8
use Illuminate\Support\Facades\Lang;
9
10
use App\Models\Applicant;
11
use App\Models\Criteria;
12
use App\Models\Lookup\Department;
13
use App\Models\JobPoster;
14
use App\Models\JobPosterKeyTask;
15
use App\Models\JobPosterQuestion;
16
use App\Models\Lookup\LanguageRequirement;
17
use App\Models\Manager;
18
use App\Models\Lookup\Province;
19
use App\Models\Lookup\SecurityClearance;
20
use Doctrine\Common\Cache\VoidCache;
21
22
class JobControllerTest extends TestCase
23
{
24
    use RefreshDatabase;
25
26
    /**
27
     * Run parent setup and provide reusable factories.
28
     *
29
     * @return void
30
     */
31
    protected function setUp() : void
32
    {
33
        parent::setUp();
34
35
        $this->faker = \Faker\Factory::create();
36
        $this->faker_fr = \Faker\Factory::create('fr_FR');
37
38
        $this->manager = factory(Manager::class)->create();
39
        $this->jobPoster = factory(JobPoster::class)
40
            ->states('unpublished')
41
            ->create([
42
                'manager_id' => $this->manager->id
43
            ]);
44
45
        $this->otherManager = factory(Manager::class)->create();
46
        $this->otherJobPoster = factory(JobPoster::class)
47
            ->states('unpublished')
48
            ->create([
49
                'manager_id' => $this->otherManager->id
50
            ]);
51
52
        $this->publishedJob = factory(JobPoster::class)->states('published')->create();
53
    }
54
55
    /**
56
     * Ensure an unauthorized user receives the correct job poster view.
57
     *
58
     * @return void
59
     */
60
    public function testGuestSingleView() : void
61
    {
62
        $response = $this->get('jobs/' . $this->publishedJob->id);
63
        $response->assertStatus(200);
64
        $response->assertSee(Lang::get('applicant/job_post')['apply']['login_link_title']);
65
    }
66
67
    /**
68
     * Ensure an authorized applicant receives the correct job poster view.
69
     *
70
     * @return void
71
     */
72
    public function testApplicantSingleView() : void
73
    {
74
        $applicant = factory(Applicant::class)->create();
75
76
        $response = $this->actingAs($applicant->user)
77
            ->get('jobs/' . $this->publishedJob->id);
78
        $response->assertStatus(200);
79
        $response->assertSee(Lang::get('applicant/job_post')['apply']['apply_link_title']);
80
    }
81
82
    /**
83
     * Ensure a manager can view their index page.
84
     *
85
     * @return void
86
     */
87
    public function testManagerIndexView() : void
88
    {
89
        $response = $this->actingAs($this->manager->user)
90
            ->get('manager/jobs');
91
        $response->assertStatus(200);
92
93
        $response->assertSee('<h3>' . $this->jobPoster->title . '</h3>');
94
        $response->assertDontSee('<h3>' . $this->otherJobPoster->title . '</h3>');
95
    }
96
97
    /**
98
     * Ensure a manager can view the create Job Poster form.
99
     *
100
     * @return void
101
     */
102
    public function testManagerCreateView() : void
103
    {
104
        $response = $this->actingAs($this->manager->user)
105
            ->get('manager/jobs/create');
106
        $response->assertStatus(200);
107
108
        $response->assertSee('<h2 class="heading--01">' . Lang::get('manager/job_create')['title'] . '</h2>');
109
        $response->assertViewIs('manager.job_create');
110
111
        $response->assertSee(Lang::get('manager/job_create', [], 'en')['questions']['00']);
112
        $response->assertSee(Lang::get('manager/job_create', [], 'fr')['questions']['00']);
113
    }
114
115
    /**
116
     * Ensure a manager can create a Job Poster.
117
     *
118
     * @return void
119
     */
120
    public function testManagerCreate() : void
121
    {
122
        $newJob = [
123
            'term_qty' => $this->faker->numberBetween(1, 4),
124
            'salary_min' => $this->faker->numberBetween(60000, 80000),
125
            'salary_max' => $this->faker->numberBetween(80000, 100000),
126
            'noc' => $this->faker->numberBetween(1, 9999),
127
            'classification' => $this->faker->regexify('[A-Z]{2}-0[1-5]'),
128
            'manager_id' => $this->manager->id,
129
            'published' => false,
130
            'remote_work_allowed' => $this->faker->boolean(50),
131
            'open_date' => $this->faker->date('Y-m-d', strtotime('+1 day')),
132
            'open_time' => $this->faker->time(),
133
            'close_date' => $this->faker->date('Y-m-d', strtotime('+2 weeks')),
134
            'close_time' => $this->faker->time(),
135
            'start_date_time' => $this->faker->date('Y-m-d', strtotime('+2 weeks')) . ' ' . $this->faker->time(),
136
            'security_clearance' => SecurityClearance::inRandomOrder()->first()->id,
137
            'language_requirement' => LanguageRequirement::inRandomOrder()->first()->id,
138
            'department' => Department::inRandomOrder()->first()->id,
139
            'province' => Province::inRandomOrder()->first()->id,
140
            'city' => $this->faker->city,
141
            'title' => [
142
                'en' => $this->faker->word,
143
                'fr' => $this->faker_fr->word
144
            ],
145
            'impact' => [
146
                'en' => $this->faker->paragraphs(
147
                    2,
148
                    true
149
                ),
150
                'fr' => $this->faker_fr->paragraphs(
151
                    2,
152
                    true
153
                )
154
            ],
155
            'branch' => [
156
                'en' => $this->faker->word,
157
                'fr' => $this->faker_fr->word
158
            ],
159
            'division' => [
160
                'en' => $this->faker->word,
161
                'fr' => $this->faker_fr->word
162
            ],
163
            'education' => [
164
                'en' => $this->faker->sentence(),
165
                'fr' => $this->faker_fr->sentence()
166
            ],
167
            'submit' => '',
168
        ];
169
170
        $dbValues = array_slice($newJob, 0, 8);
171
172
        $response = $this->followingRedirects()
173
            ->actingAs($this->manager->user)
174
            ->post('manager/jobs/', $newJob);
175
        $response->assertStatus(200);
176
        $response->assertViewIs('applicant.job_post');
177
        $this->assertDatabaseHas('job_posters', $dbValues);
178
        $response->assertSee(Lang::get('applicant/job_post')['apply']['edit_link_title']);
179
    }
180
181
    /**
182
     * Ensure a manager can edit an unpublished Job Poster they created.
183
     *
184
     * @return void
185
     */
186
    public function testManagerEditView() : void
187
    {
188
        $response = $this->actingAs($this->manager->user)
189
            ->get('manager/jobs/' . $this->jobPoster->id . '/edit');
190
191
        $response->assertStatus(200);
192
        $response->assertViewIs('manager.job_create');
193
        // Check for a handful of properties
194
        $response->assertSee($this->jobPoster->city);
195
        $response->assertSee($this->jobPoster->education);
196
        $response->assertSee($this->jobPoster->title);
197
        $response->assertSee($this->jobPoster->impact);
198
        $response->assertSee($this->jobPoster->branch);
199
        $response->assertSee($this->jobPoster->division);
200
        $response->assertSee($this->jobPoster->education);
201
    }
202
203
        /**
204
     * Ensure a manager cannot edit a published Job Poster they created.
205
     *
206
     * @return void
207
     */
208
    public function testManagerCanNotEditViewPublished() : void
209
    {
210
        $response = $this->actingAs($this->manager->user)
211
            ->get('manager/jobs/' . $this->publishedJob->id . '/edit');
212
213
        $response->assertStatus(500);
214
    }
215
}
216