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('/jobboard'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Given I go to create job page |
89
|
|
|
*/ |
90
|
|
|
public function iGoToCreateJob() |
91
|
|
|
{ |
92
|
|
|
$url = $this->generateUrl('lang/jobs/manage',['action' => 'edit']); |
93
|
|
|
$this->visit($url); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Given I go to job overview page |
98
|
|
|
*/ |
99
|
|
|
public function iGoToJobOverviewPage() |
100
|
|
|
{ |
101
|
|
|
$this->visit('/jobs'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @Given I go to edit job draft with title :jobTitle |
106
|
|
|
* @param $jobTitle |
107
|
|
|
* @throws \Exception when job is not found |
108
|
|
|
*/ |
109
|
|
|
public function iGoToEditJobWithTitle($jobTitle) |
110
|
|
|
{ |
111
|
|
|
$job = $this->getJobRepository()->findOneBy(['title' => $jobTitle]); |
112
|
|
|
if(!$job instanceof Job){ |
113
|
|
|
throw new \Exception(sprintf('Job with title "%s" is not found',$jobTitle)); |
114
|
|
|
} |
115
|
|
|
$this->currentJob = $job; |
116
|
|
|
$url = $this->generateUrl('lang/jobs/manage',[ |
117
|
|
|
'id' => $job->getId() |
118
|
|
|
]); |
119
|
|
|
$this->visit($url); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @Given I don't have any classification data |
124
|
|
|
*/ |
125
|
|
|
public function iDonTHaveAnyClassificationData() |
126
|
|
|
{ |
127
|
|
|
$this->currentJob->setClassifications(new Classifications()); |
128
|
|
|
$this->getJobRepository()->store($this->currentJob); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @When I don't have any posted job |
133
|
|
|
*/ |
134
|
|
|
public function iDonTHaveAnyPostedJob() |
135
|
|
|
{ |
136
|
|
|
/* @var $jobRepository JobRepository */ |
137
|
|
|
/* @var $job Job */ |
138
|
|
|
$user = $this->getUserContext()->getCurrentUser(); |
139
|
|
|
|
140
|
|
|
$jobRepository = $this->getJobRepository(); |
141
|
|
|
$results = $jobRepository->getUserJobs($user->getId()); |
142
|
|
|
foreach($results as $job){ |
143
|
|
|
$jobRepository->remove($job,true); |
144
|
|
|
} |
145
|
|
|
$this->currentJob = null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @When I fill job location search with :search and choose :choice |
150
|
|
|
* |
151
|
|
|
*/ |
152
|
|
|
public function iFillJobLocationAndChoose($search,$choice) |
153
|
|
|
{ |
154
|
|
|
$select2 = $this->select2Context; |
155
|
|
|
$select2->iFillInSelect2FieldWith('jobBase[geoLocation]',$search,$choice); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @When I choose :value from :field |
160
|
|
|
*/ |
161
|
|
|
public function iJobClassificationSelect($value,$field) |
162
|
|
|
{ |
163
|
|
|
$field = Inflector::camelize($field); |
164
|
|
|
|
165
|
|
|
$mapSelect2 = [ |
166
|
|
|
'professions' => '#classifications-professions-span .select2-container', |
167
|
|
|
'industries' => '#classifications-industries-span .select2-container', |
168
|
|
|
'employmentTypes' => '#classifications-employmentTypes-span .select2-container', |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
$mapMultiple = [ |
172
|
|
|
'professions' => "select#classifications-professions", |
173
|
|
|
'industries' => "select#classifications-industries", |
174
|
|
|
'employmentTypes' => "select#classifications-employmentTypes", |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
if(!isset($mapSelect2[$field])){ |
178
|
|
|
throw new \Exception('Undefined field selection value "'.$field.'"'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$multipleField = $mapMultiple[$field]; |
182
|
|
|
$page = $this->minkContext->getSession()->getPage(); |
183
|
|
|
$element = $page->find('css',$mapMultiple[$field]); |
184
|
|
|
if(!is_null($element) && $element->getAttribute('multiple')=='multiple'){ |
185
|
|
|
$this->minkContext->selectOption($value,$multipleField); |
186
|
|
|
}else{ |
187
|
|
|
$locator = $mapSelect2[$field]; |
188
|
|
|
$this->select2Context->iFillInSelect2Field($locator,$value); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return JobRepository |
194
|
|
|
*/ |
195
|
|
|
public function getJobRepository() |
196
|
|
|
{ |
197
|
|
|
return $this->getRepository('Jobs/Job'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return CategoriesRepo |
202
|
|
|
*/ |
203
|
|
|
public function getCategoriesRepository() |
204
|
|
|
{ |
205
|
|
|
return $this->getRepository('Jobs/Category'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @When I have a :status job with the following: |
210
|
|
|
* @param TableNode $fields |
211
|
|
|
*/ |
212
|
|
|
public function iHaveAJobWithTheFollowing($status,TableNode $fields) |
213
|
|
|
{ |
214
|
|
|
$normalizedField = [ |
215
|
|
|
'template' => 'modern', |
216
|
|
|
]; |
217
|
|
|
foreach($fields->getRowsHash() as $field => $value){ |
218
|
|
|
$field = Inflector::camelize($field); |
219
|
|
|
if($field == 'professions' || $field == 'industries'){ |
220
|
|
|
$value = explode(',',$value); |
221
|
|
|
} |
222
|
|
|
$normalizedField[$field] = $value; |
223
|
|
|
} |
224
|
|
|
$jobRepo = $this->getJobRepository(); |
225
|
|
|
$job = $jobRepo->findOneBy(['title' => $normalizedField['title']]); |
226
|
|
|
if(!$job instanceof Job){ |
227
|
|
|
$job = new Job(); |
228
|
|
|
$job->setTitle($normalizedField['title']); |
229
|
|
|
} |
230
|
|
|
if(isset($normalizedField['user'])){ |
231
|
|
|
/* @var $userRepo UserRepository */ |
232
|
|
|
$userRepo = $this->getRepository('Auth\Entity\User'); |
233
|
|
|
$user = $userRepo->findOneBy(['login' => $normalizedField['user']]); |
234
|
|
|
if($user instanceof User){ |
235
|
|
|
$job->setUser($user); |
236
|
|
|
$job->setOrganization($user->getOrganization()->getOrganization()); |
237
|
|
|
}else{ |
238
|
|
|
throw new \Exception('There is no user with this login:"'.$normalizedField['user'.'"']); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if($status == 'draft'){ |
243
|
|
|
$job->setIsDraft(true); |
244
|
|
|
}elseif($status == 'published'){ |
245
|
|
|
$job->setIsDraft(false); |
246
|
|
|
$job->setDatePublishStart(new \DateTime()); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
$job->setStatus(Status::ACTIVE); |
249
|
|
|
|
250
|
|
|
if(isset($normalizedField['location'])){ |
251
|
|
|
$this->setLocation($job,$normalizedField['location']); |
252
|
|
|
} |
253
|
|
|
if(isset($normalizedField['companyName'])){ |
|
|
|
|
254
|
|
|
//$job->setCompany($normalizedField['companyName']); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
if(isset($normalizedField['professions'])){ |
257
|
|
|
$this->addProfessions($job,$normalizedField['professions']); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if(isset($normalizedField['industries'])){ |
261
|
|
|
$this->addIndustries($job,$normalizedField['industries']); |
262
|
|
|
} |
263
|
|
|
if(isset($normalizedField['employmentTypes'])){ |
264
|
|
|
$types = $this->getCategories([$normalizedField['employmentTypes']]); |
265
|
|
|
$type = array_shift($types); |
266
|
|
|
$values = $job->getClassifications()->getEmploymentTypes()->getValues(); |
267
|
|
|
if(!is_array($values) || !in_array($type,$values)){ |
268
|
|
|
$job->getClassifications()->getEmploymentTypes()->getItems()->add($type); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$jobRepo->store($job); |
273
|
|
|
$this->currentJob = $job; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
private function setLocation(Job $job, $term) |
277
|
|
|
{ |
278
|
|
|
/* @var $client Photon */ |
279
|
|
|
$client = $this->coreContext->getServiceManager()->get('Geo/Client'); |
280
|
|
|
$result = $client->queryOne($term); |
281
|
|
|
$location = new Location(); |
282
|
|
|
$serialized = Json::encode($result); |
283
|
|
|
$location->fromString($serialized); |
284
|
|
|
|
285
|
|
|
$locations = $job->getLocations(); |
286
|
|
|
if(count($locations)){ |
287
|
|
|
$locations->clear(); |
288
|
|
|
} |
289
|
|
|
$job->getLocations()->add($location); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
View Code Duplication |
private function addProfessions(Job &$job,$terms) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
$professions = $this->getCategories($terms); |
295
|
|
|
foreach($professions as $profession){ |
296
|
|
|
$values = $job->getClassifications()->getProfessions()->getValues(); |
297
|
|
|
if(!is_array($values) || !in_array($profession,$values)){ |
298
|
|
|
$job->getClassifications()->getProfessions()->getItems()->add($profession); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
View Code Duplication |
private function addIndustries(Job &$job, $terms) |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
$industries = $this->getCategories($terms); |
306
|
|
|
foreach($industries as $industry){ |
307
|
|
|
$values = $job->getClassifications()->getIndustries()->getValues(); |
308
|
|
|
if(!is_array($values) || !in_array($industry,$values)){ |
309
|
|
|
$job->getClassifications()->getIndustries()->getItems()->add($industry); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param array $categories |
316
|
|
|
* |
317
|
|
|
* @return mixed |
318
|
|
|
*/ |
319
|
|
|
private function getCategories(array $categories) |
320
|
|
|
{ |
321
|
|
|
$catRepo = $this->getCategoriesRepository(); |
322
|
|
|
|
323
|
|
|
// get a professions |
324
|
|
|
$qb = $catRepo->createQueryBuilder() |
325
|
|
|
->field('name')->in($categories) |
326
|
|
|
->getQuery() |
327
|
|
|
; |
328
|
|
|
$results = $qb->execute(); |
329
|
|
|
return $results->toArray(); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return Job |
335
|
|
|
*/ |
336
|
|
|
private function getCurrentUserJobDraft($jobTitle) |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
$repo = $this->getJobRepository(); |
339
|
|
|
$user = $this->getCurrentUser(); |
|
|
|
|
340
|
|
|
|
341
|
|
|
$job = $repo->findDraft($user); |
342
|
|
|
|
343
|
|
|
if(is_null($job)){ |
344
|
|
|
$job = new Job(); |
345
|
|
|
$job |
346
|
|
|
->setUser($user) |
347
|
|
|
->setOrganization($user->getOrganization()->getOrganization()) |
348
|
|
|
->setStatus(StatusInterface::CREATED) |
349
|
|
|
; |
350
|
|
|
$job->setIsDraft(true); |
351
|
|
|
} |
352
|
|
|
$job->setTitle($jobTitle); |
353
|
|
|
$repo->store($job); |
354
|
|
|
return $job; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|