[Extension] wUnit

This is the topic for wUnit extension.

If you have questions or found a bug let me know.

Thank you for this extension!

I have made all under the instructions, but no result.

Output:


c:\...\tests>phpunit unit

PHPUnit 3.6.7 by Sebastian Bergmann.


Configuration read from C:\...\tests\phpunit.xml


..


Time: 3 seconds, Memory: 8.75Mb


OK (2 tests, 4 assertions)


c:\...\tests>phpunit functional


c:\...\tests>phpunit functional/AccessControllerTest.php


c:\...\tests>

Help please!

It occurs seems when request with a redirect.

Could you show me your bootstrap.php for tests ? I guess you use Yii::createWebApplication() instead of WUnit::createWebApplication().

If you create application with Yii::createWebApplication() and try to call action with redirect you will get no output, because redirect() contains ‘exit;’, which terminate script execution at all, no matter test it or not. That’s why we had to override some method with exit by WUnit::createWebApplication().

You’re right. My mistake.

Nevertheless, requests are processed not as I expect.

For example logout:


public function testIndex()

{

        $client = static::createClient();


        $crawler = $client->request('GET', '/logout');


        $this->assertTrue($crawler->filter('html:contains("ERP-system")')->count() > 0);

}

Output:


AccessControllerTest::testIndex()

Failed asserting that false is true.

But login:


public function testIndex()

{

        $client = static::createClient();


        $crawler = $client->request('GET', '/login');


        $this->assertTrue($crawler->filter('html:contains("ERP-system")')->count() > 0);

}

All right.

Hum… What about:


public function testIndex()

{

        $client = static::createClient();

        $crawler = $client->request('GET', '/logout');

        $crawler = $client->followRedirect();

        $this->assertTrue($crawler->filter('html:contains("ERP-system")')->count() > 0);

}

New version released.

Changelog

0.2.1

  • Fixed issues in unix-like systems

  • Enabled followRedirects by default (as per documentation)

0.2

  • Added classes autoloader so XPathExpr error should not appear any more

  • Fixed exception with YiiExitApplication

  • Modified manual to specify printerFile into phpunit.xml

Levka,

Please, update extension with latest version, and your wouldn’t require followRedirect() calls.

Here is unit tests for wunit related to login/logout:




class LoginTest extends \PHPUnit_Framework_TestCase

{


	protected static $wunit;


	public static function setUpBeforeClass()

	{

		static::$wunit = new WUnit();

		static::$wunit->init();

	}


	public function testLoginPage()

	{

		$client = static::$wunit->createClient();

		$crawler = $client->request('GET', '/test/login');




		$this->assertTrue($crawler->filter('html:contains("Please fill out the following form with your login credentials")')->count() > 0);

	}


	public function testLoginForm()

	{

		$client = static::$wunit->createClient();


		$usermame = "demo";

		$password = "demo";

		

		$crawler = $client->request('GET', '/test/index');

		$this->assertTrue($crawler->filter('html:contains("You are not loged in")')->count() > 0);


		$this->assertTrue($this->login($client, $usermame, $password)->filter('html:contains("You are loged in as demo")')->count() > 0);


		$client->back();

		$crawler = $client->request('GET', '/test/index');

		$this->assertTrue($crawler->filter('html:contains("You are loged in as demo")')->count() > 0);


		$crawler = $client->request('GET', '/test/logout');


		$this->assertTrue($crawler->filter('html:contains("You are not loged in")')->count() > 0);

		$crawler = $client->request('GET', '/test/index');


		$this->assertTrue($this->login($client, 'demo', 'demo')->filter('html:contains("You are loged in as demo")')->count() > 0);

	}


	private function login($client, $username, $password){

		$crawler = $client->request('GET', '/test/login');


		$form = $crawler->selectButton('Login')->form();

		$form['LoginForm[username]'] = $username;

		$form['LoginForm[password]'] = $password;

		$form['LoginForm[rememberMe]']->tick();


		return $client->submit($form);

	}


}



That’s great!

No problems now with autoredirect.

Does may this extension analyze messageboxes?

Thanks for this great extension.

Can you add this comment in top of function createClient (WUnitTestCase.php) ?




    /**

     * Create HttpKernel Client

     * @return WUnit\HttpKernel\Client

     */



This will allow netbeans to give autocomplete hint when you typed something like this:




   $client = static::createClient();

   $crawler = $client->

//                     ^^^ autocomplete hints appear



Edit: add this issues in github

Hello I have recently downloaded this but facing an issue. When I am running the following code


public function testIndex()

    {

        $client = static::createClient();

        $crawler = $client->request('GET', '/users/default/login',array(), array(),array('PHP_SELF'=>'/bma/app/index.php'));


        $this->assertTrue($crawler->filter('html:contains("Congratulations!")')->count() > 0);

    }

I am getting the error


1) SiteControllerTest::testIndex

CHttpException: The system is unable to find the requested action "login".


G:\xampp\htdocs\bma\framework\web\CController.php:483

G:\xampp\htdocs\bma\framework\web\CController.php:270

G:\xampp\htdocs\bma\framework\web\CWebApplication.php:282

G:\xampp\htdocs\bma\framework\web\CWebApplication.php:141

G:\xampp\htdocs\bma\app\protected\extensions\wunit\Http\YiiKernel.php:28

G:\xampp\htdocs\bma\app\protected\extensions\wunit\HttpKernel\Client.php:60

G:\xampp\htdocs\bma\app\protected\extensions\wunit\BrowserKit\Client.php:265

G:\xampp\htdocs\bma\app\protected\extensions\wunit\BrowserKit\Client.php:418

G:\xampp\htdocs\bma\app\protected\extensions\wunit\BrowserKit\Client.php:275

G:\xampp\htdocs\bma\app\protected\tests\functional\SiteControllerTest.php:8


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

Please let me know where i am wrong. My application is at path http://localhost/bma/app/index.php.

Thanks for your extension. It’s working well. However, I would like to know how to request a real DELETE or PUT Http (not simulated by POST method)?