Archive

Archive for the ‘Project’ Category

pt_date: Work with dates in php

2011/03/13 Leave a comment

One of the most awful things when I work in PHP is to manage operations with dates. With the PHP 5 release came the announce of the new Date Class with operatios to add days,months, years, etc… to get the diff between to dates, and more and more. But when you try to use this ‘wished’ class, what happens ? At least for me frustation. How can the php team believe that the next code is ‘human readable’ or usable?


$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n"; -> 2007-06-05 04:03:02

Oh my god, in my opinion this is the wrong way to help developers.

How I need to operate frequently with dates and I don’t want to use the native Date class, I have created my own class with clear function names and with chaining feature implemented.

Let me show some small examples:


- Add 2 months to the current date

date = new pt_date();
date->add_months(2);

- Move to the begin of the month and add 5 hours:

date = new pt_date();
date->move_to_begin_of_month()->add_hours(5);

- Get days between 2 dates:

date1 = new pt_date("2009-10-15 00:00:00");
date2 = new pt_date("2009-10-22 00:00:00");
echo $date1->days_between($date2); --> 7

or

date1 = new pt_date("2009-10-15 00:00:00");
echo $date1->days_between("2009-10-22 00:00:00"); --> 7

I have published the code in github as a public open source project.

[ https://github.com/gramos74/pt_date ]

I hope that you find it useful.

Advertisements
Categories: Project Tags: ,

pt_mock: Mock objects when testing in php

2011/01/16 Leave a comment

When I started with TDD in PHP, some years ago, I used the mock feature of PHPUnit but it disgust me a lot. At that time I worked at Domestika with Mathias Biilmann and Rodrigo Alvarez. All together we decided to create our mock class to work in a more simple way.

When I left Domestika I wrote, from the begining, my own version of that mock object with a similar behaviour of the original mock object but adding some new features.

Now I have finally published the code in github as a public open source project.

[ https://github.com/gramos74/pt_mock ]

Some examples of how to use it:

We have a method class that receives a object as parameter. Inside this method
we will call the object method ‘method_mock’ with parameter ‘a’
and we expect that it returns ‘b’.

    class class_a {

        function my_method($class_b) {
            return $class_b->method_mock(‘a’);
        }

    }

    $class_b = new pt_mock(‘Class B’);
    $class_b->expects(‘method_mock’)->with(‘a’)->returns(‘b’);

    $class_a = new class_a();
    echo $class_a->my_method($class_b); // —-> ‘b’

To check that all expectations have been accomplished :

    $class_b->verify(); // for a instance of pt_mock
    pt_mock::verify_all(); // for all mocks instantiated

If you want to test that the method is called two times:

    $class_b->expects(‘method_mock’)->with(‘a’)->times(2)->returns(‘b’);

Sometimes you don’t want to test if a method is called, you only want that if a
method is called the mock object returns a value based on parameters.

    $class_b->stubs(‘method_mock’)->with(‘a’)->returns(‘b’);

    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’
    …….

And sometimes you want to raise a exception instead of to return data.

    $class_b->stubs(‘method_mock’)->with(‘a’)->raises(new Exception());

    echo $class_b->method_mock(‘a’) —> raises a exception

I hope that you find it useful.

Categories: Project Tags: ,