GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 138d30...5011e7 )
by Андрей
07:49
created

WorkflowDescriptorContext::beforeScenario()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
7
use Behat\Behat\Context\Context;
8
use Behat\Behat\Context\SnippetAcceptingContext;
9
use OldTown\Workflow\Loader\AbstractDescriptor;
10
use Behat\Gherkin\Node\PyStringNode;
11
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
12
use Behat\Gherkin\Node\ScenarioInterface;
13
use Behat\Behat\Hook\Scope\AfterStepScope;
14
use Behat\Behat\Tester\Result\ExecutedStepResult;
15
use Behat\Gherkin\Node\TableNode;
16
use OldTown\Workflow\Loader\WriteXmlInterface;
17
18
19
/**
20
 * Defines application features from the specific context.
21
 */
22
class WorkflowDescriptorContext implements Context, SnippetAcceptingContext
23
{
24
    /**
25
     *
26
     * @var string
27
     */
28
    protected $workflowDescriptorNamespace = 'OldTown\Workflow\Loader';
29
30
    /**
31
     * Последний созданный дескриптор
32
     *
33
     * @var AbstractDescriptor
34
     */
35
    protected $lastCreatedDescriptor;
36
37
    /**
38
     * @var
39
     */
40
    protected $currentScenario;
41
42
    /**
43
     * @Given Create descriptor :nameDescriptor
44
     *
45
     * @param $nameDescriptor
46
     *
47
     * @return AbstractDescriptor
48
     *
49
     * @throws \RuntimeException
50
     */
51
    public function createDescriptor($nameDescriptor)
52
    {
53
        try {
54
            $descriptor = $this->factoryDescriptor($nameDescriptor);
55
56
            return $descriptor;
57
        } catch (\Exception $e) {
58
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
59
        }
60
    }
61
62
63
    /**
64
     * @Given Create descriptor :nameDescriptor based on xml:
65
     *
66
     * @param string       $nameDescriptor
67
     * @param PyStringNode $xml
68
     *
69
     * @return AbstractDescriptor
70
     * @throws \RuntimeException
71
     */
72
    public function createDescriptorByNameBasedOnXml($nameDescriptor, PyStringNode $xml)
73
    {
74
        $useXmlErrors = libxml_use_internal_errors();
75
        try {
76
            libxml_use_internal_errors(true);
77
            libxml_clear_errors();
78
79
            $xmlDoc = new \DOMDocument();
80
            $xmlDoc->loadXML($xml->getRaw());
81
82
            $libxmlGetLastError = libxml_get_last_error();
83
            if ($libxmlGetLastError instanceof \LibXMLError) {
84
                throw new \RuntimeException($libxmlGetLastError->message, $libxmlGetLastError->code);
85
            }
86
87
            $descriptor = $this->factoryDescriptor($nameDescriptor, $xmlDoc->firstChild);
0 ignored issues
show
Documentation introduced by
$xmlDoc->firstChild is of type object<DOMNode>, but the function expects a null|object<DOMElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
89
            libxml_use_internal_errors($useXmlErrors);
90
91
            return $descriptor;
92
        } catch (\Exception $e) {
93
            libxml_clear_errors();
94
            libxml_use_internal_errors($useXmlErrors);
95
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
96
        }
97
    }
98
99
100
    /**
101
     * @When Create descriptor :nameDescriptor based on xml. I expect exception with the text :expectedExceptionMessage. Xml source:
102
     *
103
     * @param string       $nameDescriptor
104
     * @param string       $expectedExceptionMessage
105
     * @param PyStringNode $xml
106
     *
107
     * @return AbstractDescriptor
108
     */
109
    public function createDescriptorByNameBasedOnXmlAndExpectToGetAnExceptionMessage($nameDescriptor, $expectedExceptionMessage, PyStringNode $xml)
110
    {
111
        $actualExceptionMessage = null;
112
        try {
113
            $this->createDescriptorByNameBasedOnXml($nameDescriptor, $xml);
114
        } catch (\Exception $e) {
115
            $actualExceptionMessage = $e->getMessage();
116
        }
117
118
        PHPUnit_Framework_Assert::assertEquals($expectedExceptionMessage, $actualExceptionMessage);
119
    }
120
121
    /**
122
     * @Then Call a method descriptor :nameMethod, I get the value of :expectedResult
123
     *
124
     * @param $nameMethod
125
     * @param $expectedResult
126
     *
127
     * @throws \RuntimeException
128
     */
129
    public function callAMethodDescriptorIGetTheValueOf($nameMethod, $expectedResult)
130
    {
131
        try {
132
            $descriptor = $this->getLastCreatedDescriptor();
133
            $r = new \ReflectionObject($descriptor);
134
135
            if (!$r->hasMethod($nameMethod)) {
136
                $errMsg = "Method {$nameMethod}  does not exist";
137
                throw new \InvalidArgumentException($errMsg);
138
            }
139
140
            $actualValue = $r->getMethod($nameMethod)->invoke($descriptor);
141
142
            $errMsg = sprintf(
143
                "Bug with attribute of \"%s\". Expected value: %s. Actual value: %s",
144
                $nameMethod,
145
                $expectedResult,
146
                $actualValue
147
            );
148
149
            PHPUnit_Framework_Assert::assertEquals($expectedResult, $actualValue, $errMsg);
150
        } catch (\Exception $e) {
151
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
152
        }
153
    }
154
155
156
    /**
157
     * @Transform /^\(.+?\).+?$/
158
     *
159
     * @param $expectedResult
160
     *
161
     * @return mixed
162
     * @throws \RuntimeException
163
     */
164
    public function intelligentTransformArgument($expectedResult)
165
    {
166
        $outputArray = [];
167
        preg_match_all('/^\((.+?)\)(.+?)$/', $expectedResult, $outputArray);
168
169
170
        if (3 !== count($outputArray)) {
171
            return $expectedResult;
172
        }
173
174 View Code Duplication
        if (!(is_array($outputArray[1]) && 1 === count($outputArray[1]))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
175
            return $expectedResult;
176
        }
177
178
179 View Code Duplication
        if (!(is_array($outputArray[2]) && 1 === count($outputArray[2]))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
180
            return $expectedResult;
181
        }
182
183
        $originalType = $outputArray[1][0];
184
        $type = strtolower($originalType);
185
186
        $value = $outputArray[2][0];
187
188
        $result = $value;
189
        switch ($type) {
190
            case 'boolean':
191
            case 'bool': {
192
                $prepareValue = trim($result);
193
                $prepareValue = strtolower($prepareValue);
194
195
                $falseStrings = [
196
                    ''      => '',
197
                    'false' => 'false',
198
                    '0'     => '0',
199
                ];
200
201
                $result = !array_key_exists($prepareValue, $falseStrings);
202
203
                break;
204
            }
205
            case 'null': {
206
                $result = '(null)null' === $expectedResult ? null : $expectedResult;
207
                break;
208
            }
209
            case '\\domdocument':
210
            case 'domdocument': {
211
                if ('(DOMDocument)domDocument' === $expectedResult) {
212
                    $result = new \DOMDocument();
213
                }
214
215
                break;
216
            }
217
            default: {
218
219
            }
220
        }
221
222
        return $result;
223
    }
224
225
    /**
226
     * @When Call a method descriptor :nameMethod. The arguments of the method:
227
     *
228
     * @param           $nameMethod
229
     * @param TableNode $table
230
     *
231
     * @throws RuntimeException
232
     */
233
    public function callAMethodDescriptorTheArgumentsOfTheMethod($nameMethod, TableNode $table)
234
    {
235
        try {
236
            $descriptor = $this->getLastCreatedDescriptor();
237
            $r = new \ReflectionObject($descriptor);
238
239
            if (!$r->hasMethod($nameMethod)) {
240
                $errMsg = "Method {$nameMethod}  does not exist";
241
                throw new \InvalidArgumentException($errMsg);
242
            }
243
244
            $rows = $table->getHash();
245
            if (1 !== count($rows)) {
246
                $errMsg = 'Incorrect arguments';
247
                throw new \InvalidArgumentException($errMsg);
248
            }
249
250
            $args = $rows[0];
251
252
            $transformArg = [];
253
            foreach ($args as $index => $arg) {
254
                $transformArg[$index] = $this->intelligentTransformArgument($arg);
255
            }
256
257
            $r->getMethod($nameMethod)->invokeArgs($descriptor, $transformArg);
258
        } catch (\Exception $e) {
259
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
260
        }
261
    }
262
263
264
    /**
265
     * @When Call a method descriptor :nameMethod. I expect to get an exception message :expectedExceptionMessage. The arguments of the method:
266
     *
267
     * @param $nameMethod
268
     * @param $expectedExceptionMessage
269
     * @param TableNode $table
270
     *
271
     */
272
    public function callAMethodDescriptorIExpectToGetAnExceptionMessageTheArgumentsOfTheMethod($nameMethod, $expectedExceptionMessage, TableNode $table)
273
    {
274
        $actualExceptionMessage = null;
275
        try {
276
            $descriptor = $this->getLastCreatedDescriptor();
277
            $r = new \ReflectionObject($descriptor);
278
279
            if (!$r->hasMethod($nameMethod)) {
280
                $errMsg = "Method {$nameMethod}  does not exist";
281
                throw new \InvalidArgumentException($errMsg);
282
            }
283
284
            $rows = $table->getHash();
285
            if (1 !== count($rows)) {
286
                $errMsg = 'Incorrect arguments';
287
                throw new \InvalidArgumentException($errMsg);
288
            }
289
290
            $args = $rows[0];
291
292
            $transformArg = [];
293
            foreach ($args as $index => $arg) {
294
                $transformArg[$index] = $this->intelligentTransformArgument($arg);
295
            }
296
297
            $r->getMethod($nameMethod)->invokeArgs($descriptor, $transformArg);
298
        } catch (\Exception $e) {
299
            $actualExceptionMessage = $e->getMessage();
300
        }
301
302
        PHPUnit_Framework_Assert::assertEquals($expectedExceptionMessage, $actualExceptionMessage);
303
    }
304
305
    /**
306
     * @Then Call a method descriptor :nameMethod. I expect to get an exception :expectedException
307
     *
308
     * @param string $nameMethod
309
     * @param string $expectedException
310
     */
311 View Code Duplication
    public function callAMethodDescriptorIExpectToGetAnException($nameMethod, $expectedException)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
312
    {
313
        $actualException = null;
314
        try {
315
            $descriptor = $this->getLastCreatedDescriptor();
316
            $r = new \ReflectionObject($descriptor);
317
318
            if (!$r->hasMethod($nameMethod)) {
319
                $errMsg = "Method {$nameMethod}  does not exist";
320
                throw new \InvalidArgumentException($errMsg);
321
            }
322
323
            $r->getMethod($nameMethod)->invoke($descriptor);
324
        } catch (\Exception $e) {
325
            $actualException = $e;
326
        }
327
328
        PHPUnit_Framework_Assert::assertInstanceOf($expectedException, $actualException);
329
    }
330
331
    /**
332
     * @Then Call a method descriptor :nameMethod. I expect to get an exception message :expectedExceptionMessage
333
     *
334
     * @param $nameMethod
335
     * @param $expectedExceptionMessage
336
     */
337 View Code Duplication
    public function callAMethodDescriptorIExpectToGetAnExceptionMessage($nameMethod, $expectedExceptionMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
338
    {
339
        $actualExceptionMessage = null;
340
        try {
341
            $descriptor = $this->getLastCreatedDescriptor();
342
            $r = new \ReflectionObject($descriptor);
343
344
            if (!$r->hasMethod($nameMethod)) {
345
                $errMsg = "Method {$nameMethod}  does not exist";
346
                throw new \InvalidArgumentException($errMsg);
347
            }
348
349
            $r->getMethod($nameMethod)->invoke($descriptor);
350
        } catch (\Exception $e) {
351
            $actualExceptionMessage = $e->getMessage();
352
        }
353
354
        PHPUnit_Framework_Assert::assertEquals($expectedExceptionMessage, $actualExceptionMessage);
355
    }
356
357
358
359
360
    /**
361
     * @Then Call a method descriptor :nameMethod, I get the value of :expectedResult. The arguments of the method:
362
     *
363
     * @param string    $nameMethod
364
     * @param string    $expectedResult
365
     * @param TableNode $table
366
     *
367
     * @throws \RuntimeException
368
     *
369
     */
370
    public function callAMethodDescriptorIGetTheValueOfTheArgumentsOfTheMethod($nameMethod, $expectedResult, TableNode $table)
371
    {
372
        try {
373
            $descriptor = $this->getLastCreatedDescriptor();
374
            $r = new \ReflectionObject($descriptor);
375
376
            if (!$r->hasMethod($nameMethod)) {
377
                $errMsg = "Method {$nameMethod}  does not exist";
378
                throw new \InvalidArgumentException($errMsg);
379
            }
380
381
            $rows = $table->getHash();
382
            if (1 !== count($rows)) {
383
                $errMsg = 'Incorrect arguments';
384
                throw new \InvalidArgumentException($errMsg);
385
            }
386
387
            $args = $rows[0];
388
389
            $transformArg = [];
390
            foreach ($args as $index => $arg) {
391
                $transformArg[$index] = $this->intelligentTransformArgument($arg);
392
            }
393
394
395
            $actualValue = $r->getMethod($nameMethod)->invokeArgs($descriptor, $transformArg);
396
397
            $errMsg = sprintf(
398
                "Bug with attribute of \"variable-name\". Expected value: %s. Actual value: %s",
399
                $expectedResult,
400
                $actualValue
401
            );
402
403
            PHPUnit_Framework_Assert::assertEquals($expectedResult, $actualValue, $errMsg);
404
        } catch (\Exception $e) {
405
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
406
        }
407
    }
408
409
    /**
410
     * @When Call a method descriptor :nameMethod
411
     *
412
     * @param $nameMethod
413
     *
414
     * @throws RuntimeException
415
     */
416
    public function callAMethodDescriptor($nameMethod)
417
    {
418
        try {
419
            $descriptor = $this->getLastCreatedDescriptor();
420
            $r = new \ReflectionObject($descriptor);
421
422
            if (!$r->hasMethod($nameMethod)) {
423
                $errMsg = "Method {$nameMethod}  does not exist";
424
                throw new \InvalidArgumentException($errMsg);
425
            }
426
427
            $r->getMethod($nameMethod)->invoke($descriptor);
428
        } catch (\Exception $e) {
429
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
430
        }
431
    }
432
433
434
    /**
435
     * @Then I save to descriptor xml. Not DomDocument. Compare with xml:
436
     *
437
     * @param PyStringNode $expectedXmlNode
438
     *
439
     * @throws \RuntimeException
440
     *
441
     */
442
    public function iSaveToDescriptorXmlNotDomDocumentCompareWithXml(PyStringNode $expectedXmlNode)
443
    {
444
        try {
445
            $descriptor = $this->getLastCreatedDescriptor();
446
            if (!$descriptor instanceof WriteXmlInterface) {
447
                $errMsg = 'Descriptor not implement WriteXmlInterface';
448
                throw new \RuntimeException($errMsg);
449
            }
450
451
            $result = $descriptor->writeXml();
452
453 View Code Duplication
            if ($result instanceof \DOMDocument) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
454
                $actualXml = $result->saveXML();
455
            } elseif ($result instanceof \DOMElement) {
456
                $actualXml = $result->ownerDocument->saveXML($result);
457
            } else {
458
                $errMsg = 'Incorrect result writeXml';
459
                throw new \RuntimeException($errMsg);
460
            }
461
462
            $expectedXml = $expectedXmlNode->getRaw();
463
464
            PHPUnit_Framework_Assert::assertXmlStringEqualsXmlString($expectedXml, $actualXml);
465
        } catch (\Exception $e) {
466
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
467
        }
468
    }
469
470
471
    /**
472
     * @Then     I save to descriptor xml. Compare with xml:
473
     *
474
     * @param PyStringNode $expectedXmlNode
475
     *
476
     * @throws \RuntimeException
477
     *
478
     */
479
    public function iSaveToDescriptorXmlCompareWithXml(PyStringNode $expectedXmlNode)
480
    {
481
        try {
482
            $dom = new \DOMDocument();
483
            $dom->encoding = 'UTF-8';
484
            $dom->xmlVersion = '1.0';
485
            $dom->formatOutput = true;
486
487
            $descriptor = $this->getLastCreatedDescriptor();
488
            if (!$descriptor instanceof WriteXmlInterface) {
489
                $errMsg = 'Descriptor not implement WriteXmlInterface';
490
                throw new \RuntimeException($errMsg);
491
            }
492
493
            $result = $descriptor->writeXml($dom);
494
495 View Code Duplication
            if ($result instanceof \DOMDocument) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
496
                $actualXml = $result->saveXML();
497
            } elseif ($result instanceof \DOMElement) {
498
                $actualXml = $result->ownerDocument->saveXML($result);
499
            } else {
500
                $errMsg = 'Incorrect result writeXml';
501
                throw new \RuntimeException($errMsg);
502
            }
503
504
            $expectedXml = $expectedXmlNode->getRaw();
505
506
            PHPUnit_Framework_Assert::assertXmlStringEqualsXmlString($expectedXml, $actualXml);
507
        } catch (\Exception $e) {
508
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
509
        }
510
    }
511
512
    /**
513
     * @Then I save to descriptor xml. I expect to get an exception :expectedException
514
     *
515
     * @param string $expectedException
516
     */
517 View Code Duplication
    public function iSaveToDescriptorXmlIExpectToGetAnException($expectedException)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
518
    {
519
        $actualException = null;
520
        try {
521
            $dom = new \DOMDocument();
522
            $dom->encoding = 'UTF-8';
523
            $dom->xmlVersion = '1.0';
524
            $dom->formatOutput = true;
525
526
            $descriptor = $this->getLastCreatedDescriptor();
527
            if (!$descriptor instanceof WriteXmlInterface) {
528
                $errMsg = 'Descriptor not implement WriteXmlInterface';
529
                throw new \RuntimeException($errMsg);
530
            }
531
532
            $descriptor->writeXml($dom);
533
        } catch (\Exception $e) {
534
            $actualException = $e;
535
        }
536
537
        PHPUnit_Framework_Assert::assertInstanceOf($expectedException, $actualException);
538
    }
539
540
    /**
541
     * @Then     I save to descriptor xml. I expect to get an exception message :expectedException
542
     *
543
     * @param $expectedExceptionMessage
544
     *
545
     */
546 View Code Duplication
    public function iSaveToDescriptorXmlIExpectToGetAnExceptionMessage($expectedExceptionMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
547
    {
548
        $actualExceptionMessage = null;
549
        try {
550
            $dom = new \DOMDocument();
551
            $dom->encoding = 'UTF-8';
552
            $dom->xmlVersion = '1.0';
553
            $dom->formatOutput = true;
554
555
            $descriptor = $this->getLastCreatedDescriptor();
556
            if (!$descriptor instanceof WriteXmlInterface) {
557
                $errMsg = 'Descriptor not implement WriteXmlInterface';
558
                throw new \RuntimeException($errMsg);
559
            }
560
561
            $descriptor->writeXml($dom);
562
        } catch (\Exception $e) {
563
            $actualExceptionMessage = $e->getMessage();
564
        }
565
566
        PHPUnit_Framework_Assert::assertEquals($expectedExceptionMessage, $actualExceptionMessage);
567
    }
568
569
570
571
    /**
572
     * @Then I validated descriptor. I expect to get an exception message :expectedExceptionMessage
573
     *
574
     * @param $expectedExceptionMessage
575
     *
576
     */
577 View Code Duplication
    public function iValidatedDescriptorIExpectToGetAnExceptionMessage($expectedExceptionMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
578
    {
579
        $actualExceptionMessage = null;
580
        try {
581
            $descriptor = $this->getLastCreatedDescriptor();
582
            if (!method_exists($descriptor, 'validate')) {
583
                $errMsg = 'Descriptor does not support validation';
584
                throw new \RuntimeException($errMsg);
585
            }
586
587
            call_user_func([$descriptor, 'validate']);
588
        } catch (\Exception $e) {
589
            $actualExceptionMessage = $e->getMessage();
590
        }
591
592
        PHPUnit_Framework_Assert::assertEquals($expectedExceptionMessage, $actualExceptionMessage);
593
    }
594
595
596
    /**
597
     * @Then I validated descriptor. I expect to get an exception :expectedException
598
     *
599
     * @param string $expectedException
600
     */
601 View Code Duplication
    public function iValidatedDescriptorIExpectToGetAnException($expectedException)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
602
    {
603
        $actualException = null;
604
        try {
605
            $descriptor = $this->getLastCreatedDescriptor();
606
            if (!method_exists($descriptor, 'validate')) {
607
                $errMsg = 'Descriptor does not support validation';
608
                throw new \RuntimeException($errMsg);
609
            }
610
611
            call_user_func([$descriptor, 'validate']);
612
        } catch (\Exception $e) {
613
            $actualException = $e;
614
        }
615
616
        PHPUnit_Framework_Assert::assertInstanceOf($expectedException, $actualException);
617
    }
618
619
620
    /**
621
     * @BeforeScenario @workflowDescriptor
622
     */
623
    public function beforeScenarioWithTagWorkflowDescriptor()
624
    {
625
        $this->lastCreatedDescriptor = null;
626
    }
627
628
    /**
629
     * @AfterStep
630
     *
631
     * @param AfterStepScope $scope
632
     */
633
    public function afterStepWithTagWorkflowDescriptor(AfterStepScope $scope)
634
    {
635
        if ($this->currentScenario instanceof ScenarioInterface && $this->currentScenario->hasTag('workflowDescriptor')) {
636
            $result = $scope->getTestResult();
637
            if ($result instanceof ExecutedStepResult) {
638
                $descriptor = $result->getCallResult()->getReturn();
639
                if ($descriptor instanceof AbstractDescriptor) {
640
                    $this->lastCreatedDescriptor = $descriptor;
641
                }
642
            }
643
        }
644
    }
645
646
    /**
647
     * Возвращает последний созданный дескриптор
648
     *
649
     * @return AbstractDescriptor
650
     *
651
     * @throws \RuntimeException
652
     */
653
    protected function getLastCreatedDescriptor()
654
    {
655
        if (!$this->lastCreatedDescriptor instanceof AbstractDescriptor) {
656
            $errMsg = 'Descriptor does not exist';
657
            throw new \RuntimeException($errMsg);
658
        }
659
660
        return $this->lastCreatedDescriptor;
661
    }
662
663
664
    /**
665
     * @BeforeScenario
666
     *
667
     * @param BeforeScenarioScope $scope
668
     */
669
    public function beforeScenario(BeforeScenarioScope $scope)
670
    {
671
        $this->currentScenario = $scope->getScenario();
672
    }
673
674
675
    /**
676
     * @AfterScenario
677
     */
678
    public function afterScenario()
679
    {
680
        $this->currentScenario = null;
681
    }
682
683
    /**
684
     * Наймспейс в котором находятся дескрипторы Workflow
685
     *
686
     * @return string
687
     */
688
    public function getWorkflowDescriptorNamespace()
689
    {
690
        return $this->workflowDescriptorNamespace;
691
    }
692
693
    /**
694
     * Фабрика по созданию дескрипторов
695
     *
696
     * @param string     $name
697
     *
698
     * @param DOMElement $element
699
     *
700
     * @return AbstractDescriptor
701
     * @throws RuntimeException
702
     */
703
    protected function factoryDescriptor($name, DOMElement $element = null)
704
    {
705
        $ns = $this->getWorkflowDescriptorNamespace();
706
        $class = "{$ns}\\{$name}";
707
708
        if (!class_exists($class)) {
709
            $errMsg = "Class not found {$class}";
710
            throw new \RuntimeException($errMsg);
711
        }
712
713
        $r = new \ReflectionClass($class);
714
        if (null === $element) {
715
            $descriptor = $r->newInstance();
716
        } else {
717
            $descriptor = $r->newInstanceArgs([
718
                $element
719
            ]);
720
        }
721
722
        if (!$descriptor instanceof AbstractDescriptor) {
723
            $errMsg = 'Descriptor not instance of AbstractDescriptor';
724
            throw new \RuntimeException($errMsg);
725
        }
726
727
        return $descriptor;
728
    }
729
730
    /**
731
     * @Given Get the descriptor using the method of :methodName
732
     *
733
     * @param $methodName
734
     *
735
     * @return AbstractDescriptor
736
     * @throws RuntimeException
737
     */
738
    public function getTheDescriptorUsingTheMethodOf($methodName)
739
    {
740
        try {
741
            $descriptor = $this->getLastCreatedDescriptor();
742
            if (!method_exists($descriptor, $methodName)) {
743
                $errMsg = "Descriptor does not support method {$methodName}";
744
                throw new \RuntimeException($errMsg);
745
            }
746
747
            $descriptors = call_user_func([$descriptor, $methodName]);
748
749
            $targetDescriptor = $descriptors;
750 View Code Duplication
            if ((is_array($descriptors) || $descriptors instanceof \Traversable) && 1 === count($descriptors)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
751
                $iterator = is_array($descriptors) ? $descriptors : iterator_to_array($descriptors);
752
                reset($iterator);
753
                $targetDescriptor = current($iterator);
754
            }
755
756
            if (!$targetDescriptor instanceof AbstractDescriptor) {
757
                $errMsg = 'Descriptor not instance of AbstractDescriptor';
758
                throw new \RuntimeException($errMsg);
759
            }
760
761
            return $targetDescriptor;
762
        } catch (\Exception $e) {
763
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
764
        }
765
    }
766
767
768
    /**
769
     * @When Get the descriptor using the method of :methodName. The arguments of the method:
770
     *
771
     * @param           $methodName
772
     * @param TableNode $table
773
     *
774
     * @return mixed
775
     * @throws \RuntimeException
776
     */
777
    public function getTheDescriptorUsingTheMethodOfTheArgumentsOfTheMethod($methodName, TableNode $table)
778
    {
779
        try {
780
            $descriptor = $this->getLastCreatedDescriptor();
781
782
783
            $r = new \ReflectionObject($descriptor);
784
785
            if (!$r->hasMethod($methodName)) {
786
                $errMsg = "Method {$methodName}  does not exist";
787
                throw new \InvalidArgumentException($errMsg);
788
            }
789
790
            $rows = $table->getHash();
791
            if (1 !== count($rows)) {
792
                $errMsg = 'Incorrect arguments';
793
                throw new \InvalidArgumentException($errMsg);
794
            }
795
796
            $args = $rows[0];
797
798
            $transformArg = [];
799
            foreach ($args as $index => $arg) {
800
                $transformArg[$index] = $this->intelligentTransformArgument($arg);
801
            }
802
803
            $descriptors = $r->getMethod($methodName)->invokeArgs($descriptor, $transformArg);
804
805
            $targetDescriptor = $descriptors;
806 View Code Duplication
            if ((is_array($descriptors) || $descriptors instanceof \Traversable) && 1 === count($descriptors)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
807
                $iterator = is_array($descriptors) ? $descriptors : iterator_to_array($descriptors);
808
                reset($iterator);
809
                $targetDescriptor = current($iterator);
810
            }
811
812
            if (!$targetDescriptor instanceof AbstractDescriptor) {
813
                $errMsg = 'Descriptor not instance of AbstractDescriptor';
814
                throw new \RuntimeException($errMsg);
815
            }
816
817
            return $targetDescriptor;
818
        } catch (\Exception $e) {
819
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
820
        }
821
    }
822
}
823