Categories
Linux PHP

Installing PHPUnit on Ubuntu

Summary: I am on Ubuntu 11.10 and have the necessity to install PHPUnit. From my past experience, I know that installing via repository installs old version + fails to run. So I decided to install it via PEAR. Here’s a walk through!

Upgrade PEAR
Damn! The shipped version of pear via the “php-pear” package is outdated. The first thing we need to do is upgrade our PEAR setup. We have to ask pear to upgrade itself!

That should upgrade our pear to the latest version available!

Setup PHPUnit
To install PHPUnit, we have to set auto discovery turned on and discover the PHPUnit PEAR channel. Then we force install PHPUnit with all dependencies. That’s simple!

If everything goes okay, we have a working setup of PHPUnit by now! Type “phpunit –version” on console and check out the output! Ooops! It didn’t work! We’re getting an error message regarding “require_once()”. The issue is obvious, the PEAR directory which contains all PEAR packages haven’t been added to the php include_path.

Fix it this way:

Vim editor window will open with the empty buffer. If you’re not comfortable with vim, just use another text editor. Type in the following directives:

Save and quit. We had to do this manually because pear doesn’t automatically set the include_path to the pear location (which is “/usr/share/php/”).

Now try on terminal:

The output should look like:

Happy Unit testing!

Categories
Uncategorized

Git: Automatically add all changes to staging area

I use Git heavily as a DVCS. The scenario I often come across is when I have manually deleted a whole bunch of files from here and there. It becomes a mess if I have to “git rm ” on them manually – one by one. Thanks to Git, there is an easy solution.

Issuing the above command automatically adds all changed files to the staging area. Cool, isn’t it?

Categories
Uncategorized

Using Entity Framework: The CodeFirst Approach

When I started into the world of .NET based web development, I often encountered the term “Entity Framework Code First” or “EF Code First”. Where as I could no where find the option to create “Code First” entities in the visual studio. 🙁 How disappointing! 😛 I could create ADO.NET Entity Data Model but that would create .edmx files which looked all cryptic to me! I was just beginning .NET and my background is heavily based on PHP and Python where nothing is “Visual”, we do it all in codes.

And then after going through some of the resources online, I finally understood what’s going on. The ADO.NET Entity framework has two popular approaches – “Database first” and “Code first”. As the names suggest, in the first approach you write your models, generate databases, map the database with your models etc. Here database comes first. On the other hand, in the “code first” approach, you write plain classes which are identical to your database objects. Then following specific convention, you magically map the defined classes with the database. No need to write (I mean generate) complex xml maps or all those mess.

While I agree that both the approach have their own benefits, strengths and all those positive things, I found the code first approach easier to get started as a novice. So how does it work? Let’s see!

Say, we already have a table defined somewhere. For convenience, let’s use the following SQL script to generate a table:

I am not going to elaborate the process but the direction is to first create a database and then use the above sql codes to generate a table. It has one ID field that is auto incremented and the Name field. The simplest table structure I could think of! 😀

Now, in code first approach, convention is prioritized over configuration. Hence, we must define an identical class. Here it goes:

Remember those “virtual” identifiers are crucial. They have a lot to do with the magic that keeps things going. Another interesting thing to notice is that the Student object doesn’t derive from any other object.

Now, we need a database context. We shall define a class that will extend the “DbContext” class from the Entity Framework. The code should be like this:

One thing to notice is that we must pass a database connection string to the DbContext class. In this case, I have a connection string defined in my configuration file and I am passing the name of the connection to the DbContext class.

With this, we’re ready to use the Entities we have generated. So, how do we use them? Let’s see some codes:

The above codes will insert a entry in the database when run. Give it a try! 🙂

PS: I am just beginning .NET and C#. If you have any comments, suggestions or plain feedback, please do feel free to leave in the comments. I would appreciate your contribution to my personal development 🙂