Link to home
Start Free TrialLog in
Avatar of Adegoroye Owolabi
Adegoroye OwolabiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Does anyone know phpunit test

Need someone to show me how to properly make good use of  phpunit testing
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

This is a very open ended question. Well may I suggests you to watch the videos to have better understanding. Most frameworks use phpunit and it is also integrated in every PHP IDE, meaning it is popular.

https://www.youtube.com/watch?v=4BXpi7056RM
https://www.youtube.com/watch?v=-9YVcssCACI

These will give you an idea to start with the things. good luck
Avatar of Adegoroye Owolabi

ASKER

so how do I make this pass the test I am getting this  message
Failed asserting that exception of type "Exception" is thrown.
Tester.php
/* @test*/ 
public function NotExistingUserName()
	    {
	        $Getuser 	= new Mynamespace\Getuser("Ade");
	        $this->setExpectedException('Exception');
	       	$NameUser 		= $Getuser->getFullName('Owolabi');
	    }

Open in new window

working.php
namespace Mynamespace; 
use \Exception; 

	class Getuser
	{
		public function __construct($user)
		{
	try{
     if(!$user)
	throw new \Exception('This an error message');	 
 } catch(Exception $e){
     return $e->getMessage();
 }		
		 $this->UserName=$user;
		}

		public function getFullName($lastName)
		{
			$UserName=$this->UserName;
			
            return ($UserName." ".$lastName);	
			
			
		}

Open in new window


Test Result

Failed asserting that exception of type "Exception" is thrown.

I am wondering how to pass the test Any help
In a bit of hurry now. You cannot set the expected exception.  Please refer this for a workaround.

public function testDivByZero()
{
    try {
        // Fyi you don't need to do an assert test here, as we are only testing the exception, so just make the call
        $result = $this->object->div(1,0);
    } catch (Exception $e) {
        if ('Exception' === get_class($e)) {
            throw new Test_Exception($e->getMessage(), $e->getCode());
        }
    }
 }

 // Test_Exception.php
 class Test_Exception extends Exception
 {
     public function __construct($message = null, $code = 0, Exception $previous = null)
     {
         parent::__construct($message, $code, $previous);
     }
 }

Open in new window


r
Thank you but I want it in similar to my code
/* @test*/ 
public function NotExistingUserName()
	    {
			
			try 
			{
				$Getuser 	= new Mynamespace\Getuser("Ade");
				$NameUser = $Getuser->getFullName('Owolabi');
			}	        
			catch (Exception $e) 
			{
			if ('Exception' === get_class($e)) {
				throw new Test_Exception($e->getMessage(), $e->getCode());
			}
	       	
	    }

Open in new window

Thank you for your responses . But I want the test to be written like this, don't reconstruct it.

/* @test*/ 
public function NotExistingUserName()
	    {
	        $Getuser 	= new Mynamespace\Getuser("Ade");
	        $this->setExpectedException('Exception');
	       	$NameUser 		= $Getuser->getFullName('Owolabi');
	    }

Open in new window


Let say this how the test is written what do I need to do to make it pass. on working.php
/* @test*/
public function NotExistingUserName()
          {
              $Getuser       = new Mynamespace\Getuser("Ade");
              $this->setExpectedException(ExpectedException::class);
                   $NameUser             = $Getuser->getFullName('Owolabi');
          }

Refer this best practice for more - https://thephp.cc/news/2016/02/questioning-phpunit-best-practices
@Author - a feedback will be appreciated. :)
still the test result is showing
Failed asserting that exception of type "ExpectedException" is thrown.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.