Using PHPUnit: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[Category:Unit Testing]] [[Category:PHP]] [[Category:Web Development]]
[[Category:Unit Testing]] [[Category:PHP]] [[Category:Web Development]]
== Testing URLs ==
== Configuring unit tests in PHPStorm ==


=== Selenium Server ===
=== Composer ===


==== Installation ====
Add PHPUnit to composer: <ref>[https://phpunit.de/getting-started/phpunit-8.html Getting Started With PHPUnit 8] - PHPUnit Documentation</ref>


Installation instructions can be found [https://phpunit.de/manual/current/en/selenium.html here] under "Installation".
<syntaxhighlight lang="javascript">
 
  "require-dev": {
Make sure Composer is installed: <ref>[https://getcomposer.org/doc/00-intro.md Getting Started] - Comoser website</ref>
    "phpunit/phpunit": "^8"
 
  }
<syntaxhighlight lang="bash">
$ composer -v
/* or */
$ php composer.phar -V
</syntaxhighlight>
</syntaxhighlight>


Add The PHPUnit Selenium dependency to `composer.json` in the project root directory:
Update installed composer dependencies:


<syntaxhighlight lang="javascript">
<pre>
{
php composer.phar update
"require-dev":
</pre>
{
phpunit/phpunit-selenium": ">=1.2"
}
}
</syntaxhighlight>


Then install with either
=== Creating unit tests ===


<syntaxhighlight lang="bash">
Test class names start with "Test" and test function names start with "test".
$ composer install
/* or */
$ composer update
</syntaxhighlight>


(To uninstall un-needed dependencies:)
Test classes extend `PHPUnit\Framework\TestCase`.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="php">
$ composer remove [LIBRARY_NAME]/[PACKAGE_NAME] --update-with-dependencies
<?php
</syntaxhighlight>
namespace Littled\Tests\Account;


==== Running the test suite ====
use Littled\Account\Address;
use PHPUnit\Framework\TestCase;


<syntaxhighlight lang="bash">
class AddressTest extends TestCase
$ java -har /usr/bin/local/selenium-server-standalone-2.x.x.jar
{
    public function testInitialValues()
    {
        $addr = new Address();
        self::assertInstanceOf('Littled\Request\IntegerInput', $addr->id);
        self::assertEquals(100, $addr->address2->sizeLimit);
        /* etc... */
    }
}
</syntaxhighlight>
</syntaxhighlight>


== Installation/Configuration errors and fixes ==
=== Running unit tests ===
 
=== Paths on Windows and OSX ===
 
Any reference to the file system path to web resources in the development environment would be Windows paths on the web server, but if unit testing locally on a Mac, the path will be different, e.g. `/Volumes/shared_dir/path/to/project/`
 
=== mysqli no such file or directory ===
 
Run a unit test that makes a database connection and receive


<syntaxhighlight lang="text">
Right click on a TestCase class or a directory containing TestCase classes and choose '''Run''' ( <kbd>&#8963;</kbd> <kbd>&#8679;</kbd> <kbd>R</kbd> )
mysqli::mysqli(): (HY000/2002): No such file or directory
</syntaxhighlight>


First Google results suggested making sure that the `mysql` extension was compiled into PHP. I don't think this was the actual issue.
=== Generating unit test classes ===


I installed PHP using homebrew, according to [http://justinhileman.info/article/reinstalling-php-on-mac-os-x/ these instructions]. This version of PHP comes with MySQL compiled into it, but I think that PHP that comes installed on the Mac most likely does also.  
PHPStorm can be configured such that when a new class is created, a corresponding class can be added to unit tests.  


The issue ended up being that the MySQL connection was configured to use `localhost` as the host name. `locahost` for the PHP CLI resolves to something very different than when running on the development web server. <ref>[https://webdevjourney.wordpress.com/2011/04/20/warning-mysql_connect-function-mysql-connect-no-such-file-or-directory/ <nowiki>Warning: mysql_connect() [function.mysql-connect]: No such file or directory</nowiki>] - Devster Journey blog</ref>
* Mark the class directory as the root for the app code.
* Mark the directory containing the unit tests as a tests directory (right click > Mark Directory As > Test Sources Root)
* Create a new PHP class file.
* Right click on the file and create the unit test.


== Notes ==
== Notes ==

Latest revision as of 17:50, 6 September 2019

Configuring unit tests in PHPStorm[edit]

Composer[edit]

Add PHPUnit to composer: [1]

"require-dev": {
    "phpunit/phpunit": "^8"
  }

Update installed composer dependencies:

php composer.phar update

Creating unit tests[edit]

Test class names start with "Test" and test function names start with "test".

Test classes extend PHPUnit\Framework\TestCase.

<?php
namespace Littled\Tests\Account;

use Littled\Account\Address;
use PHPUnit\Framework\TestCase;

class AddressTest extends TestCase
{
    public function testInitialValues()
    {
        $addr = new Address();
        self::assertInstanceOf('Littled\Request\IntegerInput', $addr->id);
        self::assertEquals(100, $addr->address2->sizeLimit);
        /* etc... */
    }
}

Running unit tests[edit]

Right click on a TestCase class or a directory containing TestCase classes and choose Run ( R )

Generating unit test classes[edit]

PHPStorm can be configured such that when a new class is created, a corresponding class can be added to unit tests.

  • Mark the class directory as the root for the app code.
  • Mark the directory containing the unit tests as a tests directory (right click > Mark Directory As > Test Sources Root)
  • Create a new PHP class file.
  • Right click on the file and create the unit test.

Notes[edit]

See also[edit]

Footnotes[edit]

  1. Getting Started With PHPUnit 8 - PHPUnit Documentation