1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Yawik\Behat; |
11
|
|
|
|
12
|
|
|
use Auth\Entity\Status; |
13
|
|
|
use Auth\Entity\User; |
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
16
|
|
|
use Behat\Gherkin\Node\TableNode; |
17
|
|
|
use Doctrine\Common\Util\Inflector; |
18
|
|
|
use Documents\UserRepository; |
19
|
|
|
use Geo\Service\Photon; |
20
|
|
|
use Jobs\Entity\Classifications; |
21
|
|
|
use Jobs\Entity\Job; |
22
|
|
|
use Jobs\Entity\Location; |
23
|
|
|
use Jobs\Entity\StatusInterface; |
24
|
|
|
use Jobs\Repository\Categories as CategoriesRepo; |
25
|
|
|
use Jobs\Repository\Job as JobRepository; |
26
|
|
|
use Zend\Json\Json; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class JobContext |
30
|
|
|
* |
31
|
|
|
* @author Anthonius Munthi <[email protected]> |
32
|
|
|
* @package Yawik\Behat |
33
|
|
|
*/ |
34
|
|
|
class JobContext implements Context |
35
|
|
|
{ |
36
|
|
|
use CommonContextTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Select2Context |
40
|
|
|
*/ |
41
|
|
|
private $select2Context; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Job |
45
|
|
|
*/ |
46
|
|
|
private $currentJob; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var JobRepository |
50
|
|
|
*/ |
51
|
|
|
static private $jobRepo; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param User $user |
55
|
|
|
*/ |
56
|
|
|
static public function removeJobByUser(User $user) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$repo = static::$jobRepo; |
|
|
|
|
59
|
|
|
$results = $repo->findBy(['user' => $user]); |
60
|
|
|
foreach($results as $result){ |
61
|
|
|
$repo->remove($result,true); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @BeforeScenario |
67
|
|
|
* |
68
|
|
|
* @param BeforeScenarioScope $scope |
69
|
|
|
*/ |
70
|
|
|
public function beforeScenario(BeforeScenarioScope $scope) |
71
|
|
|
{ |
72
|
|
|
$this->select2Context = $scope->getEnvironment()->getContext(Select2Context::class); |
|
|
|
|
73
|
|
|
if(is_null(static::$jobRepo)){ |
|
|
|
|
74
|
|
|
$this->gatherContexts($scope); |
75
|
|
|
static::$jobRepo = $this->getJobRepository(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @Given I go to job board page |
81
|
|
|
*/ |
82
|
|
|
public function iGoToJobBoardPage() |
83
|
|
|
{ |
84
|
|
|
$this->visit('/en/jobboard'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Given I go to create job page |
89
|
|
|
*/ |
90
|
|
|
public function iGoToMyOrganizationPage() |
91
|
|
|
{ |
92
|
|
|
$this->visit('/en/jobs/edit'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @Given I go to job overview page |
97
|
|
|
*/ |
98
|
|
|
public function iGoToJobOverviewPage() |
99
|
|
|
{ |
100
|
|
|
$this->visit('/en/jobs'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @Given I go to edit job draft with title :jobTitle |
105
|
|
|
* @param $jobTitle |
106
|
|
|
* @throws \Exception when job is not found |
107
|
|
|
*/ |
108
|
|
|
public function iGoToEditJobWithTitle($jobTitle) |
109
|
|
|
{ |
110
|
|
|
$job = $this->getJobRepository()->findOneBy(['title' => $jobTitle]); |
111
|
|
|
if(!$job instanceof Job){ |
112
|
|
|
throw new \Exception(sprintf('Job with title "%s" is not found',$jobTitle)); |
113
|
|
|
} |
114
|
|
|
$this->currentJob = $job; |
115
|
|
|
$url = '/en/jobs/edit?id='.$job->getId(); |
116
|
|
|
$this->visit($url); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @Given I don't have any classification data |
121
|
|
|
*/ |
122
|
|
|
public function iDonTHaveAnyClassificationData() |
123
|
|
|
{ |
124
|
|
|
$this->currentJob->setClassifications(new Classifications()); |
125
|
|
|
$this->getJobRepository()->store($this->currentJob); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @When I don't have any posted job |
130
|
|
|
*/ |
131
|
|
|
public function iDonTHaveAnyPostedJob() |
132
|
|
|
{ |
133
|
|
|
/* @var $jobRepository JobRepository */ |
134
|
|
|
/* @var $job Job */ |
135
|
|
|
$user = $this->getCurrentUser(); |
136
|
|
|
$jobRepository = $this->getJobRepository(); |
137
|
|
|
$results = $jobRepository->getUserJobs($user->getId()); |
138
|
|
|
foreach($results as $job){ |
139
|
|
|
$jobRepository->remove($job,true); |
140
|
|
|
} |
141
|
|
|
$this->currentJob = null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @When I fill job location search with :search and choose :choice |
146
|
|
|
* |
147
|
|
|
*/ |
148
|
|
|
public function iFillJobLocationAndChoose($search,$choice) |
149
|
|
|
{ |
150
|
|
|
$select2 = $this->select2Context; |
151
|
|
|
$select2->iFillInSelect2FieldWith('jobBase[geoLocation]',$search,$choice); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @When I choose :value from :field |
156
|
|
|
*/ |
157
|
|
|
public function iJobClassificationSelect($value,$field) |
158
|
|
|
{ |
159
|
|
|
$field = Inflector::camelize($field); |
160
|
|
|
|
161
|
|
|
$mapSelect2 = [ |
162
|
|
|
'professions' => '#classifications-professions-span .select2-container', |
163
|
|
|
'industries' => '#classifications-industries-span .select2-container', |
164
|
|
|
'employmentTypes' => '#classifications-employmentTypes-span .select2-container', |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
$mapMultiple = [ |
168
|
|
|
'professions' => "select#classifications-professions", |
169
|
|
|
'industries' => "select#classifications-industries", |
170
|
|
|
'employmentTypes' => "select#classifications-employmentTypes", |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
if(!isset($mapSelect2[$field])){ |
174
|
|
|
throw new \Exception('Undefined field selection value "'.$field.'"'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$multipleField = $mapMultiple[$field]; |
178
|
|
|
$page = $this->minkContext->getSession()->getPage(); |
179
|
|
|
$element = $page->find('css',$mapMultiple[$field]); |
180
|
|
|
if(!is_null($element) && $element->getAttribute('multiple')=='multiple'){ |
181
|
|
|
$this->minkContext->selectOption($value,$multipleField); |
182
|
|
|
}else{ |
183
|
|
|
$locator = $mapSelect2[$field]; |
184
|
|
|
$this->select2Context->iFillInSelect2Field($locator,$value); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return JobRepository |
190
|
|
|
*/ |
191
|
|
|
public function getJobRepository() |
192
|
|
|
{ |
193
|
|
|
return $this->getRepository('Jobs/Job'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return CategoriesRepo |
198
|
|
|
*/ |
199
|
|
|
public function getCategoriesRepository() |
200
|
|
|
{ |
201
|
|
|
return $this->getRepository('Jobs/Category'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @When I have a :status job with the following: |
206
|
|
|
* @param TableNode $fields |
207
|
|
|
*/ |
208
|
|
|
public function iHaveAJobWithTheFollowing($status,TableNode $fields) |
209
|
|
|
{ |
210
|
|
|
$normalizedField = [ |
211
|
|
|
'template' => 'modern', |
212
|
|
|
]; |
213
|
|
|
foreach($fields->getRowsHash() as $field => $value){ |
214
|
|
|
$field = Inflector::camelize($field); |
215
|
|
|
if($field == 'professions' || $field == 'industries'){ |
216
|
|
|
$value = explode(',',$value); |
217
|
|
|
} |
218
|
|
|
$normalizedField[$field] = $value; |
219
|
|
|
} |
220
|
|
|
$jobRepo = $this->getJobRepository(); |
221
|
|
|
$job = $jobRepo->findOneBy(['title' => $normalizedField['title']]); |
222
|
|
|
if(!$job instanceof Job){ |
223
|
|
|
$job = new Job(); |
224
|
|
|
$job->setTitle($normalizedField['title']); |
225
|
|
|
} |
226
|
|
|
if(isset($normalizedField['user'])){ |
227
|
|
|
/* @var $userRepo UserRepository */ |
228
|
|
|
$userRepo = $this->getRepository('Auth\Entity\User'); |
229
|
|
|
$user = $userRepo->findOneBy(['login' => $normalizedField['user']]); |
230
|
|
|
if($user instanceof User){ |
231
|
|
|
$job->setUser($user); |
232
|
|
|
$job->setOrganization($user->getOrganization()->getOrganization()); |
233
|
|
|
}else{ |
234
|
|
|
throw new \Exception('There is no user with this login:"'.$normalizedField['user'.'"']); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if($status == 'draft'){ |
239
|
|
|
$job->setIsDraft(true); |
240
|
|
|
}elseif($status == 'published'){ |
241
|
|
|
$job->setIsDraft(false); |
242
|
|
|
$job->setDatePublishStart(new \DateTime()); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
$job->setStatus(Status::ACTIVE); |
245
|
|
|
|
246
|
|
|
if(isset($normalizedField['location'])){ |
247
|
|
|
$this->setLocation($job,$normalizedField['location']); |
248
|
|
|
} |
249
|
|
|
if(isset($normalizedField['companyName'])){ |
|
|
|
|
250
|
|
|
//$job->setCompany($normalizedField['companyName']); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
if(isset($normalizedField['professions'])){ |
253
|
|
|
$this->addProfessions($job,$normalizedField['professions']); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if(isset($normalizedField['industries'])){ |
257
|
|
|
$this->addIndustries($job,$normalizedField['industries']); |
258
|
|
|
} |
259
|
|
|
if(isset($normalizedField['employmentTypes'])){ |
260
|
|
|
$types = $this->getCategories([$normalizedField['employmentTypes']]); |
261
|
|
|
$type = array_shift($types); |
262
|
|
|
$values = $job->getClassifications()->getEmploymentTypes()->getValues(); |
263
|
|
|
if(!is_array($values) || !in_array($type,$values)){ |
264
|
|
|
$job->getClassifications()->getEmploymentTypes()->getItems()->add($type); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$jobRepo->store($job); |
269
|
|
|
$this->currentJob = $job; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
private function setLocation(Job $job, $term) |
273
|
|
|
{ |
274
|
|
|
/* @var $client Photon */ |
275
|
|
|
$client = $this->coreContext->getServiceManager()->get('Geo/Client'); |
276
|
|
|
$result = $client->queryOne($term); |
277
|
|
|
$location = new Location(); |
278
|
|
|
$serialized = Json::encode($result); |
279
|
|
|
$location->fromString($serialized); |
280
|
|
|
|
281
|
|
|
$locations = $job->getLocations(); |
282
|
|
|
if(count($locations)){ |
283
|
|
|
$locations->clear(); |
284
|
|
|
} |
285
|
|
|
$job->getLocations()->add($location); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
View Code Duplication |
private function addProfessions(Job &$job,$terms) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$professions = $this->getCategories($terms); |
291
|
|
|
foreach($professions as $profession){ |
292
|
|
|
$values = $job->getClassifications()->getProfessions()->getValues(); |
293
|
|
|
if(!is_array($values) || !in_array($profession,$values)){ |
294
|
|
|
$job->getClassifications()->getProfessions()->getItems()->add($profession); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
View Code Duplication |
private function addIndustries(Job &$job, $terms) |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$industries = $this->getCategories($terms); |
302
|
|
|
foreach($industries as $industry){ |
303
|
|
|
$values = $job->getClassifications()->getIndustries()->getValues(); |
304
|
|
|
if(!is_array($values) || !in_array($industry,$values)){ |
305
|
|
|
$job->getClassifications()->getIndustries()->getItems()->add($industry); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param array $categories |
312
|
|
|
* |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
private function getCategories(array $categories) |
316
|
|
|
{ |
317
|
|
|
$catRepo = $this->getCategoriesRepository(); |
318
|
|
|
|
319
|
|
|
// get a professions |
320
|
|
|
$qb = $catRepo->createQueryBuilder() |
321
|
|
|
->field('name')->in($categories) |
322
|
|
|
->getQuery() |
323
|
|
|
; |
324
|
|
|
$results = $qb->execute(); |
325
|
|
|
return $results->toArray(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return Job |
331
|
|
|
*/ |
332
|
|
|
private function getCurrentUserJobDraft($jobTitle) |
|
|
|
|
333
|
|
|
{ |
334
|
|
|
$repo = $this->getJobRepository(); |
335
|
|
|
$user = $this->getCurrentUser(); |
336
|
|
|
|
337
|
|
|
$job = $repo->findDraft($user); |
338
|
|
|
|
339
|
|
|
if(is_null($job)){ |
340
|
|
|
$job = new Job(); |
341
|
|
|
$job |
342
|
|
|
->setUser($user) |
343
|
|
|
->setOrganization($user->getOrganization()->getOrganization()) |
344
|
|
|
->setStatus(StatusInterface::CREATED) |
345
|
|
|
; |
346
|
|
|
$job->setIsDraft(true); |
347
|
|
|
} |
348
|
|
|
$job->setTitle($jobTitle); |
349
|
|
|
$repo->store($job); |
350
|
|
|
return $job; |
351
|
|
|
} |
352
|
|
|
} |