Newbie fail: How PHPUnit/Selenium DELETED all my files

Hi everyone!

So I’m new here, trying to learn Yii, and right now I’m trying to get a grasp on PHPUnit testing, and thus Selenium as well. I’m coming from giving up on learning Zend Framework and so far Yii seems to beat ZF on an important point — community and documentation.

I thought I’d share with you how some basic troubleshooting on getting this to work ended up deleting all my PHP files in my project. Hopefully someone will end up pointing me in the right direction on getting it to work as well, but at least you should get a laugh out of it.

This was yesterday. I was following multiple tutorials at the moment. I had a basic webapp created via yiic (running WAMP here) with some slight modifications. I had successfully installed PEAR, PHPUnit and got my Selenium server running.

Oh, a tip on that, if you don’t want a command window running constantly is to call “javaw -jar selenium-server-standalone-2.6.0.jar” instead of “java -jar selenium-server-standalone-2.6.0.jar”.

My testing worked as it should! I could do Unit tests and even get code coverage reports on these. I could do functional tests using Selenium, but here lied the problem — no code coverage on the functional tests!

Naturally the Googling began. I tried the tips from PHPUnit’s official documentation regarding coverage and Selenium.

I tried the tips from this guy in this thread here:

(link altered since this is my first post and I can’t post links, this is a post on this forum)

forum/index.php?/topic/13210-installation-guideline-to-make-code-coverage-report-with-yii/

Still, my reports always showed up empty. So this is when I began debugging the actual code coverage code to see if I could figure out the problem. I inserted a lot of ‘error_log()’ statements here and there and tailed my logs to see what was getting passed around and not.

In the process of testing I noticed a method being called, named ‘getFileIterator’. This resides in File_Iterator_Factory. The two first parameters are $paths and $suffixes. I was tracing out what was being supplied here and I noticed that before running the code coverage HTML generation, there were paths sent here and the suffixes were always ‘.php’.

Except when doing the code coverage report…

At that point, from the phpunit_coverage.php in my webroot, it was passing in a variable $_GET[‘PHPUNIT_SELENIUM_TEST_ID’]. And this was of course a random string ID for Selenium.

My logical thinking (late at night I might add) concluded that this meant that it was looking for files with the filetype set to a random string, like ‘9623a9ae87be789a6b278e6b’ or whatnot. But I couldn’t find any files with that filetype.

“So maybe it was doing it wrong?”, said my brilliant engineering mind. “Let’s just change this to supply ‘.php’ instead”

Said and done, I then proceeded to try running a test.

Console output for test call ended to something like this:

(pastebin ID, can’t link)

Es1qr288

And I was tailing my apache error log for this virtual host, and those errors looked like:

(pastebin ID, can’t link)

nUmYfBLk

“Huh”, I said, about to turn back from this route. I went back to my IDE to change back the variable since obviously it wasn’t working, throwing a lot of unexpected errors.

Except I couldn’t. The file had closed in my IDE. The file was gone!

After scratching my head for a few seconds, I started looking around. ALL the files were gone! Every single PHP file in my whole project had been removed when I ran the test.

So what happened?

I’m not sure. But I’m guessing this phpunit_coverage.php is supposed to find temporary files with selenium ID’d filetype endings, parse through them and then delete them. Now, it deletes every .php file it finds instead.

Luckily, nothing important was lost and I had access to a local history over the files so in the end I could restore what I had. Important lesson somewhat learned (still don’t get it though), without a high cost to pay.

That’s the end of my story.

If someone has actually gotten code coverage to work using Selenium and PHPUnit, please let me know and share any tips you might have on getting it to run.

I’m using all the latest versions as of today’s date on PHPUnit and Selenium.

Did you set up a separate test db connection in protected/config/test.php?

The Definitive Guide to Yii has further details.

/Tommy

Thank god for IntelliJ IDEA / PhpStorm’s Local History feature :slight_smile:

No I haven’t done that… do you really think that could affect the issue?

I mean, it surely is a great thing to have a separate DB for your testing, but I can’t imagine how that would matter in this case.

I’ve been in the #PHPUnit IRC channel for a bit and got word from an op there that code coverage for this case does in fact not work. No more info than that though.

Sorry, I didn’t read thoroughly everything you wrote. My apologies. So all PHP files were gone. I don’t know why that would happen, so I’ll rest my case for now.

/Tommy