↓ Skip to main content

PHPUnit - No coverage report

I was trying to get PHPUnit to give me some coverage report for a project I had not worked on for a long time. I had received a github pull request from someone and I wanted to see what the coverage was on the project to see if the submitter had done a good job of covering his code, but I couldn’t get PHPUnit to generate a report that contained any data. All I would get was a couple of empty directory folders pages, which was useless.

I had code coverage work on another project in the same environment I was in, so I was pretty sure that my problem had to do either with how I had setup PHPUnit for that particular project, or that something else was interfering with the report generation.

I tried a couple of things, starting by calling phpunit from the command line using different arguments:


--coverage-html report test\symbol_test.php

Would generate some report with data in it, good!


-c test\phpunit.xml (logging set in phpunit.xml)

Would generate an empty report, not good…


--coverage-html report -c test\phpunit.xml

Would generate an empty report, not good…

So at that point I saw that it was working correctly and that something was definitely wrong with my phpunit.xml configuration file. I went back to the phpunit.de manual, specifically on the configuration page, and tried to figure out my problem.

For code coverage to be included in your report, you have to add a filter, be it a blacklist or a whitelist, but you have to have a filter.

So I quickly added a filter such as


<filter>
	<whitelist>
		<directory>../</directory>
	</whitelist>
</filter>

which would whitelist everything that is in the project (my project root is one level above test). Ran **phpunit ** in the test folder and I finally got a report with data!


PHPUnit - PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Util/Filter.php'

A friend of mine was trying to install PHPUnit on his mac (OS X Lion), but unfortunately, he got stuck during the process.

At some point, he was faced with this error being displayed. We both looked at the problem and first made sure that said file existed. It was the case, weird…

A bit of googling will reveal that this is frequently fixed by appending the path to the pear folder to your php include_path (defined in your php.ini). But in his case, that was already done and it still wasn’t working.

Next up was to check permissions problem. Not having read permissions on the file would of been an easy one to fix, but again, this was not the cause of the problem.

At this point you might be asking yourself, how come the file exists, you have permission to read it, but yet, you can’t…

Well, the actual problem was that his OS was set up such that the maxfiles was set to 256 and PHPUnit had already reached that amount of open files.

To check if you have the same problem, try running phpunit using sudo dtruss -f -t open phpunit (Linux users will want to use strace -e open phpunit). In your output, you should see the files being opened. At some point, you’ll find Err#24, which indicates “To many file descriptors opened”. If you have this problem, then the following should help you fix it.

The solution to this problem is quite easy. What you’ll want to do is increase the maximum number of descriptors that can be used by a process. You can do it temporarily with ulimit -S -n 1024 (to use 1024 instead of 256). Another way is to edit it and keep those settings (until you change them again) is to use launchctl limit maxfiles 1024 unlimited.


PHPUnit not outputting coverage-html

I’ve been using PHPUnit recently to test a Kohana application I’m developing as my last semester project for my bachelor’s degree.

At some point during the development, code coverage generation decided to stop working on my desktop (my remote CI still had no problem).

I started diagnosing the problem, being on Windows, I thought it could be due to the “poor job” I had done on installing php, pear and phpunit. I didn’t want to go through the trouble or reinstalling everything though and just did the minimum: uninstall and reinstall phpunit. No success at that point.

I decided to go back a week or two in my SVN revisions, have it generate code coverage and get to the point were code coverage generation would fail. Took around 2 SVN “update to” to get to that point. After that, I tried updating the tests, but the new tests were using new features. So I updated the code first, then started updating the test files one by one. After a couple of files, I hit an interesting message:

ErrorException [ 1 ]: Allowed memory size of 134217728 bytes exhausted (tried to allocate 543278 bytes) ~ C:\php\pear\Text\Template.php [ 134 ]

I never had that message show up before, which is kind of strange. I would have expected PHP to tell me that same message everytime it tried to generate the documentation but couldn’t…

So, quick fix was for me to edit my php.ini so that the memory_limit = 256M instead of 128M.


Serializing "anything" in C# using extensions

I was recently interested in getting C# objects to serialize into XML so that I could save and load a configuration out of a file. Sadly, C# support for XML and serialization is far from the best stuff I’ve seen in programming. But anyway, here’s what this post is about: using DataContract with a bit of extension magic to get anything to be serializable into XML!

One small problem when you use the Serializable attribute of C# is that you cannot serialize dictionary directly and we all know dictionaries are a VERY useful structure to have serialized. One easy way to have serializable dictionaries is to use the DataContract attribute instead. It implies a bit more code compared to the version where you use the Serializable attribute, but not that much more (mostly the WriteObject and ReadObject lines).

I haven’t coded any of this, so thanks to user Loudenvier from Stackoverflow.com! This solution allows you to turn ANY object into an XML string representation. It also allows you to turn that string back into an object representation. Very useful.


public static class SerializationExtensions
{
    public static string Serialize<T>(this T obj)
    {
        var serializer = new DataContractSerializer(obj.GetType());
        using (var writer = new StringWriter())
        using (var stm = new XmlTextWriter(writer))
        {
            serializer.WriteObject(stm, obj);
            return writer.ToString();
        }
    }
    public static T Deserialize<T>(this string serialized)
    {
        var serializer = new DataContractSerializer(typeof(T));
        using (var reader = new StringReader(serialized))
        using (var stm = new XmlTextReader(reader))
        {
            return (T)serializer.ReadObject(stm);
        }
    }
}

How to use example


// dictionary to serialize to string
Dictionary<string, object> myDict = new Dictionary<string, object>();
// add items to the dictionary...
myDict.Add(...);
// serialization is straight-forward
string serialized = myDict.Serialize();
...
// deserialization is just as simple
Dictionary<string, object> myDictCopy = serialized.Deserialize<Dictionary<string,object>>();

Source: http://www.stackoverflow.com


Enginuity series on GameDev.net

I’ve just stumbled upon a series of articles from Richard Fine that were written in 2003 which describes the basics of a game engine he calls Enginuity. It lays the foundations to basic memory management, logging, kernel and tasks management and much more. An extremely interesting read. Sadly, you can read in the articles that this was projected to be a series of more than 8 articles, but it seems it stopped at the fifth article. If anyone knows where to find the rest (if it exists), I’d be grateful!

On GameDev.net: Part 1 Part 2 Part 3 Part 4 Part 5


Software developer

A man is flying in a hot air balloon and realizes he is lost. He reduces height and spots a man down below. He lowers the balloon further and shouts: “Excuse me, can you tell me where I am?”

The man below says: “Yes you’re in a hot air balloon, hovering 30 feet above this field.”

“You must be a software developer,” says the balloonist.

“I am,” replies the man. “How did you know?”

“Well,” says the balloonist, “everything you have told me is technically correct, but it’s of no use to anyone.”

The man below says, “You must work in business as a manager.” “I do,” replies the balloonist, “but how did you know?”

“Well,” says the man, “you don’t know where you are or where you are going, but you expect me to be able to help. You’re in the same position you were before we met but now it’s my fault.”

Source: Coding Horror


Software developer

A man is flying in a hot air balloon and realizes he is lost. He reduces height and spots a man down below. He lowers the balloon further and shouts: “Excuse me, can you tell me where I am?”

The man below says: “Yes you’re in a hot air balloon, hovering 30 feet above this field.”

“You must be a software developer,” says the balloonist.

“I am,” replies the man. “How did you know?”

“Well,” says the balloonist, “everything you have told me is technically correct, but it’s of no use to anyone.”

The man below says, “You must work in business as a manager.” “I do,” replies the balloonist, “but how did you know?”

“Well,” says the man, “you don’t know where you are or where you are going, but you expect me to be able to help. You’re in the same position you were before we met but now it’s my fault.”

Source: Coding Horror


Shared network computers not showing up in Leopard

Since I’ve upgraded to Leopard, I couldn’t connect to my Windows network anymore. Browsing through various websites looking for answers, I tried pretty much all of them with no success. Not until I found this page. Is explanation is pretty simple and indeed makes my shared network show up once again!

Here’s the explanation and instructions on how to fix it:

You have plenty of suggestions for fixing the Leopard Windows sharing issue, that I see are all a bit hit or miss. I think the reason for this is because a lot of the things people are trying are fixing the real root issue by accident.

The Leopard advanced network settings has a WINS tab, and in this there is a NETBIOS name field. This is auto-generated by Leopard - it looks like it uses the MAC address to generate it. However, if what is put in there is not compliant with the NETBIOS settings of the Windows network you are connected to, then browsing will not work, because the WINS server will not acknowledge the WINS requests coming from Leopard.

The actual NETBIOS name requirements supported vary depending on the flavour of OS running on the WINS servers on the network. I would suspect the best route to success is to use the most basic NETBIOS name requirements, back from the old Windows NT 4.0 days - stick in no more than an 8-character alphanumeric into the NETBIOS field, and all Windows browsing will be restored.

I had two Leopard machines, one which would browse and one which would not. The browsing one had an 8-character WINS setting, and the non- browsing one had a MAC address in the WINS field. Removing the MAC address and putting in a simple word in the WINS field IMMEDIATELY reinstated browsing.


Boot Camp and Windows install

If you’re having a hard time getting Boot Camp to correctly install Windows and you’re getting either a “disk error” or “hal.dll missing” error, here’s how to actually fix it.

First, you’ll have to restore your disk to a full Mac OS X partition. Then, you’ll reformat it to be a Mac OS X partition and a Windows partition (using Boot Camp).

This is now the important step. You must write over the partition. So load up the Terminal and enter


sudo dd if=/dev/zero of=/dev/rdisk0s3 bs=1m count=100

Where rdisk0s3 is the disk where the Windows Partition is. To know the name of your Windows partition, you can go to the Disk Utility (Applications-Utilies) and look for a partition under your hard drive.

When that is done, you can restart your computer with your Windows CD and install it. Everything should be fine now.

PS. Use at your own risk. The command involve writting 0’s to your partition table, which might screw it up if done unproperly.

PPS. I cannot be held responsible for anything happening to you or your computer.


Firefox being slow?

If you’ve been having some problems with your connection or especially with firefox, check if you have FasterFox. If you indeed do, please uninstall this crap. I was on dnsstuff.com and got blocked by one of their script telling me I had FasterFox (which in that case was true, even though I generally don’t install that thing). I was having a hard time loading pages for no reason but I had good internet download speed using FlashGet.

In the end, I removed FasterFox, restarted FireFox, everything is back fast and furious :).