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.

Tuesday, September 29, 2015

Git Clone and Push Using SSH with Passphrase

Open console and type

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/user_name/.ssh/id_rsa):<return>
Enter passphrase (empty for no passphrase):<type passphrase>
Enter same passphrase again:<type passphrase>
Your identification has been saved in /c/Users/user_name/.ssh/id_rsa.
Your public key has been saved in /c/Users/user_name/.ssh/id_rsa.pub.
The key fingerprint is:
**:**:**:**:**:** user_name@COMPUTERNAME

Your public key is at c:/Users/user_name/.ssh in file id_rsa.pub. Add the public key to your bitbucket account.

$ git clone git@bitbucket.org:folder_name/project_name.git
Cloning into project_name...
The authenticity of host 'bitbucket.org (131.103.20.167)' can't be established.
RSA key fingerprint is **:**:**:**:**:**.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'bitbucket.org,131.103.20.167' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/user_name/.ssh/id_rsa':
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

$ cd project_name/

modify/create/add files

$ git add .

$ git commit -m "message"
[master d72b82a] modified readme file again
 1 files changed, 1 insertions(+), 1 deletions(-)

$ git push -u origin master
Enter passphrase for key '/c/Users/user_name/.ssh/id_rsa':<type passphrase>
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@bitbucket.org:folder_name/project_name.git
   cf17705..d72b82a  master -> master
Branch master set up to track remote branch master from origin.

This is how you communicate wit SSH enabled git repo.

Sunday, September 27, 2015

Upgrade and Update Ubuntu Linux

On Ubuntu you can perform following updates:
sudo apt-get update      
sudo apt-get upgrade       
sudo apt-get dist-upgrade  
If you want install newer version of operating system for example you want to migrate from 14.04 to 14.10
sudo do-release-upgrade  
for troubleshooting visit https://help.ubuntu.com/community/Upgrades#Upgrade_policy




Tuesday, September 1, 2015

CentOS Mount USB Device and Extend Volume

To mount USB device edit /etc/fstab as root on CentOS

/dev/sdb1 /media/USBDEVICE2 ntfs-3g rw,umask=0000,defaults 0 0
/dev/sdb1 /media/USBDEVICE1 vfat rw,umask=0000,defaults 0 0 

To expand volume on CentOS after resizing in VirtualBox and GParted

[root@flo ~]# lvm vgdisplay

[root@flo ~]# lvextend -l +100%FREE /dev/mapper/vg_danube-lv_root

[root@florida ~]# df -T
Filesystem           Type  1K-blocks    Used Available Use% Mounted on
/dev/mapper/vg_danube-lv_root
                     ext4    8648080 7608976    949924  89% /
tmpfs                tmpfs    961132     152    960980   1% /dev/shm
/dev/sda1            ext4     487652   87980    374072  20% /boot

Volume still not resized.

[root@flo ~]# resize2fs  /dev/mapper/vg_danube-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_danube-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg_danube-lv_root to 4728832 (4k) blocks.
The filesystem on /dev/mapper/vg_danube-lv_root is now 4728832 blocks long.

[root@flo ~]# df -T
Filesystem           Type  1K-blocks    Used Available Use% Mounted on
/dev/mapper/vg_danube-lv_root
                     ext4   18486004 7612456  10685860  42% /
tmpfs                tmpfs    961132     304    960828   1% /dev/shm
/dev/sda1            ext4     487652   87980    374072  20% /boot

Now volume has been resized.

Friday, August 21, 2015

Install GNOME on Ubuntu 14.04

Unity slows down PC and it's very difficult to adjust to new environment. That's why I install GNOME desktop:

sudo apt-get install gnome-session-fallback

Restart PC and on login screen choose GNOME metacity session You should see desktop like this:



Move close, minimize, maximize buttons to the right

gsettings set org.gnome.desktop.wm.preferences button-layout 
':minimize,maximize,close'

Good old and familiar interface is back.

Wednesday, August 19, 2015

Git Octopus Merge

alfa and beta branches are checked out from master branch. In branch beta a file named beta.txt was created and committed. The listing shows creating and committing alfa.txt and octopus merging.

$ git checkout alfa 
Switched to branch 'alfa' 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ vim alfa.txt 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git add . 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git commit -m 'added alfa.txt' 
[alfa d30de0e] added alfa.txt 
1 file changed, 1 insertion(+) 
create mode 100644 alfa.txt 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git checkout master 
Switched to branch 'master' 
Your branch is up-to-date with 'origin/master'. 

josh@SMARTBOX ~/Documents/git/developer (master) 
$ git merge alfa beta 
Fast-forwarding to: alfa 
Trying simple merge with beta 
Merge made by the 'octopus' strategy. 
alfa.txt | 1 + 
beta.txt | 1 + 
2 files changed, 2 insertions(+) 
create mode 100644 alfa.txt 
create mode 100644 beta.txt

Files alfa.txt and beta.txt that were created in their branches are now in master branch.

Sunday, July 5, 2015

PHP Dependancy Injection

This two scripts show Motor class as dependency that is injected in Car class.

<?php
class Motor {
    public function startEngine(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(Motor $m){
        $m -> startEngine();
    }
}

$a = new Car(new Motor());
?>

and second example

<?php
class Motor {
    public function __construct(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(){
        $m = new Motor();
    }
}

$a = new Car();
?>

The point is that instancing of dependent class is not done in client but in consumer class. 

Sunday, June 28, 2015

Expand Partition, Split VMDK to Multiple Files on VirtualBox

Expand Partition

C:\Windows\system32>cd C:/Program Files/Oracle

C:\Program Files\Oracle>cd VirtualBox

C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vmdk" --resize 20000
0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage.exe: error: Resize hard disk operation for this format is not impleme
nted yet!

C:\Program Files\Oracle\VirtualBox>VBoxManage clonehd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vmdk" "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vdi" --format VDI --variant standard
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: fa9fd259-fe7b-4a84-90c2-e7e0a516c
5ad

C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vdi" --resize 20000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files\Oracle\VirtualBox>


Now you can expand partition in gparted. Right click on partition -> resize

Split VMDK Volume to multiple parts


C:\Program Files\Oracle\VirtualBox>VBoxManage.exe 
clonehd "c:\Users\UserName\VirtualBox VMs\Ubuntu\Ubuntu-disk1.vmdk" 
"c:\Users\UserName\VirtualBox VMs\Ubuntu\Ubuntu-frags.vmdk" 
--format VMDK 
--variant Split2G
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'VMDK'. UUID: bc391000

Saturday, February 21, 2015

PDO - Full Snippet

This is fully working snippet for PDO Select with parameters

<?php
$username = "user_name";
$password = "password";
$hostname = "localhost";
$dataBase = "data_base_name";
$character_set = "utf8";
$id = 8;
$slug = "string";
try {
 $conn = new PDO("mysql:host=$hostname;dbname=$dataBase;charset=$character_set", $username, $password);
 $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $conn -> prepare("SELECT * FROM table_name WHERE id = :id AND slug=:slug");
 $stmt->bindParam(":id", $id, PDO::PARAM_INT);
 $stmt->bindParam(":slug", $slug, PDO::PARAM_STR); 
 $stmt->execute();
 echo "Query has: ".$stmt -> rowCount() . " rows <hr>";
 while ($row = $stmt -> fetch()) {
  echo $row['id'] . " - " . $row['slug'] . "\n<br>";
 }
} catch(PDOException $e) {
 echo 'ERROR: ' . $e -> getMessage();
}
?>