Skip to content

Instantly share code, notes, and snippets.

@diogok
Created August 7, 2013 21:24
Show Gist options
  • Save diogok/6178858 to your computer and use it in GitHub Desktop.
Save diogok/6178858 to your computer and use it in GitHub Desktop.
<?php
namespace cncflora\repository;
class Profiles {
public $user = null;
public $couchdb = null;
public $db = null;
public function __construct($user=null) {
Utils::config();
$this->user = $user;
$this->couchdb = new \Nano\Nano('http://'.COUCH_USER.":".COUCH_PASS."@".COUCH_HOST.":".COUCH_PORT);
$this->db = $this->couchdb->db->use(COUCH_BASE);
}
public function create($taxon) {
if(!isset($this->user)) {
throw new \Exception("Need a user");
}
$metadata = new \StdClass;
$metadata->status = "open";
$metadata->contributor = $this->user->name;
$metadata->contact = $this->user->email;
$metadata->creator = $this->user->name;
$metadata->created = time();
$metadata->modified = time();
$metadata->description = "Profile for ".$taxon->scientificName;
$metadata->title = "Profile for ".$taxon->scientificName;
$metadata->source = "cncflora";
$metadata->type = "profile";
$metadata->valid = false;
$metadata->identifier = "urn:lsid:cncflora.jbrj.gov.br:profile:".str_replace(' ',':',strtolower($taxon->scientificName)).":".time();
$profile = new \StdClass;
$profile->taxon = $taxon;
$profile->metadata = $metadata;
$profile->_id = $metadata->identifier;
$r = $this->db->insert($profile,$profile->_id);
$profile->_rev = $r->rev;
return $profile;
}
public function get($id) {
$obj = $this->db->get($id);
if(!isset($obj->error)) {
return $obj;
} else {
return null;
}
}
public function update($profile) {
$metadata = $profile->metadata;
if(strpos($metadata->contact,$this->user->email) === false) {
$metadata->contributor = $this->user->name ." ; ".$metadata->contributor;
$metadata->contact = $this->user->email ." ; ".$metadata->contact;
}
$metadata->modified = time();
$r = $this->db->insert($profile,$profile->_id);
$profile->_rev = $r->rev;
return $profile;
}
public function delete($profile) {
return $this->db->destroy($profile->_id,$profile->_rev);
}
}
<?php
namespace cncflora\repository;
include_once 'vendor/autoload.php';
class ProfilesTest extends \PHPUnit_Framework_TestCase {
public function setup() {
$this->user = new \StdClass;
$this->user->name = "Foo";
$this->user->email = "[email protected]";
}
public function testProfiles() {
$repo = new Profiles();
$this->assertEquals($repo,$repo);
}
/**
* @expectedException Exception
*/
public function testMustHaveUser() {
$repo = new Profiles();
$taxons = (new Species)->getSpecies('ACANTHACEAE');
$taxon = $taxons[0];
$profile = $repo->create($taxon);
}
public function testCRUD() {
$repo = new Profiles($this->user);
$taxons = (new Species)->getSpecies('ACANTHACEAE');
$taxon = $taxons[0];
$profile = $repo->create($taxon);
$this->assertNotNull($profile);
$this->assertNotNull($profile->_id);
$this->assertEquals($profile->taxon,$taxon);
$profilePersisted = $repo->get($profile->_id);
$this->assertNotNull($profilePersisted);
$this->assertEquals($profile,$profilePersisted);
$profile->ecology = new \StdClass;
$profile->ecology->resume = "Hello, World!";
$repo->update($profile);
$profilePersisted = $repo->get($profile->_id);
$this->assertEquals($profilePersisted->ecology->resume,"Hello, World!");
$repo->delete($profile);
$profilePersisted = $repo->get($profile->_id);
$this->assertNull($profilePersisted);
}
public function testMetadata() {
$this->markTestIncomplete();
}
public function testPermissions() {
$this->markTestIncomplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment