Author Archives: stz184

Mock Yii2 components

The well written code is also testable code – it is loosely coupled to the other components, it follows the single responsibility principle, there are no explicit dependencies and so on. We all know the rules, but there is always an exception.

It’s pretty common situation to unit test component or controller that depends on another core Yii component – for example using \yii\web\Request to get query string or $_GET variable.

Rewriting your library in order to remove the explicit dependencies is not always option, because it may lead to massive refactoring and actually break more things than benefits.

In such as case a what I really want to do is to assign temporarily \Yii::$app->request PHPUnit Mock Class, so I can override the return values e.g.

$request = $this->getMock('\yii\web\Request', ['getUserIP', 'getUserAgent', 'getBodyParams']);

$request->expects($this->any())
 ->method('getUserIP')
 ->will($this->returnValue('127.0.0.1'));

$request->expects($this->any())
 ->method('getUserAgent')
 ->will($this->returnValue('Dummy User Agent'));

$request->expects($this->any())
 ->method('getBodyParams')
 ->will($this->returnValue([]));


\Yii::$app->set('request', $request);

Enjoy!

test

Disabling graphical login in Raspbian

If you are using Raspberry PI as headless machine, you do not need to start the graphical server at all. It is useless when Rasbian is used as server OS and disabling it will free ~40MB of RAM.
To do so, you need to edit the /etc/X11/default-display-manager file using your favorite text editor.

$ sudo vim /etc/X11/default-display-manager

Then comment out the line

#/usr/sbin/lightdm

and add a new one

/bin/false

Save the file and reboot the machine. Voila!

Update

There is a easier way of doing it…
Just run the Rasbian configuration utility

$ sudo raspi-config

In boot option menu choose:

  1. Enable Boot to Desktop/Scratch and enable Console Text.
  2. Also check SSH by going in ssh and selecting Enable or disable ssh server.
  3. After you quit, you’ll be asked to reboot, choose Yes.

 

test

Symfony 2 : Redirect to Referer

There are few methods of redirecting to referer.
Inside your controller action you can use:

return $this->redirect(
              $this->getRequest()
                   ->headers
                   ->get('referer')
           );

Alternatively you can write it like this:

 $request = $this->getRequest();
 return $this->redirect($request->server->get('HTTP_REFERER'));
test

Generate huge JSON files with custom PHP >5.3 class

Lately, I’ve been working on transitioning XML feeds to JSON format on big video site.  We generate these feeds in order to feed external search service with results. It’s similar to sitemap, but it provides more detailed information about the pages.

This task is challenging because of the following problems that need to be resolved:

  1. The feed need to represent over 500 000 database entries i.e. videos. It’s just not possible to generate huge PHP multidimensional array with more than 500 000 elements and pass it to json_encode(). Obviously, you need to generate small JSON objects (chunks) concatenated with hand-coded strings and so build the full feed.
  2. The development and production servers we use are equipped with outdated PHP version 5.3.27. That means:
    – No meaningful error messages because json_last_error_msg() function it’s not available prior PHP 5.5
    – No JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE
  3. The code should be easy to test and maintain, so it should provide meaningful debug information and error handling.

Continue reading

test

Format microtime() to provide better feedback on the time elapsed

As a PHP developer you know that it’s common task to check how long a particular class, function or procedure performs. Usually this involves measuring of the time particular code snippet takes to execute. There are lots of PHP code benchmark scripts out there, but the simplest method remains to check the difference between the start and end time. Continue reading

test

VLC does not support the audio or video format “hevc”. Unfortunately there is no way for you to fix this.

Just you have chosen a good mоvie to watch, you are seated comfortably in an armchair with pack of popcorn, clicked on your favorite media player VideoLAN and… bang! Error!

VLC does not support the audio or video format “hevc”. Unfortunately there is no way for you to fix this.

The error message above is not really correct. There is a way to fix it!

 

test

How To Configure a Mail Server Using Postfix and Dovecot on Ubuntu 14.04 LTS

This tutorial explains how to setup mail server on Ubuntu 14.04 using Postfix and Dovecot. It’s based on few other tutorials and does not pretend to show you all the best practices. I am writing this tutorial because it’s 99.9% sure that I will forget how I configured my own mail server very very soon and I have to write down all the info while it’s still fresh in my head.

Ok, let’s start to work!

Continue reading

test

How To Get The Log Instead of Just Revision Numbers by svn merge info

svn merge command is really great – it can be used to determine which revisions are already merged and which are eligible to merge from particular branch. But has one big drawback – it can output just the revision numbers, but not the corresponding log messages. So, it’s really hard to determine what has been commited actually with these revisions. Of course, you can run svn log command for the revisions in question, but what if there are, lets say, 50 commits?

You could build a list of commit messages by piping mergeinfo into the svn log command using xargs. It looks like this:

svn mergeinfo --show-revs=eligible ^/branches/version | tr "\\n" "," | xargs -i svn log -c {} ^/branches/version

To make the command little bit easier and shorter, you may consider to add an alias in your ~/.bash_aliases file as follow:

alias svnlog='tr "\\n" "," | xargs -i svn log -c {}'

Now, you can shorten the command like that:

svn mergeinfo --show-revs=eligible ^/branches/version | svnlog ^/branches/version
test