Categories
Programming

Jasmine Best Practices

  1. The file name should be Matching for the top level test suite name.
  2. There should be one top level test suite per file.
  3. Give a meaningful name or title for the test suite.
  4. Either use var or this to define variables that needs to use across tests. var variables should be placed just below the top describe (test suite)
  5. Use setup and teardown methods to perform common variable initializations and functionalities.
  6. A spec (test case) should test only “ONE” expectation.
  7. Group the main test suite with sub test suites to group sections.

Good Test Suite

Following is a structure of a good looking Jasmine test suite.

// GoodTestSpec.js
/**
 * Describe the purpose of this test suite here.
 */

// give a meaningful name for the test suite.
// only "ONE" top level test suite per file.
describe('Good test suite', function () {
    // define "ALL" the values that needs to be accessed across test cases
    // Else use `this` keyword. But follow one approach. If `this` is used, don't define values here.
    var foo = null,
        bar = null,
        numberOne = null;

    // initialize "ALL" values above to their desired default value, in this block "beforeEach"
    beforeEach(function () {
        foo = null;
        bar = null;
        numberOne = 1;

        // Using `this` approach. Pick one that suits the Test Suite.
        this.moo = null;
    });

    // a spec (test case) should test only "ONE" expectation
    it('should check foo to be null', function () {
        // ....
        expect(foo).toBe(null);
    });

    // another spec
    it('should check numberOne to equal 1', function () {
        // call fail() if something is not right and want to fail the test
        // fail('woops! not correct state.');

        expect(numberOne).toEqual(1);
    });

    // Group the main test suite with sub test suites to group sections.
    // The title should be descriptive.
    describe('that is magnificent', function () {

        // Test name will read as "Good test suite that is magnificent should check 42 to is 42"
        it('should check 42 is 42', function () {
            expect(42).toEqual(42);
        });
    });
});

Learn More

Categories
Blog Programming

Fix Sublime Text auto-complete hiding issue

Hello Sublime Text lovers out there: you might be using auto-save package [https://packagecontrol.io/packages/auto-save] as a plugin. But there is an irritating bug with it that causes auto-complete popup to hide when auto-save is invoked.

While this is not officially fixed in the package, there is a patch (workaround) that will resolve this issue. Please see my comment [https://github.com/jamesfzhang/auto-save/issues/21…] to learn how to apply the work-around in GitHub auto-complete package repository.

Workaround

First go to sublime user’s package folder. /home/<user>/.config/sublime-text-3/Packages/
2. Then create a folder called auto-save which is the package name.
3. Download and save this auto-save.py in the newly created folder . tip: click ‘view raw’ content then save the file as ‘auto-save.py’
4. Restart Sublime Text.
5. Now when auto complete is visible, auto save will not be invoked which means auto complete will not hide.

Important Notes

  • Make sure you have Sublime Text 3 build 3103 which has the new method which I used to patch the bug.
  • The link to download auto-save.py is from the patch I proposed to the owner of this plugin. You can see the status of the PR request at [#27].
  • As I stated everywhere, this is not a perfect patch, but will be significantly less irritating than the auto complete hiding bug. In other words this change causes the save not to get called when auto complete popup is visible. So any file changes made while auto complete is visible is not saved.

PS
I think this new way to override the plugin behavior(as per Sublime Text docs) is more easier and better than my first approach. So until the original author resolve this issue in the package, I hope this patch will help you. If you want to use the latest auto-save package (that is going to include updates, bug fixes, etc), remove the folder that created earlier in user’s package folder. Otherwise still this patch version will be loaded instead.

Hope this will help others also who have encountered the bug.

Categories
Blog Programming

What makes a software product great?

Before talking about software, lets talk about cars. Why do I want to talk about cars is to take great car design as an analogy to great software design.

What comes to your head when you hear BMW or Mercedes-Benz? The standard quality of these vehicles. They ordered by most important first are to be;

  1. Perform well (functional, does what it is suppose to do well)
  2. Reliable
  3. Comfortable (ease of use, positive feeling)
  4. Artistic / Luxurious (Have a taste)

Let’s look at their slogans.

Mercedes-Benz: The Best or Nothing

BMW: Sheer Driving Pleasure

Essentially their focus is to built great vehicles. With mindset let’s see how the above qualities can be applied to software design.

When it comes to software the most important characteristic is functionality.

Functionality

Functionality is the starting point of a software product. All the functionality that is expected from the software product should perform. It is thoroughly important the developer understand the functionality.

  1. Developer must thoroughly understand what functionality are expected and any doubts have to be cleared.
  2. Think about the functionality. This if the functionality is bad (-1), ok(0), good (+1).  If it is bad or ok think whether the functionality can be turned into good or awesome (+2).
  3. Discuss with stakeholders any suggesting changes.
  4. Agree on new functionality if there are changes. Then carry it to development.

Ease of Use

A software product is a thing that is going to be used by someone. Preferably a human being. Whatever the product is (Library, API, web app, website, desktop or console application, game) it should be easy to use. If the software makes the user uncomfortable there is no point of talking about quality because already the product is neglected.

Keep in mind ease of use should be addressed for both end users as well as for developers who are going to work on the product. The code should follow an accepted standard. Commenting is must. Always be consistent. There are tools to generate documents out from the comments in the code. Make sure to use such a tool and understand the power of that tool.

In development use tools that make the developer’s life easy. Example use SASS instead of CSS.

Reliability

No one like something unexpected happen to them. We seek reliability in living and static things. We seek reliability from these things because we rely/depend on them. When you get a sense of reliability in something you start to trust it and use acknowledge them into your life. Reliability and trust are some of the core requirements of daily life. They are part of human nature. It is never enough to stress out the importance of reliability. Reliability is a measure that filter out bad and good. Hence reliability is a premium characteristic of a good software product.

Let’s take a simple notepad application. When you try to save a document but it crash like one out of five it is not a reliable product. If the product crash when it exceeds it limits it is not a reliable product.

So how to improve reliability?

First thing that comes to mind is the testing. Testing is a solid way to analyze the reliability of the product. There is a specific Wikipedia article about different types of reliability testing.

Then follow industry best practices. When developing the application properly handle exceptions.

A Taste

What makes a BMW a BMW or a Mercedes-Benz a Mercedes-Benz? They have their own style and taste. Like that having a taste in a good software product can let your product stand out from the crowd.

When compared to public web applications enterprise applications seems to have very little taste. Michael Dubakov has written a good article about this. With good taste, usability improves. For example on a personal note, I have used the project management app called Trello. Their overall UI design and feature layout almost feel natural.

There are two perspective for software taste. One is the usability and the actual product itself the other is software code and application structure. For example, take Ruby on Rails framework. Their code layout is more humane and how they have written comments along with examples indicate their own style. If you are using a framework learn the style and taste of the code base. Inherit those characteristics when you build your application on top of the framework. Follow standard coding styles and be consistent. Any documentation should not left behind.

 

Conclusion

Bad software are built because you don’t value them as a physical product. In fact you must put great care and quality as if you are building the next gen BMW or Mercedes-Benz. Every letter in your code matters. How you place comments how they describe something must be elegant. The four factors need to consider to build a good software are functionality, ease of use, reliability, and having its own taste.

Categories
Programming

Why your photos not deleted when you delete a user in Laravel

Ok so you have a relationship mentioned in your eloquent user model to link the photo model relationship. So in your user model class you might have something like below to define the relationship.

/**
 * Get the photos
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function photos()
{
    return $this->hasMany(Photo::class);
}

In your Photo class you might have put some code for deleting event to delete the photo files.

protected static function boot()
{
    parent::boot();

    static::deleting(function ($photo) {
        $photo->deletePhoto();
    });
}

Ok so now you can define what to do when the user is getting deleted. Here’s the catch.

If you use the relationship as a function and then call delete on the return query, it will not execute the deleting block for the photos. See following.

static::deleting(function($user) {
    $user->photos()->delete();
});

It is because the query does not identify the photo objects and only delete the records in the table.

To overcome this you have to iterate over each photo object and call delete. That way the object will execute the deleting code block in the Photo class.

static::deleting(function($user) {
    foreach($user->photos as $photo) {
        $photo->delete();
    }
});

 

 

 

 

Categories
Programming

Sonder WordPress Theme

I wanted to develop a minimalist type of a WordPress theme and use on my personal website. The source code is available under Git if you are interested in using it.

View Source

 

sonder wordpress theme screenshot

Categories
Programming

OpenShift with FileZilla

Here’s how you can configure to access your OpenShift using FileZilla FTP.

  1. Open FileZilla and go to Edit > Settings > SFTP
  2. Click Add key and navigate to .ssh folder.
  3. Select id_rsa and when asked, save as id_rsa.ppk
  4. Now it should be listed in SFTP block
  5. Go to OpenShift and get the SSH URL.
  6. In FileZilla add a new connection.
  7. For Host use the URL after @ sign of the SSH URL.
  8. For user use the text before the @ sign of the SSH URL.
  9. Refer Below screenshot

 

openshift-filezilla-ftp

Categories
Programming

Improving PHP Xdebug Speed

Great, you have finally figured out how to install and enable PHP Xdebug. Now you can debug your PHP websites and web apps in your local machine. However what you might found out is that after enabling Xdebug, your website’s response time is very low. You might have to wait for more like a second to display your website.

Disabling profiler

As I found out, the main reason for this is the Xdebug profiler. If Xdeubg profiler is enabled, it will add some significant overhead to the PHP execution. Profiling enables us to get execution statistics such as name of functions executed, the number of times a function has called, execution time etc. This can give information on how to improve the code.

For general debugging, profiling is not needed. So we can disable it by updating the php.ini file and restart the server. Now your web application response time should be greatly improved.

xdebug.profiler_enable = 0;

Well if you want to enable it again, you can set the value to ‘1’ and restart the server. Another way is to use the profiler_enable_trigger cookie. For this first we need to specify value for it.

xdebug.profiler_enable_trigger = 1;

Now to enable profiler for an app request, you need to have the Xdebug profiler cookie set to this value. In Firefox Developer Edition, I use The Easiest Xdebug add on to do this. With this addon, I can easily specify the profiler_enable_trigger value and then before request the website, I would click a button to set the cookie value.

Setting remote host to 127.0.0.1

Typically you would put localhost as the xdebug.remote_host, but I found out that there is a slight lag in Windows when referring localhost to 127.0.0.1. So replace localhost to 127.0.0.1. The special IP 127.0.0.1 is referring to the current machine you are using.
xdebug.remote_host=127.0.0.1

Additional References

PHP Applications Profiling in PhpStorm: https://www.youtube.com/watch?v=_ua_O01IICg

Xdebug Documentation basic features: http://xdebug.org/docs/basic

Firefox : The easiest Xdebug: https://addons.mozilla.org/en-us/firefox/addon/the-easiest-xdebug/

 

Categories
Programming

How to setup canonical URL in Yii2

You can place a canonical URL to a page using your view file.

To generate the canonical URL, we can use the canonical ( ) in the URL helper class. Also note this method consider if the current website URL is applicable to any URL rules specified and if so generate the canonical URL based on that.

yiihelpersUrl::canonical()

One catch I found with this canonical URL method is that all the parameters in the URL are captured and parsed into the generated URL. So If you need to ignore a set of parameters. you would need to create your own canonical URL method where you can pass the parameters that need to be ignored.

The in the view file call the canonical method as following.

$this->registerLinkTag(['rel' => 'canonical', 'href' => Helper::canonical()]);

You are done. Now your canonical URL should be visible in the page header.

 

Categories
Programming

Set up mongoDB as a Windows service

  1. Create a log folder in your data folder
  2. Open console with admin privileges
  3. Then in console type mongod –logpath c:datalogmongodb.log –install
  4. Now mongodb should be listed as a service
  5. Then in console type ‘services.msc’ and find mongodb service listed
  6. Right click on it and select start. Now mongodb should be running
Categories
Programming

VSFTP to backup multiple websites

For our web management service we also performance website backups in a timely manner by using our own FTP server. This guide is written on how we configured VSFTP (A recognized FTP server in Linux distros such as Ubuntu) to handle multiple websites.