| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Behatch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Mink\Exception\ExpectationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | trait Asserter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     protected function not(callable $callbable, $errorMessage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |             $callbable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         throw new ExpectationException($errorMessage, $this->getSession()->getDriver()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function assert($test, $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if ($test === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             throw new ExpectationException($message, $this->getSession()->getDriver()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected function assertContains($expected, $actual, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $regex   = '/' . preg_quote($expected, '/') . '/ui'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assert( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             preg_match($regex, $actual) > 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $message ?: "The string '$expected' was not found." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected function assertNotContains($expected, $actual, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $message = $message ?: "The string '$expected' was found."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->not(function () use($expected, $actual) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $this->assertContains($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         }, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function assertCount($expected, array $elements, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assert( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             intval($expected) === count($elements), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function assertEquals($expected, $actual, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assert( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $expected == $actual, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $message ?: "The element '$actual' is not equal to '$expected'" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     protected function assertSame($expected, $actual, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assert( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $expected === $actual, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $message ?: "The element '$actual' is not equal to '$expected'" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function assertArrayHasKey($key, $array, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assert( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             isset($array[$key]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $message ?: "The array has no key '$key'" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function assertArrayNotHasKey($key, $array, $message = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $message = $message ?: "The array has key '$key'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->not(function () use($key, $array) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->assertArrayHasKey($key, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         }, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected function assertTrue($value, $message = 'The value is false') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->assert($value, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     protected function assertFalse($value, $message = 'The value is true') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->not(function () use($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $this->assertTrue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         }, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |     protected function assertNull($value, $message = 'The value is not null') | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         $this->assert(null === $value, $message); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                        
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.