29 January 2018

Fun with Slim

I'm working on a smallish project to build service endpoints using Slim. (We've used other frameworks in the past. I recently converted my model code to a PSR-4 autoloader. The team strongly suggested Slim for this project.)

The Slim documentation makes it abundantly clear that Response objects are immutable: each method that accepts a Response as an argument returns a copy of that object. You can also puzzle out that methods like withHeader() and withStatus() can be chained together. And the docs explain how to use withJson() if you want your endpoint's payload to be JSON and you're content with the default MIME type.

What is not so well explained: that getBody()->write() does not return a Response, so it can't be chained. Nor is it clear how to use these methods when you want to set your own MIME type ("application/hal+json", in my case) or status code. Hence, this example, somewhat simplified.

The GET /short endpoint returns a brief HAL representation of the resource identified by :id.



namespace Demo;

class Controller {
    /**
     * GET /short/:id
     * 
     * @param \Slim\Http\Request $request
     * @param \Slim\Http\Response $response
     * @param string[] $args
     * @return \Slim\Http\Response
     */
    public function short($request, $response, $args) {
        $id = $args['id'];
        try {
            $guid = ...;  //look up info about $id
            if (empty($guid)) {
                Logger::warning("No info for $id");
                return $response->withStatus(404);
            }
            $doc = ...; // construct a model, using $guid
            $rsp = $response->withAddedHeader('Content-type', 'application/hal+json');
            $rsp->getBody()->write(json_encode($doc->getDocArray()));
            return $rsp;
        }
        catch (Exception $e) {
            Logger::warning($e->getMessage()." Failed");
            return $response->withStatus(500);
        }
    }
}

And how do you write a unit test for this endpoint? Likewise, the docs give some strong hints, but leave you to connect the dots. The trick is to use Environment::mock() to get an object that can be passed to a factory method on Request. A simplified version of my happy-path unit test:


class ControllerShortTests extends \PHPUnit_Framework_TestCase
{
    /** @var \Slim\Http\Request */
    private $request;
    /** @var \Slim\Http\Response */
    private $response;
    /** @var string[] */
    private $args;
    /** @var \Slim\Container */
    private $container;
    /** @var \Demo\Controller */
    private $controller;

    public function setUp()
    {
        $this->request = \Slim\Http\Request::createFromEnvironment(\Slim\Environment::mock());
        $this->response = new \Slim\Http\Response();
        $this->args = [];
        $this->container = new \Slim\Container();
        ... // set up a mock datastore provider
        $this->controller = new \Demo\Controller($this->container);
    }

    public function testExpectSuccess()
    {
        ... // configure the provider
        $this->args['id'] = 987654321;
        $response = $this->controller->short($this->request, $this->response, $this->args);
        $this->assertEquals(200, $response->getStatusCode(), 'Wrong status');
        ... // and more assertions
    }
}

14 January 2018

Gaming the benchmarks

What's holding us back from exascale computing? According to Katherine Bourzac's report, the simple answer is power.

20 November 2017

The decade in review

I just happened to notice that the 10-year blogaversary for this low-volume property had passed in April without recognition. So, belatedly, here’s the first sentence (more or less) of the first post of each year from this blog:
  • 9 April 2007: I've just downloaded the FogBugz for Visual Studio 2005 plugin, and I'm taking it for a spin.
  • 11 January 2008: The Economist brings its customary skeptical perspective to the software-as-a-service market.
  • 8 January 2009: Ashlee Vance introduces the open-source statistical analysis programming environment R, one that challenges proprietary packages.
  • 10 January 2010: Junk Charts provides some how-I-did-it code in the statistical programming language R.
  • 12 January 2011: Michael Donohoe describes an update [to the] New York Times's Emphasis feature, which enables readers and bloggers to deep link to individual paragraphs within a story.
  • 9 January 2012: "I decided to follow the single-letter style and called it C, leaving open the question whether the name represented a progression through the alphabet or through the letters in BCPL."
  • 7 January 2013: Aaron Souppouris gets a quick look at the XO-4 convertible at CES.
  • 2 January 2014: As product owner Patrick Cooper explains in an unusually frank post, our launch a couple of weeks ago was actually our second try.
  • 2 January 2015: Sylvia Tippmann offers a brief introduction for scientists to the programming language R and its ecosystem.
  • 11 January 2016: Dalmeet Singh Chawla introduces Depsy, a service that seeks to measure the contributions made by researchers to the body of software that powers science.
  • 30 January 2017: Antonia Cereijido and Alina Selyukh ask, "Why Aren't There More Women in Tech?"
Despite what you might infer from this selection drawn at random (without replacement), this blog is more than just about R.

Worth it

Janice Keihanaikukauakahihuliheekahaunaele, a name for database schema and user experience designers to reckon with.