Skip to content

Commit

Permalink
A very-nearly-finished stats tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierumbelow committed Feb 24, 2013
1 parent a5144fd commit 2baf933
Show file tree
Hide file tree
Showing 24 changed files with 491 additions and 68 deletions.
4 changes: 3 additions & 1 deletion application/config/autoload.php
Expand Up @@ -27,6 +27,8 @@
|
*/

require_once APPPATH . '../vendor/autoload.php';

/*
| -------------------------------------------------------------------
| Auto-load Packges
Expand Down Expand Up @@ -64,7 +66,7 @@
| $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array();
$autoload['helper'] = array( 'url', 'form' );


/*
Expand Down
2 changes: 1 addition & 1 deletion application/config/routes.php
Expand Up @@ -38,7 +38,7 @@
|
*/

$route['default_controller'] = "welcome";
$route['default_controller'] = "trackers";
$route['404_override'] = '';


Expand Down
27 changes: 26 additions & 1 deletion application/controllers/Trackers.php
Expand Up @@ -7,11 +7,36 @@ class Trackers extends MY_Controller

public function index()
{
$this->data['trackers'] = $this->tracker->get_all();
$this->data['dropdown'] = $this->tracker->dropdown('name');
}

public function show($id)
{
$this->data['tracker'] = $this->tracker->presented()->get($id);
$this->data['dropdown'] = $this->tracker->dropdown('name');
}

public function add()
{
$this->data['dropdown'] = $this->tracker->dropdown('name');
$this->data['trackerData'] = array( 'id' => '', 'type' => '', 'name' => '' );

if ($tracker = $this->input->post('tracker'))
{
if ($this->tracker->insert($tracker) === FALSE)
{
$this->data['trackerData'] = $tracker;
$this->data['validation_errors'] = validation_errors();
}
else
{
$this->_redirect('trackers/show/' . $tracker['id']);
}
}
}

public function _redirect($slug)
{
redirect(site_url($slug));
}
}
2 changes: 1 addition & 1 deletion application/controllers/test_bootstrap.php
@@ -1,6 +1,6 @@
<?php

class Test_bootstrap
class Test_bootstrap extends CI_Controller
{
public function index() { }
}
8 changes: 5 additions & 3 deletions application/models/Tracker_model.php
Expand Up @@ -3,10 +3,13 @@
class Tracker_model extends MY_Model
{
public $after_get = array();
public $validate = array(
array( 'field' => 'id', 'label' => 'ID', 'rules' => 'required|alpha_dash|is_unique[trackers.id]' ),
array( 'field' => 'name', 'label' => 'Name', 'rules' => 'required' ),
);

const TABLE = 1;
const GRAPH = 2;
const VISITS = 3;
const VISITS = 2;

public function presented()
{
Expand All @@ -22,7 +25,6 @@ public function present($obj)
{
switch ($obj->type)
{
case self::GRAPH: $type = 'Graph'; break;
case self::VISITS: $type = 'Visits'; break;
case self::TABLE: default: $type = 'Table'; break;
}
Expand Down
22 changes: 22 additions & 0 deletions application/presenters/TrackerPresenter.php
@@ -0,0 +1,22 @@
<?php

class TrackerPresenter extends Presenter
{
public function __construct($object, $name = FALSE)
{
parent::__construct($object, $name);

$this->ci =& get_instance();
}

public function values()
{
if (!isset($this->values))
{
$this->ci->load->model('value_model', 'value');
$this->values = $this->ci->value->getManyForTracker($this->tracker->id);
}

return $this->values;
}
}
33 changes: 2 additions & 31 deletions application/presenters/tracker/TablePresenter.php
@@ -1,38 +1,9 @@
<?php

class Tracker_TablePresenter extends Presenter
class Tracker_TablePresenter extends TrackerPresenter
{
public function values()
{
$this->ci =& get_instance();

if (!isset($this->values))
{
$this->ci->load->model('value_model', 'value');
$this->values = $this->ci->value->getManyForTracker($this->tracker->id);
}

return $this->values;
}

public function display()
{
$html = "<table>\n\t";
$html .= "<thead>\n\t\t";
$html .= "<tr><th>Value</th><th>When</th></tr>\n\t";
$html .= "</thead>\n\t";
$html .= "<tbody>\n\t\t";

$values = $this->values();

foreach ($values as $value)
{
$html .= "<tr><td>" . $value[1] . "</td><td>" . $value[0] . "</td></tr>\n\t\t";
}

$html .= "</tbody>\n";
$html .= "</table>";

return $html;
return $this->ci->load->view('presenters/table', array( 'data' => $this->values() ), TRUE);
}
}
43 changes: 42 additions & 1 deletion application/presenters/tracker/VisitsPresenter.php
@@ -1,3 +1,44 @@
<?php

class Tracker_VisitsPresenter extends Presenter { }
class Tracker_VisitsPresenter extends TrackerPresenter
{
public $dataTableHeaders = array( 'Day', 'Visits' );

public function display()
{
return $this->ci->load->view('presenters/visits', array( 'data' => $this->values() ), TRUE);
}

public function dataTableHeader()
{
return '[ \'' . implode("', '", $this->dataTableHeaders) . '\' ],';
}

public function dataTableData()
{
$dataStr = '';
$data = $this->values();
$dayCounts = array();

foreach ($data as $value)
{
$day = date('Y-m-d', strtotime($value[0]));

if (isset($dayCounts[$day]))
{
$dayCounts[$day]++;
}
else
{
$dayCounts[$day] = 1;
}
}

foreach ($dayCounts as $day => $count)
{
$dataStr .= "[ '" . $day . "', " . $count . " ],";
}

return substr($dataStr, 0, strlen($dataStr) - 1);
}
}
64 changes: 60 additions & 4 deletions application/tests/controllers/TrackersTest.php
Expand Up @@ -5,6 +5,7 @@ class TrackersTest extends PHPUnit_Framework_TestCase
public function setUp()
{
$this->trackers = new Trackers();
$this->dropdown = array( 'website_visits' => 'Website Visits' );
}

public function testModelsAreAutoloaded()
Expand All @@ -20,13 +21,12 @@ public function testInheritsFromBaseController()

public function testTrackersAreRetrieved()
{
$data = array( array( 'id' => 'website_visits', 'name' => 'Website Visits' ) );
$this->trackers->tracker = Mockery::mock('Tracker_model', array( 'get_all' => $data ));
$this->trackers->tracker = Mockery::mock('tracker model', array( 'dropdown' => $this->dropdown ));

$this->trackers->index();

$this->assertTrue(isset($this->trackers->data['trackers']), "isset(data['trackers']) is FALSE");
$this->assertEquals($data, $this->trackers->data['trackers']);
$this->assertTrue(isset($this->trackers->data['dropdown']), 'isset(data["dropdown"]) is FALSE');
$this->assertEquals($this->trackers->data['dropdown'], $this->dropdown);
}

public function testASpecificTrackerCanBeRetrieved()
Expand All @@ -45,11 +45,67 @@ public function testASpecificTrackerCanBeRetrieved()
->once()
->with('trackers')
->andReturn($result);
$this->trackers->tracker = Mockery::mock($this->trackers->tracker);
$this->trackers->tracker->shouldReceive('dropdown')
->once()
->andReturn($this->dropdown);

$this->trackers->show($data->id);

$this->assertTrue(isset($this->trackers->data['tracker']), "isset(data['tracker']) is FALSE");
$this->assertThat($this->trackers->data['tracker'], $this->isInstanceOf('Tracker_TablePresenter'));
$this->assertEquals($data, $this->trackers->data['tracker']->tracker);
}

public function testAddWillRespondToValidation()
{
$this->trackers->load->library('form_validation');

$oldModel = $this->trackers->tracker;
$this->trackers->tracker = Mockery::mock($this->trackers->tracker);
$this->trackers->form_validation = Mockery::mock($this->trackers->form_validation);

$error = 'Trackers error message';
$_POST['tracker'] = array( 'id' => 'some incorrect id', 'name' => '' );

$this->trackers->tracker->shouldReceive('insert')
->once()
->with($_POST['tracker'])
->andReturn(FALSE);
$this->trackers->form_validation->shouldReceive('error_string')
->once()
->andReturn($error);

$this->trackers->add();

$this->trackers->tracker = $oldModel;

$this->assertEquals($error, $this->trackers->data['validation_errors']);
$this->assertEquals($_POST['tracker'], $this->trackers->data['trackerData']);
}

public function testAddWillRedirectWhenSuccessful()
{
$trackers = Mockery::mock('Trackers[_redirect]');
$trackers->__construct();

$trackers->tracker = Mockery::mock(new Tracker_model());

$_POST['tracker'] = array( 'id' => 'some-correct-id', 'name' => 'Some Correct Name' );

$trackers->tracker->shouldReceive('insert')
->once()
->andReturn(TRUE);

$trackers->shouldReceive('_redirect')
->once()
->with('trackers/show/some-correct-id');

$trackers->add();
}

public function tearDown()
{
Mockery::close();
}
}
11 changes: 6 additions & 5 deletions application/tests/models/Tracker_modelTest.php
Expand Up @@ -4,8 +4,7 @@ class Tracker_modelTest extends PHPUnit_Framework_TestCase
{
public $expectedConstants = array(
'TABLE' => 1,
'GRAPH' => 2,
'VISITS' => 3
'VISITS' => 2
);

public function testConstantValues()
Expand Down Expand Up @@ -33,15 +32,12 @@ public function testPresent()
$tracker = new Tracker_model();

$tableObj = (object)array( 'type' => $this->expectedConstants['TABLE'] );
$graphObj = (object)array( 'type' => $this->expectedConstants['GRAPH'] );
$visitsObj = (object)array( 'type' => $this->expectedConstants['VISITS'] );

$tableObj = $tracker->present($tableObj);
$graphObj = $tracker->present($graphObj);
$visitsObj = $tracker->present($visitsObj);

$this->assertThat($tableObj, $this->isInstanceOf('Tracker_TablePresenter'));
$this->assertThat($graphObj, $this->isInstanceOf('Tracker_GraphPresenter'));
$this->assertThat($visitsObj, $this->isInstanceOf('Tracker_VisitsPresenter'));
}

Expand All @@ -55,4 +51,9 @@ public function testRemovePresentationRemovesAssociatedCallbacks()
$this->assertNotContains('present', $tracker->after_get);
$this->assertNotContains('removePresentation', $tracker->after_get);
}

public function tearDown()
{
Mockery::close();
}
}
3 changes: 3 additions & 0 deletions application/tests/models/Value_modelTest.php
Expand Up @@ -30,7 +30,10 @@ public function testGetManyForTracker()
},
$dbdata),
$result);
}

public function tearDown()
{
Mockery::close();
}
}
28 changes: 28 additions & 0 deletions application/tests/presenters/TrackerPresenterTest.php
@@ -0,0 +1,28 @@
<?php

class TrackerPresenterTest extends PHPUnit_Framework_TestCase
{
public function testConstructorFetchesCI()
{
$pre = new TrackerPresenter(new stdClass);

$this->assertEquals(get_instance(), $pre->ci);
}

public function testGrabsValues()
{
$data = array( 1, 2, 3 );
$obj = (object)array( 'id' => 'website_visits' );
$pre = new TrackerPresenter($obj, 'tracker');

$pre->ci =& get_instance();
$pre->ci->value = Mockery::mock('Value_model');
$pre->ci->value->shouldReceive('getManyForTracker')
->once()
->with('website_visits')
->andReturn($data);

$this->assertEquals($data, $pre->values());
$this->assertEquals($data, $pre->values());
}
}

0 comments on commit 2baf933

Please sign in to comment.