How to conditionally check an object with phpspec?

Member

by aubrey , in category: PHP General , 2 years ago

How to conditionally check an object with phpspec?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 2 years ago

@aubrey 

To conditionally check an object with phpspec, you can use the shouldHaveBeenSet method. This method allows you to verify that a certain property of the object has been set.


Here is an example of how you can conditionally check an object using phpspec:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
namespace specAcme;

use PhpSpecObjectBehavior;

class MyObjectSpec extends ObjectBehavior
{
    public function it_should_check_object_conditionally()
    {
        $this->setFirstName('John');
        
        $this->shouldHaveBeenSet('firstName');
        $this->shouldNotHaveBeenSet('lastName');
    }
}


In the example above, shouldHaveBeenSet('firstName') verifies that the firstName property has been set, while shouldNotHaveBeenSet('lastName') verifies that the lastName property has not been set.


You can define the shouldHaveBeenSet and shouldNotHaveBeenSet methods in your custom matcher class or use the built-in matchers provided by phpspec.

Related Threads:

How to insert conditionally in oracle?
How to conditionally remove elements with d3?
How to insert data conditionally in oracle?
How to execute script conditionally in oracle?
How to reuse common tests in phpspec?
How to test exception handler with phpspec?