PHPUnit and Slim Frameworks

  1. Download PhpUnit with Composer
    • Update the composer.json file
    • "require-dev": {
              "phpunit/phpunit": "5.4.*"
          }
    • Run php composer.phar update

 

2. Create file indexTest.php for testing file

session_start();
require_once 'vendor/autoload.php';
require_once 'AutoLoader.php';
$appConfig = include('configTest.php');

 

3.  Create folder ‘test’ in root app dir, and create file for test, eg: EmailTest.php

<?php
use PHPUnit\Framework\TestCase;
use \models\User\EmailModel as Email; //Any namespace on your setup

/**
 * @backupGlobals disabled
 */
final class EmailTest extends TestCase
{
    public function testCanBeCreatedFromValidEmailAddress()
    {
        $this->assertInstanceOf(
            Email::class,
            Email::fromString('user@example.com')
        );

    }

    public function testCannotBeCreatedFromInvalidEmailAddress()
    {
        $this->expectException(InvalidArgumentException::class);

        Email::fromString('invalid');
    }

    public function testCanBeUsedAsString()
    {
        $this->assertEquals(
            'user@example.com',
            Email::fromString('user@example.com')
        );
    }
}

Use annotation @backupGlobals to prevent Closure errors.

 

4. Then create file \models\User\EmailModel.php :

<?php
namespace models\User;

class EmailModel extends UserModel
{
private $email;

public function __construct($email)
{
$this->ensureIsValidEmail($email);

$this->email = $email;
}

public static function fromString($email)
{
return new self($email);
}

public function __toString()
{
return $this->email;
}

private function ensureIsValidEmail($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException(
sprintf(
'"%s" is not a valid email address',
$email
)
);
}
}
}

 

5. In terminal, run the below command:

./vendor/bin/phpunit --bootstrap indexTest.php test/EmailTest

Results:

PHPUnit 5.4.8 by Sebastian Bergmann and contributors.

... 3 / 3 (100%)

Time: 28 ms, Memory: 3.25MB

OK (3 tests, 3 assertions)

The sample of EmailTest.php and EmailModel.php is taken from PhpUnit site.

Ok Bye.

Apache2.4 Using Alias with mod_rewrite return error 404 not found

Problem:
When i configure Alias in apache,
Alias /testing/ “/var/www/test_folder/”

Then in my ‘test_folder’, i’ve .htaccess file,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Then when i run http://mydomain.com/testing,
it will return error 404 Not found

Solution:
The solution below at least is working with my environment:

Alias /testing/ “/var/www/testing/”

<Directory “/var/www/testing/”>

RewriteEngine On
RewriteBase /testing

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
AllowOverride None

</Directory>

So the trick here is by adding the,
RewriteBase /testing
AllowOverride None – set None to NOT use the htaccess file

That’s all, ok Bye 😉

Docker Permission Error in Ubuntu

If you are using Docker in the Ubuntu and receive the permission error as below:

“Got permission denied while trying to connect to the Docker daemon socket at unix …”

The solution just add the current user to the Docker group:

$sudo usermod -a -G docker $USER

Then logout / reboot your machine.

After reboot, try the command below:

$docker run hello-world

If successful, then the result shown as below:

Hello from Docker!
This message shows that your installation appears to be working correctly.

PHP Save array to database in JSON format

If you’re using class, take advantage on using the setter and getter method.

class Example
{
 public function setDisaster($value)
 {
     //convert the array to json format
     $this->potential_disaster = json_encode($value);
 }
 
 public function getDisaster()
 {
     //convert json to array format
     return json_decode($this->potential_disaster);
 }

 public function save()
 {
    $model = new Example();
    $model->save();
 }
}

$obj = new Example();
//Save the array data
$obj->disaster = array(1,2,3);
$obj->save();

//Print the array data
print_r($obj->disaster);