diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a3872a8..08fa48f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Parable PHP Framework Changelog +### 0.12.14 + +__Changes__ +- `/bin/vendor/parable`, the command-line tool, now shows the Parable version. +- `\Parable\Rights\Rights` has an improved `combine()` method and gained `getRightsFromNames()` and `getNamesFromRights()` methods, so it's easier to work with. +- `\Parable\Framework\App` has lost its dependency on `\Parable\Filesystem\Path`, since it became obsolete after recent changes. +- `\Parable\ORM\Query` now has `addValues()`, so you can add an array of values instead of having to do them one-by-one. +- `\Parable\ORM\Repository` now has `createInstanceForModelName()`, which is now what `Toolkit::getRepository` calls as well. Toolkit's `getRepository` is sticking around, since it's more useful in views. +- Small fixes to increase code base quality. + +__Bugfixes__ +- The `parable init-structure` command used a hard-coded vendor path, which could eventually cause problems. +- Fixed tests up so that all components have 100% code coverage without `Framework` touching all the things. Also improved and simplified some tests. + ### 0.12.13 __Changes__ diff --git a/parable b/parable index 4284c4ef..fccc36e3 100644 --- a/parable +++ b/parable @@ -5,7 +5,7 @@ ini_set('display_errors', '1'); /** @var \Parable\Console\App $app */ $app = require_once(__DIR__ . '/src/Framework/Bootstrap.php'); -$app->setName('Parable'); +$app->setName("Parable " . \Parable\Framework\App::PARABLE_VERSION); // Always add Help & Init $app->addCommand(\Parable\DI\Container::get(\Parable\Console\Command\Help::class)); diff --git a/src/Console/Command/Help.php b/src/Console/Command/Help.php index 06f2066e..91b8039f 100644 --- a/src/Console/Command/Help.php +++ b/src/Console/Command/Help.php @@ -12,9 +12,10 @@ class Help extends \Parable\Console\Command public function run() { - $this->output->writeln("{$this->app->getName()} help"); - $this->output->writeln('--------------------------------------------------'); - $this->output->writeln('Available commands:'); + $this->output->newline(); + $this->output->writeln("{$this->app->getName()} " . str_repeat(" ", 36) . "command-line tool"); + $this->output->writeln(str_repeat("-", 69)); + $this->output->writeln('Help screen - available commands:'); $this->output->newline(); $longestName = 0; diff --git a/src/Framework/App.php b/src/Framework/App.php index 688d70f8..3487cb7d 100644 --- a/src/Framework/App.php +++ b/src/Framework/App.php @@ -4,6 +4,8 @@ class App { + const PARABLE_VERSION = '0.12.14'; + const HOOK_HTTP_404 = "parable_http_404"; const HOOK_HTTP_200 = "parable_http_200"; const HOOK_RESPONSE_SEND = "parable_response_send"; @@ -17,10 +19,6 @@ class App const HOOK_INIT_DATABASE_BEFORE = "parable_init_database_before"; const HOOK_INIT_DATABASE_AFTER = "parable_init_database_after"; - - /** @var \Parable\Filesystem\Path */ - protected $path; - /** @var \Parable\Framework\Config */ protected $config; @@ -48,11 +46,7 @@ class App /** @var \Parable\ORM\Database */ protected $database; - /** @var string */ - protected $version = '0.12.13'; - public function __construct( - \Parable\Filesystem\Path $path, \Parable\Framework\Config $config, \Parable\Framework\Dispatcher $dispatcher, \Parable\Framework\Toolkit $toolkit, @@ -63,7 +57,6 @@ public function __construct( \Parable\GetSet\Session $session, \Parable\ORM\Database $database ) { - $this->path = $path; $this->config = $config; $this->dispatcher = $dispatcher; $this->toolkit = $toolkit; @@ -221,6 +214,6 @@ protected function dispatchRoute(\Parable\Routing\Route $route) */ public function getVersion() { - return $this->version; + return self::PARABLE_VERSION; } } diff --git a/src/Framework/Command/InitStructure.php b/src/Framework/Command/InitStructure.php index 2e764401..3d300980 100644 --- a/src/Framework/Command/InitStructure.php +++ b/src/Framework/Command/InitStructure.php @@ -13,11 +13,16 @@ class InitStructure extends \Parable\Console\Command /** @var \Parable\Filesystem\Path */ protected $path; + /** @var string */ + protected $vendor_path; + public function __construct( \Parable\Filesystem\Path $path ) { $this->addOption("homeDir", false, true, "public"); $this->path = $path; + + $this->vendor_path = __DIR__ . "/../../.."; } /** @@ -86,62 +91,65 @@ public function run() $this->output->write('Copying files: '); copy( - $this->path->getDir("vendor/devvoh/parable/structure/.htaccess"), + $this->path->getDir("{$this->vendor_path}/structure/.htaccess"), $this->path->getDir(".htaccess") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/public/.htaccess"), + $this->path->getDir("{$this->vendor_path}/structure/public/.htaccess"), $this->path->getDir("{$homeDir}/.htaccess") ); $this->output->write('.'); - copy( - $this->path->getDir("vendor/devvoh/parable/structure/public/index.php_struct"), - $this->path->getDir("{$homeDir}/index.php") - ); + + // For index.php, we do it a bit differently, since we need to alter the content + $content = file_get_contents($this->path->getDir("{$this->vendor_path}/structure/public/index.php_struct")); + $content = str_replace("###VENDOR_PATH###", $this->vendor_path, $content); + file_put_contents($this->path->getDir("{$homeDir}/index.php"), $content); $this->output->write('.'); + + // And we continue copying files copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Command/HelloWorld.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Command/HelloWorld.php_struct"), $this->path->getDir("app/Command/HelloWorld.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Config/App.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Config/App.php_struct"), $this->path->getDir("app/Config/App.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Config/Custom.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Config/Custom.php_struct"), $this->path->getDir("app/Config/Custom.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Controller/Home.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Controller/Home.php_struct"), $this->path->getDir("app/Controller/Home.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Init/Example.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Init/Example.php_struct"), $this->path->getDir("app/Init/Example.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Model/User.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Model/User.php_struct"), $this->path->getDir("app/Model/User.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/Routing/App.php_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/Routing/App.php_struct"), $this->path->getDir("app/Routing/App.php") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/View/Home/index.phtml_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/View/Home/index.phtml_struct"), $this->path->getDir("app/View/Home/index.phtml") ); $this->output->write('.'); copy( - $this->path->getDir("vendor/devvoh/parable/structure/app/View/Home/test.phtml_struct"), + $this->path->getDir("{$this->vendor_path}/structure/app/View/Home/test.phtml_struct"), $this->path->getDir("app/View/Home/test.phtml") ); $this->output->write('.'); @@ -157,6 +165,7 @@ public function run() $htaccess = str_replace("public/$1", "{$homeDir}/$1", $htaccess); file_put_contents($this->path->getDir('.htaccess'), $htaccess); } + $this->output->write('.'); $this->output->writeln(" OK"); diff --git a/src/Framework/Toolkit.php b/src/Framework/Toolkit.php index 628037d4..0795505c 100644 --- a/src/Framework/Toolkit.php +++ b/src/Framework/Toolkit.php @@ -37,14 +37,7 @@ public function __construct( */ public function getRepository($modelName) { - /** @var \Parable\ORM\Model $model */ - $model = \Parable\DI\Container::create($modelName); - - /** @var \Parable\ORM\Repository $repository */ - $repository = \Parable\DI\Container::create(\Parable\ORM\Repository::class); - - $repository->setModel($model); - return $repository; + return \Parable\ORM\Repository::createInstanceForModelName($modelName); } /** diff --git a/src/Http/Request.php b/src/Http/Request.php index 3f5a5c85..db0a50fc 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -139,7 +139,7 @@ public function getScheme() return $_SERVER["REDIRECT_REQUEST_SCHEME"]; } if (isset($_SERVER["HTTP_X_FORWARDED_PROTO"])) { - // Sometimes available in proxied requests + // Sometimes available in proxy-forwarded requests return $_SERVER["HTTP_X_FORWARDED_PROTO"]; } if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") { @@ -147,7 +147,7 @@ public function getScheme() return "https"; } if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) { - // Hacky but this is our last attempt, so why not + // This doesn't say much, but this is our last attempt, so why not try return "https"; } return "http"; diff --git a/src/Http/Response.php b/src/Http/Response.php index bc32525a..7645da72 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -4,6 +4,9 @@ class Response { + /** @var \Parable\Http\Request */ + protected $request; + /** @var int */ protected $httpCode = 200; @@ -84,14 +87,12 @@ class Response /** @var bool */ protected $shouldTerminate = true; - /** - * By default we're going to set the Html Output type. - */ public function __construct( \Parable\Http\Request $request ) { $this->request = $request; + // By default we're going to set the Html Output, but this can be switched at any time before sending.. $this->setOutput(new \Parable\Http\Output\Html); } diff --git a/src/ORM/Database.php b/src/ORM/Database.php index 52d5d482..8ec25a8b 100644 --- a/src/ORM/Database.php +++ b/src/ORM/Database.php @@ -288,7 +288,7 @@ public function setInstance(\PDO $instance) } /** - * If an instance is available, quote/escape the message through PDO's quote function + * If an instance is available, quote/escape the message through PDOs quote function * * @param string $string * diff --git a/src/ORM/Query.php b/src/ORM/Query.php index f5420ac6..94bc17fd 100644 --- a/src/ORM/Query.php +++ b/src/ORM/Query.php @@ -326,6 +326,19 @@ public function addValue($key, $value) return $this; } + /** + * @param array $values + * + * @return $this + */ + public function addValues(array $values) + { + foreach ($values as $key => $value) { + $this->addValue($key, $value); + } + return $this; + } + /** * Sets the order for select queries * diff --git a/src/ORM/Repository.php b/src/ORM/Repository.php index 4d55e460..9d103fa5 100644 --- a/src/ORM/Repository.php +++ b/src/ORM/Repository.php @@ -296,4 +296,21 @@ protected function handleResult(array $result) } return $entities; } + + /** + * @param string $modelName + * + * @return \Parable\ORM\Repository + */ + public static function createInstanceForModelName($modelName) + { + $model = \Parable\DI\Container::create($modelName); + + /** @var \Parable\ORM\Repository $repository */ + $repository = \Parable\DI\Container::create(static::class); + + $repository->setModel($model); + + return $repository; + } } diff --git a/src/Rights/Rights.php b/src/Rights/Rights.php index ab881411..ea654b8c 100644 --- a/src/Rights/Rights.php +++ b/src/Rights/Rights.php @@ -87,16 +87,44 @@ public function check($provided, $name) */ public function combine(array $rights) { - $return = []; + $right_combined = str_repeat(0, count($this->rights)); foreach ($rights as $right) { - for ($i = 0; $i < strlen($right); $i++) { - if ($right[$i] == '1') { - $return[$i] = '1'; - } elseif ($right[$i] !== 1 && !isset($return[$i])) { - $return[$i] = 0; - } + $right_combined |= $right; + } + return $right_combined; + } + + /** + * Turn an array of right names (["read", "create"]) into a binary string of rights ("0011") + * + * @param string[] $names + * + * @return string + */ + public function getRightsFromNames(array $names) + { + $rights_string = ""; + foreach ($this->getRights() as $right => $value) { + $rights_string .= in_array($right, $names) ? "1" : "0"; + } + return strrev($rights_string); + } + + /** + * Turn a binary string of rights ("0011") into an array of right names (["read", "create"]) + * + * @param string $rights + * + * @return string[] + */ + public function getNamesFromRights($rights) + { + $names = []; + foreach ($this->rights as $name => $value) { + if ($this->check($rights, $name)) { + $names[] = $name; } } - return implode($return); + return $names; } } diff --git a/structure/app/View/Home/index.phtml_struct b/structure/app/View/Home/index.phtml_struct index 8b3d5724..f0d4aa45 100644 --- a/structure/app/View/Home/index.phtml_struct +++ b/structure/app/View/Home/index.phtml_struct @@ -46,7 +46,7 @@ font-size: 4.15rem; } .container aside { - font-size: 0.73rem; + font-size: 0.76rem; margin-top: -0.8rem; letter-spacing: 5px; margin-left: 3px; diff --git a/structure/public/index.php_struct b/structure/public/index.php_struct index 664688e5..5019641c 100644 --- a/structure/public/index.php_struct +++ b/structure/public/index.php_struct @@ -1,7 +1,7 @@ ERROR: You need to run
composer install
before Parable will work."); } diff --git a/tests/Base.php b/tests/Base.php index 0b545f3e..f1f0a548 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -4,6 +4,14 @@ abstract class Base extends \PHPUnit\Framework\TestCase { + protected function setUp() + { + parent::setUp(); + + // This key might be handy to have + $GLOBALS['_SESSION'] = []; + } + /** * Set $value on $object->$propertyName, even if it's private * or protected. diff --git a/tests/Components/Console/Command/HelpTest.php b/tests/Components/Console/Command/HelpTest.php index 4f7eb525..72c9b3a8 100644 --- a/tests/Components/Console/Command/HelpTest.php +++ b/tests/Components/Console/Command/HelpTest.php @@ -31,12 +31,19 @@ public function testRun() { $this->helpCommand->run(); - $this->assertSame("\e[0;33m help\e[0m\e[0m ---------------------------------------------------\e[0m -Available commands:\e[0m + $expectedOutput = <<getActualOutputAndClean()); + +EOT; + $this->assertSame( + $expectedOutput, + $this->getActualOutputAndClean() + ); } } diff --git a/tests/Components/Framework/AppTest.php b/tests/Components/Framework/AppTest.php index 60c51a03..5bfdf330 100644 --- a/tests/Components/Framework/AppTest.php +++ b/tests/Components/Framework/AppTest.php @@ -11,7 +11,7 @@ class AppTest extends \Parable\Tests\Components\Framework\Base protected $config = []; /** @var \Parable\Routing\Router */ - protected $mockRouter; + protected $router; /** @var \Parable\Http\Response|\PHPUnit_Framework_MockObject_MockObject */ protected $mockResponse; @@ -19,24 +19,10 @@ class AppTest extends \Parable\Tests\Components\Framework\Base /** @var \Parable\GetSet\Session|\PHPUnit_Framework_MockObject_MockObject */ protected $mockSession; - /** @var \Parable\Filesystem\Path */ - protected $mockPath; - - /** @var \Parable\Framework\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $mockConfig; - - /** @var \Parable\Framework\Dispatcher */ - protected $mockDispatcher; - protected function setUp() { parent::setUp(); - // We need to 'prepare' some classes since they'll expect files elsewhere - $this->mockPath = new \Parable\Filesystem\Path(); - $existingPath = \Parable\DI\Container::get(\Parable\Filesystem\Path::class); - $this->mockPath->setBasedir(realpath($existingPath->getBaseDir() . '/structure')); - /** @var \Parable\Http\Response $this->mockResponse */ // Response should not actually terminate $this->mockResponse = $this->createPartialMock(\Parable\Http\Response::class, ['terminate']); @@ -51,21 +37,8 @@ protected function setUp() \Parable\DI\Container::store($this->mockSession, \Parable\GetSet\Session::class); - $this->mockDispatcher = new \Parable\Framework\Dispatcher( - \Parable\DI\Container::get(\Parable\Event\Hook::class), - $this->mockPath, - \Parable\DI\Container::get(\Parable\Framework\View::class), - \Parable\DI\Container::get(\Parable\Http\Response::class) - ); - - // Router is not a Framework class but does depend on other Parable Components - $this->mockRouter = new \Parable\Routing\Router( - \Parable\DI\Container::get(\Parable\Http\Request::class), - \Parable\DI\Container::get(\Parable\Http\Url::class), - $this->mockPath - ); - - $this->mockRouter->addRoute('index', [ + $this->router = \Parable\DI\Container::get(\Parable\Routing\Router::class); + $this->router->addRoute('index', [ 'methods' => ['GET'], 'url' => '/', 'controller' => \Parable\Tests\TestClasses\Controller::class, @@ -103,7 +76,7 @@ public function testAppRunWithUnknownUrlGives404() public function testAppRunWithSimpleUrlWorks() { $_GET['url'] = '/simple'; - $this->mockRouter->addRoute( + $this->router->addRoute( 'simple', [ 'methods' => ['GET'], @@ -126,7 +99,7 @@ public function testAppRunWithTemplatedUrlWorks() $path = \Parable\DI\Container::get(\Parable\Filesystem\Path::class); $_GET['url'] = '/template'; - $this->mockRouter->addRoute( + $this->router->addRoute( 'template', [ 'methods' => ['GET'], @@ -150,7 +123,7 @@ public function testLoadRoutesThrowsExceptionWhenInvalidRouteIsAdded() $this->expectException(\Parable\Routing\Exception::class); $this->expectExceptionMessage("Either a controller/action combination or callable is required."); - $this->mockRouter->addRoute( + $this->router->addRoute( 'simple', [ 'methods' => ['GET'], @@ -164,7 +137,7 @@ public function testLoadRoutesThrowsExceptionWhenInvalidRouteIsAdded() public function testAppRunWithValuedUrlWorks() { $_GET['url'] = '/valued/985'; - $this->mockRouter->addRoute( + $this->router->addRoute( 'valued', [ 'methods' => ['GET'], @@ -187,7 +160,7 @@ public function testAppThrowsExceptionOnInvalidRoute() $this->expectException(\Parable\Routing\Exception::class); $this->expectExceptionMessage("Either a controller/action combination or callable is required."); - $this->mockRouter->addRoute('simple', [ + $this->router->addRoute('simple', [ 'methods' => ['GET'], 'url' => '/', ]); @@ -218,17 +191,8 @@ protected function createApp($mainConfigClassName = \Parable\Tests\TestClasses\C $config = new \Parable\Framework\Config($this->path); $config->setMainConfigClassName($mainConfigClassName); - return new \Parable\Framework\App( - $this->mockPath, - $config, - $this->mockDispatcher, - \Parable\DI\Container::get(\Parable\Framework\Toolkit::class), - \Parable\DI\Container::get(\Parable\Event\Hook::class), - $this->mockRouter, - \Parable\DI\Container::get(\Parable\Http\Response::class), - \Parable\DI\Container::get(\Parable\Http\Url::class), - \Parable\DI\Container::get(\Parable\GetSet\Session::class), - \Parable\DI\Container::get(\Parable\ORM\Database::class) - ); + \Parable\DI\Container::store($config); + + return \Parable\DI\Container::create(\Parable\Framework\App::class); } } diff --git a/tests/Components/GetSet/CookieTest.php b/tests/Components/GetSet/CookieTest.php index 64b1e32b..57530fa7 100644 --- a/tests/Components/GetSet/CookieTest.php +++ b/tests/Components/GetSet/CookieTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testGetSetOnResource() { - $this->getSet->reset(); - $this->getSet->set("stuff", "here"); $this->assertSame( diff --git a/tests/Components/GetSet/GetSetTest.php b/tests/Components/GetSet/GetSetTest.php index e1c04065..bbd31c0c 100644 --- a/tests/Components/GetSet/GetSetTest.php +++ b/tests/Components/GetSet/GetSetTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testSetAllGetAll() { - $this->getSet->reset(); - $this->getSet->setAll([ 'key1' => 'yo1', 'key2' => 'yo2', @@ -39,8 +37,6 @@ public function testSetAllGetAll() public function testGetAllAndReset() { - $this->getSet->reset(); - $this->getSet->setAll([ 'key1' => 'yo1', 'key2' => 'yo2', @@ -59,8 +55,6 @@ public function testGetAllAndReset() public function testGetAndRemove() { - $this->getSet->reset(); - $this->getSet->setAll([ 'key1' => 'yo1', 'key2' => 'yo2', @@ -80,10 +74,26 @@ public function testGetAndRemove() $this->assertSame(1, $this->getSet->count()); } - public function testSetSpecificGetSpecificAndGetAll() + public function testRemoveNonExistingKeyDoesNothing() { - $this->getSet->reset(); + $this->getSet->setMany([ + 'key1' => 'yo1', + 'key2' => 'yo2', + 'key3' => 'yo3', + ]); + + $this->assertSame(3, $this->getSet->count()); + + $this->assertInstanceOf( + \Parable\GetSet\Base::class, + $this->getSet->remove("stuff") + ); + + $this->assertSame(3, $this->getSet->count()); + } + public function testSetSpecificGetSpecificAndGetAll() + { $this->getSet->set('key1', 'yo1'); $this->getSet->set('key2', 'yo2'); $this->getSet->set('key3', 'yo3'); @@ -102,8 +112,6 @@ public function testSetSpecificGetSpecificAndGetAll() public function testSetAllVersusSetMany() { - $this->getSet->reset(); - $this->assertCount(0, $this->getSet->getAll()); $this->getSet->setAll([ @@ -161,8 +169,6 @@ public function testGetAllReturnsEmptyArrayIfNoResourceSet() public function testGetSetAndRemoveWithHierarchalKeys() { - $this->getSet->reset(); - $this->getSet->set("one", ["this" => "should stay"]); $this->getSet->set("one.two.three.four", "totally nested, yo"); @@ -225,4 +231,24 @@ public function testGetSetAndRemoveWithHierarchalKeys() $this->getSet->getAll() ); } + + public function testRemoveHierarchalKey() + { + $this->getSet->set("one.two.three", "totally"); + $this->getSet->set("one.two.four", "also"); + + $this->assertCount(2, $this->getSet->get("one.two")); + + $this->getSet->remove("one.two.three"); + + $this->assertCount(1, $this->getSet->get("one.two")); + $this->assertTrue(is_array($this->getSet->get("one.two"))); + + $this->getSet->remove("one.two"); + + $this->assertNull($this->getSet->get("one.two")); + + // But one should be untouched and still an array + $this->assertTrue(is_array($this->getSet->get("one"))); + } } diff --git a/tests/Components/GetSet/GetTest.php b/tests/Components/GetSet/GetTest.php index 340c2280..b5e1e6b3 100644 --- a/tests/Components/GetSet/GetTest.php +++ b/tests/Components/GetSet/GetTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testGetSetOnResource() { - $this->getSet->reset(); - $this->getSet->set("stuff", "here"); $this->assertSame( @@ -40,8 +38,6 @@ public function testGetSetOnResource() public function testSetAndRemove() { - $this->getSet->reset(); - $this->getSet->set('test', 'value'); $this->assertSame('value', $this->getSet->get('test')); @@ -50,4 +46,11 @@ public function testSetAndRemove() $this->assertNull($this->getSet->get('test')); } + + public function tearDown() + { + parent::tearDown(); + + $this->getSet->reset(); + } } diff --git a/tests/Components/GetSet/InternalTest.php b/tests/Components/GetSet/InternalTest.php index 94a4be2f..7e3fbbb3 100644 --- a/tests/Components/GetSet/InternalTest.php +++ b/tests/Components/GetSet/InternalTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testGetSetOnResource() { - $this->getSet->reset(); - $this->getSet->set("stuff", "here"); $this->assertSame( diff --git a/tests/Components/GetSet/PostTest.php b/tests/Components/GetSet/PostTest.php index b85902f0..2c69cf84 100644 --- a/tests/Components/GetSet/PostTest.php +++ b/tests/Components/GetSet/PostTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testGetSetOnResource() { - $this->getSet->reset(); - $this->getSet->set("stuff", "here"); $this->assertSame( diff --git a/tests/Components/GetSet/SessionTest.php b/tests/Components/GetSet/SessionTest.php index 8594eca3..0a69f9dd 100644 --- a/tests/Components/GetSet/SessionTest.php +++ b/tests/Components/GetSet/SessionTest.php @@ -21,8 +21,6 @@ public function testGetResource() public function testGetSetOnResource() { - $this->getSet->reset(); - $this->getSet->set("stuff", "here"); $this->assertSame( diff --git a/tests/Components/ORM/QueryTest.php b/tests/Components/ORM/QueryTest.php index c35c87f0..e4809581 100644 --- a/tests/Components/ORM/QueryTest.php +++ b/tests/Components/ORM/QueryTest.php @@ -209,7 +209,25 @@ public function testInsert() $this->query->addValue('active', 1); $this->query->addValue('thing', null); - $this->assertSame("INSERT INTO `user` (`name`, `active`, `thing`) VALUES ('test', '1', NULL);", (string)$this->query); + $this->assertSame( + "INSERT INTO `user` (`name`, `active`, `thing`) VALUES ('test', '1', NULL);", + (string)$this->query + ); + } + + public function testInsertWithAddValues() + { + $this->query->setAction('insert'); + $this->query->addValues([ + 'name' => 'test', + 'active' => 1, + 'thing' => null, + ]); + + $this->assertSame( + "INSERT INTO `user` (`name`, `active`, `thing`) VALUES ('test', '1', NULL);", + (string)$this->query + ); } public function testUpdate() @@ -223,7 +241,10 @@ public function testUpdate() $this->query->addValue('active', 1); $this->query->addValue('thing', null); - $this->assertSame("UPDATE `user` SET `name` = 'test', `active` = '1', `thing` = NULL WHERE `user`.`id` = '3';", (string)$this->query); + $this->assertSame( + "UPDATE `user` SET `name` = 'test', `active` = '1', `thing` = NULL WHERE `user`.`id` = '3';", + (string)$this->query + ); } public function testSelectGivesEmptyStringOnNoSelect() diff --git a/tests/Components/ORM/RepositoryTest.php b/tests/Components/ORM/RepositoryTest.php index 691d2a15..e9c95f77 100644 --- a/tests/Components/ORM/RepositoryTest.php +++ b/tests/Components/ORM/RepositoryTest.php @@ -247,4 +247,16 @@ public function testBuildAndOrSets() $this->assertInstanceOf(\Parable\ORM\Query\Condition\OrSet::class, $set); $this->assertSame(\Parable\ORM\Query\ConditionSet::SET_OR, $set::TYPE); } + + public function testCreateInstanceForModelName() + { + $repository = \Parable\ORM\Repository::createInstanceForModelName(\Parable\Tests\TestClasses\Model::class); + + $this->assertInstanceOf( + \Parable\Tests\TestClasses\Model::class, + $repository->getModel() + ); + + $this->assertCount(3, $this->repository->getAll()); + } } diff --git a/tests/Components/Rights/RightsTest.php b/tests/Components/Rights/RightsTest.php index 40e79dd9..07e70ad9 100644 --- a/tests/Components/Rights/RightsTest.php +++ b/tests/Components/Rights/RightsTest.php @@ -68,7 +68,35 @@ public function testCheckRight() public function testCombine() { - $value = $this->rights->combine(['10000', '00001']); - $this->assertSame('10001', $value); + $value = $this->rights->combine(['10000', '00001', "00100"]); + $this->assertSame('10101', $value); + } + + public function testGetNamesFromRights() + { + $this->assertSame( + ["create", "update"], + $this->rights->getNamesFromRights("0101") + ); + } + + public function testGetRightsFromNames() + { + $this->assertSame( + "0101", + $this->rights->getRightsFromNames(["create", "update"]) + ); + } + + public function testGetRightsFromNamesWithCustomRightsWorksToo() + { + $this->rights->addRight("test"); + $this->rights->addRight("hello"); + $this->rights->addRight("destroy_humanity"); + + $this->assertSame( + "1100101", + $this->rights->getRightsFromNames(["create", "update", "hello", "destroy_humanity"]) + ); } }