Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esokullu committed Jan 17, 2018
1 parent 2e50db7 commit 053fc74
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Pho/Kernel/Foundation/AbstractActor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class AbstractActor extends Framework\Actor implements ParticleInterfac
public function __construct(Kernel $kernel, Framework\ContextInterface $graph)
{
parent::__construct($graph);
$this->registerSetHandler();
$this->registerHandler(
"form",
\Pho\Kernel\Foundation\Handlers\Form::class
Expand Down
1 change: 1 addition & 0 deletions src/Pho/Kernel/Foundation/AbstractGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class AbstractGraph extends Framework\Graph implements ParticleInterfac
public function __construct(Kernel $kernel, AbstractActor $actor, Framework\ContextInterface $graph)
{
parent::__construct($actor, $graph);
$this->registerSetHandler();
$this->hydrate($kernel, $graph);
$this->kernel->space()->emit("particle.added", [$this]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Pho/Kernel/Foundation/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class AbstractObject extends Framework\Object implements ParticleInterf
public function __construct(Kernel $kernel, AbstractActor $actor, Framework\ContextInterface $graph)
{
parent::__construct($actor, $graph);
$this->registerSetHandler();
$this->hydrate($kernel, $graph);
$this->kernel->space()->emit("particle.added", [$this]);

Expand Down
58 changes: 58 additions & 0 deletions src/Pho/Kernel/Foundation/Handlers/Set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of the Phở package.
*
* (c) Emre Sokullu <emre@phonetworks.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Kernel\Foundation\Handlers;

use Pho\Framework\ParticleInterface;
use Pho\Framework\FieldHelper;

/**
* Kernel adapter of the Set Handler class.
*
*
* @author Emre Sokullu <emre@phonetworks.org>
*/
class Set extends \Pho\Framework\Handlers\Set {

/**
* {@inheritDoc}
*
* @todo Find a better way of accessing kernel than calling $GLOBALS
*/
protected static function saveField(
ParticleInterface $particle,
string $field_name,
/*mixed*/ $field_value,
bool $defer_persist,
FieldHelper $helper): void
{
$kernel = $GLOBALS["kernel"];
if($helper->isUnique()) {
// check if it is unique via index
//eval(\Psy\sh());
if($kernel->live() && !$kernel->index()->checkNodeUniqueness($field_name, $field_value, $particle->label())) {
throw new \InvalidArgumentException("Given field is not unique");
}
}
elseif($helper->withIndex()) {
// check if there is an index
}
parent::saveField($particle, $field_name, $field_value, $defer_persist, $helper);

/*if(!$defer_persist) {
$particle->attributes()->$field_name = $field_value;
return;
}
$particle->attributes()->quietSet($field_name, $field_value);
*/
}

}
8 changes: 8 additions & 0 deletions src/Pho/Kernel/Foundation/ParticleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ trait ParticleTrait
protected $deferred_persistence = false;
protected $rewired = false;

protected function registerSetHandler(): void
{
$this->registerHandler(
"set",
\Pho\Kernel\Foundation\Handlers\Set::class
);
}

protected function hydrate(
Kernel $kernel,
Framework\ContextInterface $graph): void
Expand Down
15 changes: 15 additions & 0 deletions src/Pho/Kernel/Services/Index/IndexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ public function edgeDeleted(string $id): void;
*/
public function flush(): void;

/**
* Checks whether the given node exists in the database or not.
*
* @param string $field_name Field Name
* @param mixed $field_value Field Value
* @param string $label The label of the node in question
*
* @return bool
*/
public function checkNodeUniqueness(
string $field_name,
/*mixed*/ $field_value,
string $label = ""
): bool;

}
3 changes: 2 additions & 1 deletion src/Pho/Kernel/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"database" => ["type"=>"apcu", "uri"=> "" ],
"logger" => ["type"=>"stdout", "uri"=> "" ],
"storage" => ["type"=>"filesystem", "uri"=> "" ],
"events" => ["type"=>"local", "uri"=> "" ]
"events" => ["type"=>"local", "uri"=> "" ],
"index" => ["type"=>"neo4j", "uri"=> ""]
),
"tmp_path" => sys_get_temp_dir(), // Temporary folder to store files. For example uploaded files may go there.
"root_path" => __DIR__,
Expand Down
1 change: 1 addition & 0 deletions tests/Pho/Kernel/CustomFounderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ protected function startKernel($founder=null): void
$founder = new \PhoNetworksAutogenerated\User($this->kernel, $this->kernel->space(), "123456");
$this->kernel->boot($founder);
$this->graph = $this->kernel->graph();
//eval(\Psy\sh());
}
}
1 change: 1 addition & 0 deletions tests/Pho/Kernel/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

include("tests/assets/compiled/Graph.php");
include("tests/assets/compiled/User.php");
include("tests/assets/compiled/UserWithUniqueFeatures.php");
include("tests/assets/compiled/Status.php");
include("tests/assets/compiled/StatusOut/Mention.php");
include("tests/assets/compiled/UserOut/Follow.php");
Expand Down
74 changes: 74 additions & 0 deletions tests/Pho/Kernel/UniqueFieldsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <emre@phonetworks.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Kernel;

class UniqueFieldsTest extends TestCase
{

protected function getKernelConfig()
{
$configs = parent::getKernelConfig();
$configs["default_objects"] = [
"graph" => \PhoNetworksAutogenerated\Graph::class,
"founder" => \PhoNetworksAutogenerated\UserWithUniqueFeatures::class,
];
return $configs;
}

protected function startKernel($founder=null): void
{
$this->configs = $this->getKernelConfig();
$this->kernel = new Kernel($this->configs);
$founder = new \PhoNetworksAutogenerated\UserWithUniqueFeatures($this->kernel, $this->kernel->space(), "esokullu", "123456");
$this->kernel->boot($founder);

}


public function testSucceed() {
$this->flushDBandRestart();
$u = new \PhoNetworksAutogenerated\UserWithUniqueFeatures($this->kernel, $this->kernel->graph(), "another_username", "123456");
$this->created[] = $u->id();
$this->kernel->founder()->setBirthday("12/21/1983");
$u->setBirthday("12/21/1983");
$this->assertEquals($this->kernel->founder()->getBirthday(), $u->getBirthday());
//eval(\Psy\sh());
$this->assertTrue(true); // if it has come to this point with no exception, we're good
}

public function testFailDueToDuplicate() {
$this->flushDBandRestart();
$this->expectException(\InvalidArgumentException::class);
$u = new \PhoNetworksAutogenerated\UserWithUniqueFeatures($this->kernel, $this->kernel->graph(), "esokullu", "123456");
$this->created[] = $u->id();
}

public function testAnotherFieldWithSameValueSucceed() {
$this->flushDBandRestart();
$u = new \PhoNetworksAutogenerated\UserWithUniqueFeatures($this->kernel, $this->kernel->graph(), "another_username", "123456");
$this->created[] = $u->id();
$u->setAbout("esokullu");
$this->assertEquals($this->kernel->founder()->getUsername(), $u->getAbout());
//eval(\Psy\sh());
$this->assertTrue(true); // if it has come to this point with no exception, we're good
}


/*
// @todo
public function testUniquenesswithDistinctLabel() {
// ?
}
*/


}
13 changes: 8 additions & 5 deletions tests/assets/compiled/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class User extends Foundation\AbstractActorDP {
const DEFAULT_MOD = 0x07554;
const DEFAULT_MASK = 0xfffff;

const FIELDS = "{\"password\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":\"^[a-zA-Z0-9_]{4,12}$\",\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":true,\"now\":false,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"join_time\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":true,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"birthday\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":false,\"default\":411436800}},\"about\":{\"constraints\":{\"minLength\":null,\"maxLength\":\"255\",\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":false,\"default\":\"\"}}}";
const FIELDS = "{\"password\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":\"^[a-zA-Z0-9_]{4,12}$\",\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":true,\"now\":false,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"join_time\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":true,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"birthday\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":false,\"default\":411436800}},\"about\":{\"constraints\":{\"minLength\":null,\"maxLength\":\"255\",\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{ \"md5\":false,\"now\":false,\"default\":\"\"}}}";

public function __construct(\Pho\Kernel\Kernel $kernel, \Pho\Lib\Graph\GraphInterface $graph , string $password, ?int $birthday = 411436800, ?string $about = "")
{
Expand All @@ -34,10 +34,13 @@ public function __construct(\Pho\Kernel\Kernel $kernel, \Pho\Lib\Graph\GraphInte
$this->registerIncomingEdges(UserOut\Consume::class);
parent::__construct($kernel, $graph);
$this->setPassword($password, true);
$this->setJoinTime(time(), true);
$this->setBirthday($birthday, true);
$this->setAbout($about, true);


$this->setJoinTime(time(), true);

$this->setBirthday($birthday, true);

$this->setAbout($about, true);

$this->persist();
}

Expand Down
53 changes: 53 additions & 0 deletions tests/assets/compiled/UserWithUniqueFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* This is a customized version of User.php
* with a unique feature; username.
*/

namespace PhoNetworksAutogenerated;

use Pho\Framework;
use Pho\Kernel\Kernel;
use Pho\Kernel\Traits;
use Pho\Kernel\Foundation;




/*****************************************************
* This file was auto-generated by pho-compiler
* For more information, visit http://phonetworks.org
******************************************************/

class UserWithUniqueFeatures extends User {

const T_EDITABLE = false;
const T_PERSISTENT = true;
const T_EXPIRATION = 0;
const T_VERSIONABLE = false;

const DEFAULT_MOD = 0x07554;
const DEFAULT_MASK = 0xfffff;

// added username with 3 chars min, 20 chars max, and must be unique!
const FIELDS = "{\"username\":{\"constraints\":{\"minLength\":3,\"maxLength\":20,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"unique\":true, \"md5\":false,\"now\":false,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"password\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":\"^[a-zA-Z0-9_]{4,12}$\",\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":true,\"now\":false,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"join_time\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":true,\"default\":\"|_~_~NO!-!VALUE!-!SET~_~_|\"}},\"birthday\":{\"constraints\":{\"minLength\":null,\"maxLength\":null,\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{\"md5\":false,\"now\":false,\"default\":411436800}},\"about\":{\"constraints\":{\"minLength\":null,\"maxLength\":\"255\",\"uuid\":null,\"regex\":null,\"greaterThan\":null,\"lessThan\":null},\"directives\":{ \"md5\":false,\"now\":false,\"default\":\"\"}}}";

public function __construct(\Pho\Kernel\Kernel $kernel, \Pho\Lib\Graph\GraphInterface $graph , string $username, string $password, ?int $birthday = 411436800, ?string $about = "")
{
parent::__construct($kernel, $graph, $password, $birthday, $about);

$this->setUsername($username, true);


$this->persist();
}

}

/*****************************************************
* Timestamp: 1501461738
* Size (in bytes): 2346
* Compilation Time: 3434
* e64cf8335098e7a03ebd665e7c424c27
******************************************************/

0 comments on commit 053fc74

Please sign in to comment.