When mocking a method in PHPUnit produces a “cannot be configured” error

When mocking an object in PHPUnit testing, if you receive the following error even when the function name  is correct. For example, if mocking non-custom Doctrine Repositories (i.e. no SomeEntityRepository class) will cause:

Trying to configure method “getUser” which cannot be configured because it does not exist, has not been specified, is final, or is static

This occurs on a PHPUnit_Framework_MockObject_MockObject object when using the PHPUnit_Framework_TestCase and PHPUnit_Framework_MockObject_MockBuilder.

In the case of mocking Doctrine find calls, often this error means $methodName parameter in the getMockBuilder($methodName) function is incorrect. For example the following code is incorrect:

// Produces error
$itemRepoMock = $this

Here you need to specify Doctrine’s base EntityRepository class instead, so:
The same example:

// Fixed
$itemRepoMock = $this

As a defined ItemRepository does not exist, neither dies its find function, even though you can usually use the inferred name.


Fix a Git Commit Message Typo

If after quickly typing in a commit you notice a typo, as such:

git commit -m "this is splet wrong"

You can quickly fix this using Git’s --amend flag, as such:

  1. Type: git commit --amend
  2. By default, the vi editor is opened. press a to enter INSERT mode and make changes
  3. Press ESC to leave INSERT mode and save by typing :wq (write and quit), if you accidently made everything 10x worse, you can cancel by typing :q! (quit, ignoring changes)
  4. Your commit now has an updated commit message and you can push as normal

No more embarrassing errors highlighting non-compliance with the absurd and illogical English (or other) written language.

Doctrine findBy and findOneBy Argument 2 passed must be an instance of […], none given

Argument 2 passed to AppBundle\Entities\WrappingPaperRepository::findWrapperInfoForProduct() must be an instance ofAppBundle\Entities\Product, none given, called in .. .php

This error sometimes occurs when converting a “findBy” call located outside the Repository into a named repository function. Whilst you can of course make repo custom function arguments also use arrays of parameters, often when writing a quick database lookup this is too much hassle and we just specify them like so:

  //Repository File
  function findInfoForProduct(Product $product) {
    // ..custom logic

But this is different to how Doctrine’s findBy and findOneBy functions work – above we provide a single parameter of type product – they take an array of parameters as such:

  //Repository File
  $em->getRepository('AppBundle\Entities\Product')->findBy(['product' => $product]);

Remove the “array(” or “[” characters from your repository function call and your data retrieval service will work fine, or at least get you a bit further towards the next issue!

Doctrine convention adherence leads to much simpler entity classes, no picky join column specification. A standard bi-directional one-to-many join becomes simply:

use Doctrine\Common\Collections\ArrayCollection;

/** @Entity **/
class Product
     * @OneToMany(targetEntity="Feature", mappedBy="product")
    private $features;

    public function __construct() {
        $this->features = new ArrayCollection();

/** @Entity **/
class Feature
     * @ManyToOne(targetEntity="Product", inversedBy="features")
    private $product;

Nice and straight forward.

Of course, if you do want to specify the join column manually, it would be:

/** @Entity **/
class Feature
     * @ManyToOne(targetEntity="Product", inversedBy="features")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
    private $product;

The getters & setters can be auto-generated with the following calls from the server’s command line:

app/console doctrine:generate:entities AppBundle:Product
app/console doctrine:generate:entities AppBundle:Feature

Alternatively, modern IDE tools like PHPStorm can also generate the standard for you. Chaining is a great feature to encourage on setter methods, simply return $this after setting an entity property.

Using Twig’s default() filter to avoid “Variable some_var does not exist”

References to undefined variables are a common cause of web pages not displaying when using Twig. A simple mention of an undefined variable can cause 500 errors:

{{ some_var }}

Variable some_var does not exist in..

Whilst turning strict_variables off in the twig config will obviously hide the error and is best for production, during development hiding errors can ultimately making spotting bugs harder. A better solution is using Twig’s default() filter like so:

{{ some_var|default("") }}

This will print the variable if it exists or nothing if it is not defined.
For if statements and loops, there are 3 choices:

  1. The default() filter:
  2. {% if some_var|default(false) %}some text{% endif %}
  3. The fine grain but hideously verbose control of defined:
  4. {% if some_var is defined and some_var %}some text{% endif %}
  5. The empty filter:
  6. {% if some_var is not empty %}some text{% endif %}

There are also lots of other useful tests, filters and functions that Twig provides out of the box, check out their documentation pages for more info. Hope this helps. Please like and share if it does 🙂

AngularJS IE11- (ONLY) SyntaxError on ng-view div when opening WebSockets socketHelper

A strange error cropped up today. The Angular 1 JS WebSocket() based application worked fine on all browser exception Internet Explorer 11. The error reported was as such:

SyntaxError <div class="ng-scope" ng-view="">
 code: <Permission denied>,
 constructor: { },

Far from actually being a syntax error, the actual cause turned out to be a missing / (forward slash) in the WebSocket connection creation call to:

conn = new WebSocket('ws:/localhost:3001');

This of course should have been:

conn = new WebSocket('ws://localhost:3001');

Only Internet Explorer 8 – 11 experienced this error. Whilst there is a definite syntax error in specifying a websocket url, it’s hard to tell if the error is perhaps bubbling up or causing an unrelated issue further along the command sequence.

Which Symfony Version number?

The Symfony console provides a -V option. This can be handy for quickly finding out which version of Symfony a system is running but it has moved location a bit over the years (note the uppercase -V or use –version):

$ php bin/console -V
Symfony version 3.1.2 - app/dev/debug

If this produces a bunch of errors about autoload.php then you need to run composer install to install other required parts of Symfony. Whilst if you receive a command not found error then try:

$ php app/console -V
Symfony version 2.7.10 - app/dev/debug

If still a command not found error then try:

$ php lib/vendor/symfony/data/bin/symfony -V
Symfony version 1.4.20

In all versions, running console without -V will produce a long list of all the commands available, along with the version info. This is very useful and really good regular learning exercise, the options really show how Symfony works.

As we can see, the console command moved to the project folder’s bin/ folder as of version 3.0 in Symfony2 versions the console command lived in the project’s app/ directory, whilst if only lib/vendor/symfony/data/bin/symfony exists then you’re using Symfony version 1 (now an obsolete legacy platform)!

Modify XML files using XSLT

XSLT can be great for transforming XML files, especially where information is stored in attributes thanks to the @ operator.

If you have a xml file like:

The elements can be combined into one with multiple attributes, ie:

<field row0=”Value0″ row1=”Value1″ row2=”Value2″ row3=”Value3″ row4=”Value4″ row5=”Value5″ row6=”Value6″ row7=”Value7″ row8=”Value8″ row9=“Value9”></field>

using the following XSLT:

A web search will reveal many handy online testing tools, such as:

Additionally, many IDEs now come with tools and verifiers build in to facilitate XSLT translation of XML files. As much as we prefer using JSON where possible these days, XSLT can adds an extraordinary amount of functionality and flexibility to XML files.

General directions in online learning

Since its beginnings, e-learning has always been an area where technology can thrive and improve upon an already progressive area of learning. With the help of laptops, tablets and smartphones, the area of e-learning has never looked so promising, so before the new school year begins, let’s look at the top 3 new structure and ideas to look out for in e-learning platforms this semester.

  1. Cloud based technologies
    As Cloud based technologies become more and more popular, many businesses and organisations are starting to see the benefits of using this technology to train their students and employees. Thanks to its accessibility, security and cost-saving properties, the Cloud is the obvious choice for businesses looking to remotely transfer content and collaborate with colleagues online.
  1. Mobile learning
    With the ability to learn from absolutely anywhere, mobile learning or mLearning, is probably the fastest growing area of e-learning out there. Aside from the capacity to use your smartphone or mobile device to browse educational websites and use educational software, the mLearning platform is quickly beginning to encompass interactive aspects such as the phone’s accelerometer, compass, gps, camera and audio to make learning even more interesting and accessible.
  2. Gamification
    Having been on the e-learning horizon for a few years now, this year is proving to be bigger than ever when it comes to the gamification of learning. Making it much more fun to learn, and distancing itself from a more traditional style of lecture, gamification turns boring old tests into exciting new challenges that the learner can solve at their own pace.

E-learning is an exciting and innovative way for students of all types and abilities to learn, and in the coming years it is likely to become totally unrecognizable from what it is today, but until then, these are the key trends to look out for in the e-learning world.

Changing the default keyboard shortcut of a Chrome Extension

Changing the keyboard shortcuts of some Chrome web browser extensions can be a pain when the preferences option is non-editable in chrome://settings. A some-what longwinded workaround is to edit the source code of the extension directly. Often this is available on somewhere like GitHub.

To change a Chrome extension keyboard shortcut via its source code, the steps are as follows:

  1. Clone the extension source code to your computer
  2. Open the manifest.json file in the extension’s route directory. Edit or add this section:”omnibox”: { “keyword” : “key” }

Where “key” is the keyboard shortcut key itself. As an fyi, Chrome likes to calls its address bar the “omnibox”

  1. In chrome, go to: chrome://extensions/
  2. Delete the old extension (careful of deleting important data if relevant)
  3. Switch on developer mode in top right of page
  4. Select Load unpacked extension... and navigate to the cloned directory
  5. Verify the extension has loaded in Chrome and works correctly
  6. It is a security risk to browse the Internet with Chrome Extensions developer mode left on. Solve this by packing the extension into a single .crx file (a type of .zip file).
  7. Delete the newly created “unpacked” extension and click Pack extension... (you can sign the file with a .pem key but this is not necessary).
  8. Switch off developer mode in top right of page
  9. Nagivate to the directory above the cloned directory
  10. Drag the .crx file produced in the packing step onto the Chrome window
  11. The new extension should now appear on the chrome://extensions/ page
  12. Verify the correct result by navigating to chrome://settings/ -> Manage search engines...
  13. At bottom of page under “Search engines added by extensions” the keyboard shortcut should be updated and extension only appears once


You now have a modified keyboard shortcut for a Google Chrome extension. Some installations may experience problems with the browser disabling the extension unless it is officially released through the store (private repos are available). If the extension is disable then a restart of the web browser usually suffices in making the extension work again.