Steps to get PHPUnit to run on my XAMPP setup with Cygwin, so I can write and run Symfony2 unit tests.

1. Upgrade PEAR

  1. Download http://pear.php.net/go-pear.phar to C:\xampp\php\go-pear.phar

  2. Run this in cmd.exe (cygwin prompts hosed somehow), taking all defaults

c:\xampp\php>go-pear.bat
  1. Yay.
c:\xampp\php>pear version
PEAR Version: 1.9.4
PHP Version: 5.3.5
Zend Engine Version: 2.3.0
Running on: Windows NT FAI1046162 6.1 build 7600 (Unknow Windows version Enterpr
ise Edition) i586

2. Install/upgrade PHPUnit

Now we can use the cygwin shell. Not sure all these channels are needed, I did this out of order.

cd /cygdrive/c/xampp/php
pear update-channels
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear channel-discover pear.phpunit.de
 
pear install --alldeps phpunit/PHPUnit

3. Use it

XAMPP and/or PHPUnit come with a wrapper called phpunit.bat, which has now been upgraded, but you may need to set your PHPBIN environment var. Also I already have c:\xampp\php in my $PATH.

export PHPBIN=c:/xampp/php/php.exe
 
cd /path/to/mysf2project
phpunit.bat -c app

Now PHPUnit works. Make tests and make them work!

Propel 1.6 (and Propel 1.5 before it) is pretty sweet (thank you François!). I had some confusion with my model’s array of related objects though, thinking it was a regular PHP array. Actually it’s a Collection, specifically a PropelObjectCollection, which implements PHP 5′s ArrayObject interface. You can do a lot of cool things with them.

Sorting

Not immediately obvious, however, was how to sort them. This did the trick for my case (I have a Sequence field manually re-calculable through a jQueryUI sortable widget. Also note the cool inline anonymous function syntax available since PHP 5.3. Incidentally, I’m not sure the terms lambda or closure are helpful because they’re not quite like Lisp lambdas or JavaScript closures.

// Re-sort them by Sequence, numerically
$this->collSegments->uasort(function($a, $b) {
    return $a->getSequence() - $b->getSequence();
});
// Re-sort them as strings, case-insensitively.
$this->collSegments->uasort(function($a, $b) {
    return strnatcasecmp($a->__toString(), $b->__toString();
});

Deleting

Thanks to the PropelArrayCollection API Documentation

  /*
   * Remove the provided Segment object.
   *
   * @param Segment $s
   * @return Segment $s that was deleted.
   */
  public function deleteSegment(Segment $s) {
      $s->delete();
      $key = $this->collSegments->search($s);
      $ret = $this->collSegments->remove($key);
      return $s;
  }

So elsewhere,

	      $this->deleteSegment($s);

Problem

On my Snow Leopard machine this kept happening.

  • The “open” indicator (glowing silver ball under the app icon) in the Dock was flaky, only showing for a few apps though more were running.
  • The task switcher (which you see when you option-tab/alt-tab) didn’t show all running apps. That RUINS it for me. I always use option-tab.
  • I hate rebooting.

Workaround–Restart the Dock process.

Open Terminal† and use one command:

killall -HUP Dock

Don’t be afraid of “killall”. HUP means “Hang Up” and is the normal way of telling something to relaunch.

Thanks to AcmeTech’s old post.

† Terminal is located in the Utilities folder in the Applications folder. Or the quick Spotlight way is command-space, Terminal.

A while back I realized something important:

Monitoring tools are to the sysadmin what testing tools are to the developer.

Recently I realized that there need to be more ways to bring both toolsets together. Here’s one, tying the Nagios monitoring toolset to anything that emits the popular Test Anything Protocol.

More …

I just got this working today. The weirdnesses of Cygwin’s half Unix-half Windows nature had stymied me before, but I’ve prevailed!

I’ve got Gnu Emacs 23.3 for Windows installed in c:/emacs, and a fairly recent install of Cygwin on Windows 7.

Saved this script in ~/cygemacs.sh

#!/usr/bin/bash
c:/emacs/bin/emacsclientw.exe -n -a "c:/emacs/bin/runemacs.exe" `cygpath -wa $@`

Then in my ~/.bashrc:

alias ec="~/cygemacs.sh"

Now I can be all like,

$ ec ~/.minttyrc

And it opens a new frame in my running Emacs (I have (server-start) in my ~/.emacs), or starts Emacs and opens the file if Emacs isn’t running yet.

See also: EmasClient at EmacsWiki.

Bonus tips

Yes I’m still using Subversion but also gitting going with Git.

And courtesy of The Lumber Room, in ~/.bashrc and others:

    export SVN_EDITOR='c:/emacs/bin/emacsclientw.exe -a c:/emacs/bin/runemacs.exe '

And silence that annoying “kill client buffer z0mgbbq?!?” warning:

  (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

Stupid me made the same mistake twice in a row so I’m documenting it for humanity.

I’m updating an old Symfony project to use Symfony 1.3/1.4 and Propel 1.5 through François Zaninotto’s sfPropel15Plugin.

Problem:

I followed the README, right? But…

$ ./symfony propel:build --forms
>> schema    converting "C:/web/myproject/config/schema.yml" to XML
>> schema    putting C:/web/myproject/config/generated-schema.xml
>> propel    Running "om" phing task
>> file-     C:/web/myproject/config/generated-schema.xml
>> autoload  Resetting application autoloaders
>> autoload  Resetting CLI autoloader
>> propel    generating form classes
PHP Warning:  call_user_func() expects parameter 1 to be a valid callback, class 'FooPeer' does not have a method 'getUniqueColumnNames' in C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php on line 485
PHP Stack trace:
PHP   1. {main}() C:\web\myproject\symfony:0
PHP   2. include() C:\web\myproject\symfony:14
PHP   3. sfSymfonyCommandApplication->run() C:\symfony1.3\lib\command\cli.php:20
PHP   4. sfTask->runFromCLI() C:\symfony1.3\lib\command\sfSymfonyCommandApplication.class.php:76
PHP   5. sfBaseTask->doRun() C:\symfony1.3\lib\task\sfTask.class.php:97
PHP   6. sfPropelBuildTask->execute() C:\symfony1.3\lib\task\sfBaseTask.class.php:68
PHP   7. sfTask->run() C:\web\myproject\plugins\sfPropel15Plugin\lib\task\sfPropelBuildTask.class.php:135
PHP   8. sfBaseTask->doRun() C:\symfony1.3\lib\task\sfTask.class.php:173
PHP   9. sfPropelBuildFormsTask->execute() C:\symfony1.3\lib\task\sfBaseTask.class.php:68
PHP  10. sfGeneratorManager->generate() C:\web\myproject\plugins\sfPropel15Plugin\lib\task\sfPropelBuildFormsTask.class.php:72
PHP  11. sfPropelFormGenerator->generate() C:\symfony1.3\lib\generator\sfGeneratorManager.class.php:126
PHP  12. sfGenerator->evalTemplate() C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php:106
PHP  13. require() C:\symfony1.3\lib\generator\sfGenerator.class.php:84
PHP  14. sfPropelFormGenerator->getUniqueColumnNames() C:\web\myproject\plugins\sfPropel15Plugin\data\generator\sfPropelForm\default\template\sfPropelFormGeneratedTemplate.php:34
PHP  15. call_user_func() C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php:485
... etc ...

Solution

I removed too much of the default config in propel.ini..

Make sure this original line is still intact in propel.ini, even though you’ve removed/commented out all the propel.behavior lines that point to the old sfPropelPlugin:

propel.behavior.default                        = symfony,symfony_i18n

So, I had to deal with ISO 8601 formatted dates in JavaScript, and I can’t just use the cool ISO 8601 support in js 1.8.5′s Date.parse because of IE 6. I found a couple of examples out there on blogs but couldn’t get them to work right with my data, and I wasn’t sure I wanted a heavyweight do-it-all solution. I reluctantly reinvented the wheel so I thought I’d share it.

function parseISO8601Date(s){
 
  // parenthese matches:
  // year month day    hours minutes seconds  
  // dotmilliseconds 
  // tzstring plusminus hours minutes
  var re = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(\.\d+)?(Z|([+-])(\d\d):(\d\d))/;
 
  var d = [];
  d = s.match(re);
 
  // "2010-12-07T11:00:00.000-09:00" parses to:
  //  ["2010-12-07T11:00:00.000-09:00", "2010", "12", "07", "11",
  //     "00", "00", ".000", "-09:00", "-", "09", "00"]
  // "2010-12-07T11:00:00.000Z" parses to:
  //  ["2010-12-07T11:00:00.000Z",      "2010", "12", "07", "11", 
  //     "00", "00", ".000", "Z", undefined, undefined, undefined]
 
  if (! d) {
    throw "Couldn't parse ISO 8601 date string '" + s + "'";
  }
 
  // parse strings, leading zeros into proper ints
  var a = [1,2,3,4,5,6,10,11];
  for (var i in a) {
    d[a[i]] = parseInt(d[a[i]], 10);
  }
  d[7] = parseFloat(d[7]);
 
  // Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
  // note that month is 0-11, not 1-12
  // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
  var ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
 
  // if there are milliseconds, add them
  if (d[7] > 0) {  
    ms += Math.round(d[7] * 1000);
  }
 
  // if there's a timezone, calculate it
  if (d[8] != "Z" && d[10]) {
    var offset = d[10] * 60 * 60 * 1000;
    if (d[11]) {
      offset += d[11] * 60 * 1000;
    }
    if (d[9] == "-") {
      ms -= offset;
    }
    else {
      ms += offset;
    }
  }
 
  return new Date(ms);
};

A friendly user named Raynos in the new StackOverflow JavaScript chat room helped me with the 0-based month gotcha. Thanks again, Raynos!

Also, the author of Anentropic tipped me off to this effort to promote good-quality JavaScript documentation, like the Mozilla Javascript Reference and Javascript Guide, which are still definitive after all these years. Googling for javascript stuff usually gets you spam sites with lots of ads and less-than-stellar docs. W3schools, I love you, but seriously.

So listen, Google:

Javascript Date .UTC

I published my second plugin for WordPress today. It lets you configure how long user sessions and the “remember me” cookie last. By default they’re only 2 days and 2 weeks, respectively.

Twofold impetus: a problem with the internal P2 microblog at work when people like me left their browsers open for long periods, and the fact that I access too many WordPress blogs from too many different computers for the 2 week “Remember Me” timeout to be convenient enough. Cranking it up to 22 years!

The WordPress documentation is pretty great BTW.

And it was a little bit interesting to run in to the Year 2038 problem with 32-bit int timestamps.  I don’t know, it was fun working around the  ”infinity” ceiling:

  if ( PHP_INT_MAX - time() < $expire_in ) {
    $expire_in =  PHP_INT_MAX - time() - 5;
  }

So my trusty network administrator at work was on my case because my workstation has been trying to get to Internet sites directly without a proxy, and my little machine is the top hitter on her firewall deny rules. Normally that’s a symptom of malware, so after a partial day looking for and not finding what was causing it, I formatted my aging XP machine and installed Windows 7, which I’d been meaning to do anyway.

After several days rebuilding everything the way I like it, my machine turned up as the frequentest flyer in the firewall deny logs again! It turns out that Windows 7 has some better tools that made it easier to find what was going on. More …

Problem:

Old retiring IIS 5.0 web server has been accepting URLs containing plus (+) for spaces instead of %20 for like 74 years. People have the old URLs bookmarked and stuff so they’ll keep going to them. The content will still exist on the replacement IIS 7 web server. Wouldn’t it be nice to make it transparent?

(Not to mention “foo+bar.pdf” is sane, but “foo%20bar.pdf” reads “foo percent twenty bar”, awkwardly. )

The problem is that IIS7 by default considers naked plusses in the URL as scary and sends a 404.11, URL_DOUBLE_ESCAPED error. Even if you convince it the URL is OK, it no longer maps plus to space and finds a piece of content.

On the old IIS 5.0 server these URLs both work, serving up the document named “foo bar.pdf”:

http://server/foo+bar.pdf

http://server/foo%20bar.pdf

On the new IIS 7 (Windows 2008) server, the second URL works but the first one gives an error.

Solution:

I put this in my application’s web.config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="True" />
    </security>
    <rewrite>
      <rules>
        <rule name="RewriteUserFriendlyURL1" stopProcessing="false">
          <match url="\+" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="{UrlDecode:{REQUEST_URI}}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

the allowDoubleEscaping directive on line 5 solves the first part of the problem, allowing IIS to handle unescaped plusses. The rewrite rule below passes the requested URI through the UrlDecode function (line 15), which thankfully still remembers the time-tested convention of plus equaling space. Now both forms of the URL work.

(I didn’t really write the rewrite stanza, I just stumbled around with the rewrite URL editor in the IIS Manager until it worked)

Yay!