Skip to content

Commit

Permalink
Test and document the query logger callback
Browse files Browse the repository at this point in the history
  • Loading branch information
treffynnon committed Aug 28, 2013
1 parent 51c1f47 commit c842904
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 98 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Changelog

* Add PSR-1 compliant camelCase method calls to Idiorm (PHP 5.3+ required) [[crhayes](https://github.com/crhayes)] - [issue #108](https://github.com/j4mie/idiorm/issues/108)
* Add static method `get_config()` to access current configuration [[javierd](https://github.com/mikejestes)] - [issue #141](https://github.com/j4mie/idiorm/issues/141)
* Add logging callback functionality [[lalop](https://github.com/lalop)] - [issue #130](https://github.com/j4mie/idiorm/issues/130)
* Uses table aliases in `WHERE` clauses [[vicvicvic](https://github.com/vicvicvic)] - [issue #140](https://github.com/j4mie/idiorm/issues/140)
* Ignore result columns when calling an aggregate function [[tassoevan](https://github.com/tassoevan)] - [issue #120](https://github.com/j4mie/idiorm/issues/120)
* Improve documentation [[bruston](https://github.com/bruston)] - [issue #111](https://github.com/j4mie/idiorm/issues/111)
Expand Down
24 changes: 24 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ the log. ``ORM::get_last_query()`` returns the most recent query
executed. ``ORM::get_query_log()`` returns an array of all queries
executed.

Query logger
^^^^^^^^^^^^

Setting: ``logger``

.. note::

You must enable ``logging`` for this setting to have any effect.

It is possible to supply a ``callable`` to this configuration setting, which will
be executed for every query that idiorm executes. In PHP a ``callable`` is anything
that can be executed as if it were a function. Most commonly this will take the
form of a anonymous function.

This setting is useful if you wish to log queries with an external library as it
allows you too whatever you would like from inside the callback function.

.. code-block:: php
<?php
ORM::configure('logger', function($log_string) {
echo $log_string;
});
Query caching
^^^^^^^^^^^^^

Expand Down
109 changes: 11 additions & 98 deletions test/ConfigTest53.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class ConfigTest extends PHPUnit_Framework_TestCase {
class ConfigTest53 extends PHPUnit_Framework_TestCase {

public function setUp() {
// Enable logging
Expand All @@ -20,104 +20,17 @@ public function tearDown() {
ORM::configure('id_column', 'id');
}

protected function setUpIdColumnOverrides() {
ORM::configure('id_column_overrides', array(
'widget' => 'widget_id',
'widget_handle' => 'widget_handle_id',
));
}

protected function tearDownIdColumnOverrides() {
ORM::configure('id_column_overrides', array());
}

public function testSettingIdColumn() {
ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `primary_key` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());
}

public function testSettingIdColumnOverridesOne() {
$this->setUpIdColumnOverrides();

ORM::for_table('widget')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `widget_id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testSettingIdColumnOverridesTwo() {
$this->setUpIdColumnOverrides();

ORM::for_table('widget_handle')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `widget_handle_id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testSettingIdColumnOverridesThree() {
$this->setUpIdColumnOverrides();
public function testLoggerCallback() {
ORM::configure('logger', function($log_string) {
return $log_string;
});
$function = ORM::get_config('logger');
$this->assertTrue(is_callable($function));

ORM::for_table('widget_nozzle')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `primary_key` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testInstanceIdColumnOne() {
$this->setUpIdColumnOverrides();

ORM::for_table('widget')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget` WHERE `new_id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testInstanceIdColumnTwo() {
$this->setUpIdColumnOverrides();

ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_handle` WHERE `new_id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testInstanceIdColumnThree() {
$this->setUpIdColumnOverrides();

ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5);
$expected = "SELECT * FROM `widget_nozzle` WHERE `new_id` = '5' LIMIT 1";
$this->assertEquals($expected, ORM::get_last_query());

$this->tearDownIdColumnOverrides();
}

public function testGetConfig() {
$this->assertTrue(ORM::get_config('logging'));
ORM::configure('logging', false);
$this->assertFalse(ORM::get_config('logging'));
}
$log_string = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'";
$this->assertEquals($log_string, $function($log_string));

public function testGetConfigArray() {
$expected = array(
'connection_string' => 'sqlite::memory:',
'id_column' => 'primary_key',
'id_column_overrides' => array(),
'error_mode' => PDO::ERRMODE_EXCEPTION,
'username' => null,
'password' => null,
'driver_options' => null,
'identifier_quote_character' => '`',
'logging' => true,
'caching' => false,
'return_result_sets' => false,
);
$this->assertEquals($expected, ORM::get_config());
ORM::configure('logger', null);
}

}
}

0 comments on commit c842904

Please sign in to comment.