Tagphp

PHP: Testing protected methods in CakePHP3

One of the things I recently had to deal with – PHP unit testing protected methods of the class in CakePHP 3. Few seconds of checking StackOverflow brought a nice and elegant way of checking protected methods using ReflectionClasses.

Sebastian Bergmann has a complete guide how to check non-public functionality of the classes in his archieves.

Here’s a short sample of the code using CakePHP3:

<?php
use Search\Model\Table\SaveSearchTable;
use Cake\TestSuite\TestCase;
class SaveSearchTableTest extends TestCase
{
public function setUp()
{
$this->SavedSearches = \Cake\ORM\TableRegistry::get('SavedSearches');
}
public function testProtectedMethod()
{
$methodName = 'protectedMethod';
$reflectionClass = new \ReflectionClass('\Search\Model\Table\SaveSearchesTable');
$method = $reflectionClass->getMethod($methodName);
$methodResult = $method->invokeArgs( $this->SavedSearches, ['arg1', ['arg2']]);
$this->assertNotEmpty($methodResult);
}
}

WP-CLI becomes part of make.wordpress.org

WP-CLI reached 1.x stable release. WordPress decided to make it as part of make.wordpress.org tools.

Great news from the WordPress community and those who spends most of their time in the console. No more weird sftpd/vsftpd installations.

It might come handy with composer bundle as we do it in Qobo for WordPress project templates.

PSR-7: storage-less sessions

Some measurements on Arrays vs ArrayObjects found among gist users. Just going to bring it here, in case gist dissappears.

# php -v
PHP 5.3.6 (cli) (built: Mar 17 2011 20:58:15)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
655040

# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $o = new ArrayObject; $o->name = "Adam";  $o->age = 35;  $s[] = $o;} echo memory_get_peak_usage(); ' | php
887984

# time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $o = new ArrayObject; $o->name = "Adam";  $o->age = 35;  $s[] = $o;} echo memory_get_peak_usage(); ' | php
61010784
real    0m1.448s
user    0m1.208s
sys     0m0.231s

# time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
33647944
real    0m0.525s
user    0m0.429s
sys     0m0.092s

We can conclude the following at least in PHP 5.6:

  • Winner for Speed: Arrays are faster than objects (with undefined or defined properties)
  • Winner for Memory: Objects with defined properties use less memory, 50%+ less than objects with undefined properties, only slightly less than Arrays

Ordered Fastest to Slowest in Speed for 100,000 items:

  • 0.107s – Arrays
  • 0.163s – Objects with undefined properties
  • 1.190s – Objects with defined properties

Ordered Least to Most Memory Usage for 1,000 items:

  • ~583kb – Objects with defined properties
  • ~807kb – Arrays
  • ~1.1mb – Objects with undefined properties

Chronos: CakePHP replacement for carbon

Chronos is a drop-in library replacement for nesbot/carbon.

It provides immutable date/datetime objects. Immutable objects help us to ensure, that DateTime objects aren’t accidentally modified.

<?php
require 'vendor/autoload.php';
use Cake\Chronos\Chronos;
printf("Now: %s", Chronos::now());
?>