SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Locker's Avatar
    Join Date
    May 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unit Testing and logicalOr()

    I'm just looking into unit testing with PHPUnit and understand basic assertion tests such as:

    PHP Code:
    $this->assertInternalType('int'$expected_int_value);
    $this->assetEquals($one_value$another_value); 
    However, I want to be able to make an assertion where a value can equal an internal type of string OR null.

    I've seen there's a logicalOr() method available but aren't sure how to use it.

    If someone could point me in the right direction that would be great!

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    How about:

    PHP Code:
    switch($value){
        
    is_int($value) == 1:
            
    $this->assertInternalType('int'$value);
            break;
        
    is_string($value) == 1
            
    $this->assertInternalType('string'$value);
            break;
       empty(
    $value):
           
    $this->assertNull($value);
       default:
           throw new 
    exception('Value should be either a string, integer or NULL');

    You would want to replace the thrown exception with the PHPUnit method that can manually add a failure. Something like this should work.
    ictus==""

  3. #3
    SitePoint Enthusiast Locker's Avatar
    Join Date
    May 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ServerStorm View Post
    You would want to replace the thrown exception with the PHPUnit method that can manually add a failure. Something like this should work.
    Is this even possible? I can't find a method to manually add a failure...

    Perhaps something like this would work:

    PHP Code:
    if (is_string($value) || is_null($value)) {
        
    $result TRUE;
    } else {
        
    $result FALSE;
    }

    $this->assertTrue($result); 
    My only concern is whether this is an acceptable way of doing things or whether PHPUnit's built-in logical operators can/should be used some how?

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi,

    I use SimpleTest and it has such an assertion; although the way you have written it is OK as it does test the value and should fail if it is neither of your two filtered types.

    I don't know how full-in you've gone regarding unit testing, but here is a decent link on the TDD driven approach, and it also provides benefits to this process. And there is this link on PHPUnit's site regarding behavioral development a modern approach that addresses the fact that XP (Extreme) programming still forces a developer to not think in terms of specifications.
    ictus==""

  5. #5
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Locker View Post
    Is this even possible? I can't find a method to manually add a failure...

    Perhaps something like this would work:

    PHP Code:
    if (is_string($value) || is_null($value)) {
        
    $result TRUE;
    } else {
        
    $result FALSE;
    }

    $this->assertTrue($result); 
    My only concern is whether this is an acceptable way of doing things or whether PHPUnit's built-in logical operators can/should be used some how?

    There's a $this->fail() method in phpunit:

    http://www.phpunit.de/manual/3.0/en/...leneck-methods

    There's also a logicalOR method:

    http://www.phpunit.de/manual/3.0/en/...es.constraints

  6. #6
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am also using phpunit. I'm a little new and have run into a tough problem.

    I am testing a function in a class whose constructor I absolutely have to disable because it news up an object that I can't access from the test. So, I use the code:

    PHP Code:
    public function testConnect(){
        
    $mock $this->getMockBuilder('FSCall')
            ->
    setMethods(array('setOpened'))
            ->
    disableAutoload()
            ->
    disableOriginalConstructor()
            ->
    getMock();
        
    $call = new FSCall;
        
    $call->connect();

    ...where 'setOpened' is a local function that function connect calls, but I don't need it to actually call the function for the unit test.

    The problem is that phpUnit returns the error "Call to undefined method FSCall::connect()". The function is in the FSCall class. Now I can "fix" the error by doing a...

    PHP Code:
    $mock->expects($this->any())->method('connects'
    ...but then the connects function doesn't actually get called.

    What am I misunderstanding here?

    Any help will be greatly appreciated.

    G


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •