Thursday, October 22, 2015

Webgrind profiling in WAMP

You can get list of all PHP functions and their arguments that were executed with Webgrind extension. In URL of page put XDEBUGPROFILE=true as shown on video.



You need to setup your php.ini like this
; XDEBUG Extension

zend_extension = "c:/wamp/bin/php/
php5.5.12/zend_ext/
php_xdebug-2.2.5-5.5-vc11-x86_64.dll"
;
[xdebug]
xdebug.remote_enable = 1
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = "c:/wamp/tmp"
xdebug.show_local_vars=0
Keep in mind that WAMP has two php.ini files. So you need co change the right one.

Friday, October 16, 2015

PHPUnit Testing in Composer Project

install PHPUnit

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

now phpunit is accessible globally

download some project from git that contains "Tests" folder and composer.json

git clone --depth=1 https://github.com/domnikl/DesignPatternsPHP.git
composer install

see if there is phpunit.xml.dist file in project root

cat phpunit.xml.dist 
phpunit bootstrap="./vendor/autoload.php" colors="true">
    <testsuites>
        <testsuite name="Design Patterns">
            <directory suffix="Test.php">Behavioral/*/Tests</directory>
            <directory suffix="Test.php">Creational/*/Tests</directory>
            <directory suffix="Test.php">More/*/Tests</directory>
            <directory suffix="Test.php">Structural/*/Tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>./vendor</directory>
        </blacklist>
    </filter>
</phpunit>

From project root run this

phpunit -vvv
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.8-0ubuntu0.16.04.2
Configuration: /var/www/html/DesignPatternsPHP/phpunit.xml.dist

................................................................. 65 / 68 ( 95%)
...                                                               68 / 68 (100%)

Time: 839 ms, Memory: 10.00MB

OK (68 tests, 208 assertions)

or you can run one test on single file also from project root

phpunit Structural/Composite/Tests/CompositeTest.php
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 103 ms, Memory: 8.00MB

OK (1 test, 1 assertion)

detailed output for each test

phpunit --testdox
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

DesignPatterns\Behavioral\ChainOfResponsibilities\Tests\Chain
 [x] Can request key in fast storage
 [x] Can request key in slow storage

DesignPatterns\Behavioral\Command\Tests\Command
 [x] Invocation
...

Inside DesignPatternsPHP/Structural/Composite/TestsCompositeTest.php you can put method like this

public function testMyMethod(){
   print __METHOD__;
   $this->assertEquals("abcd", "abc");
}

and run phpunit --verbose you'll get

1) DesignPatterns\Structural\Composite\Tests\CompositeTest::testMyMethod
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'abcd'
+'abc'

Also you'll get name of method in output in 65th test.

XDebug in Eclipse

How to debug a PHP script in eclipse like C program in Visual Studio.