-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
223 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
// ? | ||
} | ||
*/ | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
******************************************************/ |