Skip to content

Commit

Permalink
Using a single 'name' instead of 'first/last' -- with tests
Browse files Browse the repository at this point in the history
* Moved names over to just 'name' and removed 'first_name/last_name' from schema
* closes #45
* Added Person unit tests
* Configured Travis Continuous Integration
* TODO: Add more tests
  • Loading branch information
David Kobia committed Oct 1, 2013
1 parent 5618873 commit 6dfc723
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ application/logs/*
application/cache/*
application/config/*.php
httpdocs/media/.sass-cache/
httpdocs/.htaccess
/vendor
33 changes: 14 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
language: php

php:
- 5.3

before_install:
- "git submodule update --init --recursive"

services:
- mysql
before_script:
- "pear channel-discover pear.phing.info"
- "pear install phing/phing"
- "phpenv rehash"
- "composer install"

script: "phing test"
- composer install --no-interaction --prefer-source # Have to prefer source or hit github rate limit
- git submodule update --init --recursive
- mkdir application/cache application/logs
- chmod 777 application/cache application/logs
# db setup
- mysql -e 'create database pingapp_test;'
- ./minion --task=migrations:run --up
# webserver setup
- php -S localhost:8000 httpdocs/index.php &
- sleep 3

notifications:
irc:
channels:
- "irc.freenode.org#kohana"
template:
- "%{repository}/%{branch} (%{commit}) - %{author}: %{message}"
- "Build details: %{build_url}"
email: false
script:
- /vendor/bin/phpunit -c application/tests/phpunit.xml
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ The default login credentials are admin / westgate
### Designers

Please refer to PING's [Design Guide](https://github.com/ushahidi/pingapp/blob/master/README-DESIGN-GUIDE.md)

### Tests

Coming soon...
8 changes: 3 additions & 5 deletions application/classes/Controller/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function action_edit()
{
// 1. Save Names
$person->values($post, array(
'first_name', 'last_name',
'name',
));
$person->check($extra_validation);

Expand Down Expand Up @@ -131,8 +131,7 @@ public function action_edit()
if ( $person->loaded() )
{
$post = array(
'first_name' => $person->first_name,
'last_name' => $person->last_name,
'name' => $person->name,
);

// Get Person Contacts
Expand Down Expand Up @@ -230,7 +229,7 @@ public function action_ajax_list()
$this->auto_render = FALSE;

// Data table columns
$columns = array('first_name', 'status', 'pings', 'last_name');
$columns = array('name', 'status', 'pings', 'last_name');

$pings = DB::select('cp.person_id', array(DB::expr('COUNT(pings.id)'), 'pings'))
->from('pings')
Expand All @@ -241,7 +240,6 @@ public function action_ajax_list()
->group_by('cp.person_id');

$query = ORM::factory('Person')
->select(array(DB::expr('CONCAT(person.first_name, " ", person.last_name)'), 'name'))
->select('pings.pings')
->join(array($pings, 'pings'), 'LEFT')
->on('person.id', '=', 'pings.person_id')
Expand Down
10 changes: 4 additions & 6 deletions application/classes/Model/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ class Model_Person extends ORM {
public function rules()
{
return array(
'first_name' => array(
'name' => array(
array('not_empty'),
array('min_length', array(':value', 2)),
array('max_length', array(':value', 100)),
array('max_length', array(':value', 150)),
),
'last_name' => array(
array('not_empty'),
array('min_length', array(':value', 2)),
array('max_length', array(':value', 100)),
'status' => array(
array('in_array', array(':value', array('ok', 'notok', 'unknown')) ),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions application/classes/PingApp/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static function people($user)
{
$people = $user->people
->where('parent_id', '=', 0)
->order_by('first_name', 'ASC')
->order_by('name', 'ASC')
->find_all();

foreach ($people as $person)
{
$array[$person->id] = $person->first_name.' '.$person->last_name;
$array[$person->id] = $person->name;
}
}

Expand Down
75 changes: 75 additions & 0 deletions application/migrations/1/20131001090904.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Migration_1_20131001090904 extends Minion_Migration_Base {

/**
* Run queries needed to apply this migration
*
* @param Kohana_Database $db Database connection
*/
public function up(Kohana_Database $db)
{
// Add a new 'name' column
$db->query(NULL, "ALTER TABLE `people` ADD `name` VARCHAR(150) NULL DEFAULT NULL AFTER `user_id`;");

// Migrate names to new column
$this->_migrate_old();

// Drop old columns
$db->query(NULL, "ALTER TABLE `people` DROP `first_name`;");
$db->query(NULL, "ALTER TABLE `people` DROP `last_name`;");
}

/**
* Run queries needed to remove this migration
*
* @param Kohana_Database $db Database connection
*/
public function down(Kohana_Database $db)
{
// Restore first_name and last_name
$db->query(NULL, "ALTER TABLE `people` ADD `first_name` VARCHAR(100) NULL DEFAULT NULL AFTER `user_id`;");
$db->query(NULL, "ALTER TABLE `people` ADD `last_name` VARCHAR(100) NULL DEFAULT NULL AFTER `first_name`;");

// Migrate names back to old columns
$this->_un_migrate_old();

// Drop name column
$db->query(NULL, "ALTER TABLE `people` DROP `name`;");
}


/**
* Migrate Old People
* @return void
*/
private function _migrate_old()
{
$people = ORM::factory('Person')
->find_all();

foreach ($people as $person)
{
$person->name = $person->first_name.' '.$person->last_name;
$person->save();
}
}

/**
* UnMigrate Old People
* @return void
*/
private function _un_migrate_old()
{
$people = ORM::factory('Person')
->find_all();

foreach ($people as $person)
{
$name = explode(' ', $person->name);
$person->first_name = $name[0];
$person->last_name = (isset($name[1])) ? $name[1] : '';
$person->save();
}
}
}
118 changes: 118 additions & 0 deletions application/tests/classes/models/PersonModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php defined('SYSPATH') or die('No direct script access allowed.');

/**
* Unit tests for the person model
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application\Tests
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
*/

class PersonModelTest extends Unittest_TestCase {
/**
* Provider for test_validate_valid
*
* @access public
* @return array
*/
public function provider_validate_valid()
{
return array(
array(
// Valid person data
array(
'name' => 'Joe Schmoe',
'status' => 'ok',
)
),
array(
// Valid person data
array(
'name' => 'Bill Murray',
'status' => 'notok',
)
),
array(
// Valid person data
array(
'name' => 'Miley Cyrus',
'status' => 'unknown',
)
)
);
}

/**
* Provider for test_validate_invalid
*
* @access public
* @return array
*/
public function provider_validate_invalid()
{
return array(
array(
// Invalid person data set 1 - No Data
array()
),
array(
// Invalid person data set 2 - Missing Name
array(
'status' => 'ok',
)
),
array(
// Invalid person data set 4 - Invalid status
array(
'name' => 'Batcave Chris',
'status' => 'not okay',
)
)
);
}

/**
* Test Validate Valid Entries
*
* @dataProvider provider_validate_valid
* @return void
*/
public function test_validate_valid($set)
{
$person = ORM::factory('Person');
$person->values($set);

try
{
$person->check();
}
catch (ORM_Validation_Exception $e)
{
$this->fail('This entry qualifies as invalid when it should be valid: '. json_encode($e->errors('models')));
}
}

/**
* Test Validate Invalid Entries
*
* @dataProvider provider_validate_invalid
* @return void
*/
public function test_validate_invalid($set)
{
$person = ORM::factory('Person');
$person->values($set);

try
{
$person->check();
}
catch (ORM_Validation_Exception $e)
{
return;
}

$this->fail('This entry qualifies as valid when it should be invalid');
}
}
7 changes: 7 additions & 0 deletions application/tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
<testsuites>
<testsuite name="PingApp Tests">
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>
7 changes: 2 additions & 5 deletions application/views/pages/person/edit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php if ( $parent AND $parent->loaded() ): ?>
<h4>Secondary Contact For: <?php echo $parent->first_name.' '.$parent->last_name; ?></h4>
<h4>Secondary Contact For: <?php echo $parent->name; ?></h4>
<?php endif; ?>

<?php if (isset($errors)): ?>
Expand Down Expand Up @@ -27,10 +27,7 @@
<legend>Name</legend>
<div class="new-name-row">
<div class="new-name-first">
<?php echo Form::input("first_name", $post['first_name'], array("id" =>"first_name", "placeholder" => "First Name", "required" => "required")); ?>
</div>
<div class="new-name-last">
<?php echo Form::input("last_name", $post['last_name'], array("id" =>"last_name", "placeholder" => "Last Name", "required" => "required")); ?>
<?php echo Form::input("name", $post['name'], array("id" =>"name", "placeholder" => "Full Name", "required" => "required")); ?>
</div>
</div>
</fieldset>
Expand Down
2 changes: 1 addition & 1 deletion application/views/pages/person/view.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h3><?php echo $person->first_name.' '.$person->last_name; ?></h3>
<h3><?php echo $person->name; ?></h3>
<a href="/person/edit/<?php echo $person->id; ?>" class="small button" id="ping-add-contact">Edit</a>
<?php
// Make sure this isn't a secondary contact
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"require": {
"phpunit/phpunit": "3.7.24",
"phpunit/phpunit": "3.7.*",
"phpunit/dbunit": ">=1.2",
"phing/phing": "dev-master",

"twilio/sdk": "dev-master"
}
}
},
"config": {
"bin-dir": "bin/"
}
}

0 comments on commit 6dfc723

Please sign in to comment.