diff --git a/CHANGELOG.md b/CHANGELOG.md index f383c03ba..80853e93c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ CHANGELOG for 0.9.0 RC ====================== +1.0.6 (28.09.2017) +----- +- Fixed a bug when error message was converted to empty array +- Fixed a bug when multidepth requests contained invalid prefix (in case of depth more than 3) + +1.0.5 +----- +- ability to locate view cache files by view name and namespace + +1.0.4 +----- +- ability to add conditions to skip validation rules 1.0.2 (04.05.2017) ----- diff --git a/source/Spiral/Http/Request/InputInterface.php b/source/Spiral/Http/Request/InputInterface.php index 4c77bcbc0..df7b8f7d9 100644 --- a/source/Spiral/Http/Request/InputInterface.php +++ b/source/Spiral/Http/Request/InputInterface.php @@ -36,7 +36,9 @@ public function getValue(string $source, string $name = null); * * @param string $prefix * + * @param bool $add When set to false current prefix path will be overwritten. + * * @return InputInterface */ - public function withPrefix(string $prefix): InputInterface; + public function withPrefix(string $prefix, bool $add = true): InputInterface; } \ No newline at end of file diff --git a/source/Spiral/Http/Request/InputManager.php b/source/Spiral/Http/Request/InputManager.php index 621fa95bf..d6a95fb4b 100644 --- a/source/Spiral/Http/Request/InputManager.php +++ b/source/Spiral/Http/Request/InputManager.php @@ -387,10 +387,16 @@ public function getValue(string $source, string $name = null) * * @return self */ - public function withPrefix(string $prefix): InputInterface + public function withPrefix(string $prefix, bool $add = true): InputInterface { $input = clone $this; - $input->prefix = $prefix; + + if ($add) { + $input->prefix .= '.' . $prefix; + $input->prefix = trim($input->prefix, '.'); + } else { + $input->prefix = $prefix; + } return $input; } diff --git a/source/Spiral/Http/Request/InputMapper.php b/source/Spiral/Http/Request/InputMapper.php index 28eccf336..89f1eb167 100644 --- a/source/Spiral/Http/Request/InputMapper.php +++ b/source/Spiral/Http/Request/InputMapper.php @@ -113,7 +113,7 @@ public function originateErrors(array $errors): array $this->mountMessage($mapped, $this->schema[$field]['origin'], $message); } else { //Custom error - $mapped[$field] = $mapped; + $mapped[$field] = $message; } } @@ -264,4 +264,4 @@ private function createOrigins( return $result; } -} \ No newline at end of file +} diff --git a/source/Spiral/Views/AbstractViewCache.php b/source/Spiral/Views/AbstractViewCache.php new file mode 100644 index 000000000..324347fe2 --- /dev/null +++ b/source/Spiral/Views/AbstractViewCache.php @@ -0,0 +1,32 @@ +getFilename() . '.' . $this->environment->getID()); - return $this->environment->cacheDirectory() . $hash . '.php'; + return $this->environment->cacheDirectory() . + $this->getPrefix($context->getName(), $context->getNamespace()) . '-' . $hash . '.php'; } /** diff --git a/source/Spiral/Views/Engines/Twig/TwigCache.php b/source/Spiral/Views/Engines/Twig/TwigCache.php index 87139c7d7..520acaca6 100644 --- a/source/Spiral/Views/Engines/Twig/TwigCache.php +++ b/source/Spiral/Views/Engines/Twig/TwigCache.php @@ -9,12 +9,13 @@ namespace Spiral\Views\Engines\Twig; use Spiral\Files\FilesInterface; +use Spiral\Views\AbstractViewCache; use Spiral\Views\EnvironmentInterface; /** * Spiral specific twig cache. OpCache reset not included yet. */ -class TwigCache implements \Twig_CacheInterface +class TwigCache extends AbstractViewCache implements \Twig_CacheInterface { /** * @var FilesInterface @@ -41,9 +42,10 @@ public function __construct(FilesInterface $files, EnvironmentInterface $environ */ public function generateKey($name, $className) { + $prefix = $this->getPrefix($name); $hash = hash('md5', $className . '.' . $this->environment->getID()); - return "{$this->environment->cacheDirectory()}/{$hash}.php"; + return "{$this->environment->cacheDirectory()}/{$prefix}-{$hash}.php"; } /** diff --git a/source/Spiral/Views/ViewCacheLocator.php b/source/Spiral/Views/ViewCacheLocator.php new file mode 100644 index 000000000..5d2785cef --- /dev/null +++ b/source/Spiral/Views/ViewCacheLocator.php @@ -0,0 +1,52 @@ +config = $config; + $this->fileManager = $fileManager; + } + + /** + * Returns all cache files for view. + * + * @param string $view + * @param string $namespace + * @return array + */ + public function getFiles($view, $namespace = 'default') + { + $prefix = $this->getPrefix($view, $namespace); + + return $this->fileManager->getFiles($this->config->cacheDirectory(), $prefix.'-*'); + } +} \ No newline at end of file diff --git a/tests/Http/Fixtures/FirstDepthRequest.php b/tests/Http/Fixtures/FirstDepthRequest.php new file mode 100644 index 000000000..7ae04357a --- /dev/null +++ b/tests/Http/Fixtures/FirstDepthRequest.php @@ -0,0 +1,12 @@ + SecondDepthRequest::class + ]; +} \ No newline at end of file diff --git a/tests/Http/Fixtures/SecondDepthRequest.php b/tests/Http/Fixtures/SecondDepthRequest.php new file mode 100644 index 000000000..d3a1f2223 --- /dev/null +++ b/tests/Http/Fixtures/SecondDepthRequest.php @@ -0,0 +1,12 @@ + ThirdDepthRequest::class + ]; +} \ No newline at end of file diff --git a/tests/Http/Fixtures/ThirdDepthRequest.php b/tests/Http/Fixtures/ThirdDepthRequest.php new file mode 100644 index 000000000..6a3226a5f --- /dev/null +++ b/tests/Http/Fixtures/ThirdDepthRequest.php @@ -0,0 +1,16 @@ + 'data:third' + ]; + + const VALIDATES = [ + 'third' => ['notEmpty'] + ]; +} \ No newline at end of file diff --git a/tests/Http/RequestFilters/MultipleDepthRequestTest.php b/tests/Http/RequestFilters/MultipleDepthRequestTest.php new file mode 100644 index 000000000..3278add90 --- /dev/null +++ b/tests/Http/RequestFilters/MultipleDepthRequestTest.php @@ -0,0 +1,52 @@ +withParsedBody([]); + $this->container->bind(ServerRequestInterface::class, $serverRequest); + + /** @var DemoRequest $request */ + $request = $this->container->get(FirstDepthRequest::class); + + $this->assertFalse($request->isValid()); + $this->assertArrayHasKey('first', $request->getErrors()); + $this->assertArrayHasKey('second', $request->getErrors()['first']); + $this->assertArrayHasKey('third', $request->getErrors()['first']['second']); + } + + public function testDataPassed() + { + $serverRequest = new ServerRequest(); + $serverRequest = $serverRequest->withParsedBody([ + 'first' => [ + 'second' => [ + 'third' => '3rd value', + ], + ] + ]); + $this->container->bind(ServerRequestInterface::class, $serverRequest); + + /** @var DemoRequest $request */ + $request = $this->container->get(FirstDepthRequest::class); + + $this->assertTrue($request->isValid()); + $this->assertArrayNotHasKey('first', $request->getErrors()); + + $this->assertArrayHasKey('first', $request->getValidator()->getData()); + $this->assertArrayHasKey('second', $request->getValidator()->getData()['first']); + $this->assertArrayHasKey('third', $request->getValidator()->getData()['first']['second']); + $this->assertSame('3rd value', $request->getValidator()->getData()['first']['second']['third']); + } +} diff --git a/tests/Http/RequestFilters/ScopingTest.php b/tests/Http/RequestFilters/ScopingTest.php index eaa127179..240e7095a 100644 --- a/tests/Http/RequestFilters/ScopingTest.php +++ b/tests/Http/RequestFilters/ScopingTest.php @@ -35,7 +35,7 @@ public function getValue(string $source, string $name = null) } } - public function withPrefix(string $prefix): InputInterface + public function withPrefix(string $prefix, bool $add = true): InputInterface { return $this; } diff --git a/tests/Translator/LocatorTest.php b/tests/Translator/LocatorTest.php index 8f2931170..96b4fb30b 100644 --- a/tests/Translator/LocatorTest.php +++ b/tests/Translator/LocatorTest.php @@ -49,7 +49,12 @@ public function testGetLocales() $config->shouldReceive('localesDirectory')->andReturn(__DIR__ . '/fixtures/locales/'); - $this->assertSame(['en', 'ru'], $source->getLocales()); + $compared = $source->getLocales(); + $shouldBe = ['en', 'ru']; + sort($shouldBe); + sort($compared); + + $this->assertSame($shouldBe, $compared); } public function testLoadLocale() diff --git a/tests/Views/StemplerTest.php b/tests/Views/StemplerTest.php index 50c0692a7..9fac91b77 100644 --- a/tests/Views/StemplerTest.php +++ b/tests/Views/StemplerTest.php @@ -10,10 +10,20 @@ use Spiral\Tests\BaseTest; use Spiral\Views\Engines\Stempler\StemplerCache; use Spiral\Views\Engines\Stempler\StemplerView; +use Spiral\Views\ViewCacheLocator; use Spiral\Views\ViewLoader; class StemplerTest extends BaseTest { + protected function deleteCacheFiles() + { + foreach ($this->files->getFiles($this->views->getEnvironment()->cacheDirectory()) as $filename) { + //If exception is thrown here this will mean that application wasn't correctly + //destructed and there is open resources kept + $this->files->delete($filename); + } + } + public function testRenderSimple() { $this->assertSame('Hello, World!', trim($this->views->render('home', [ @@ -53,7 +63,10 @@ public function testCompileWithEnvironment() public function testRenderFromOtherLoader() { - $this->assertSame('Hello, World!', $this->views->render('native', [ + $this->deleteCacheFiles(); + clearstatcache(); + + $this->assertContains('Hello, World!', $this->views->render('home', [ 'name' => 'World' ])); @@ -64,7 +77,7 @@ public function testRenderFromOtherLoader() ) ); - $this->assertSame('home alt', $views->render('home')); + $this->assertContains('home alt', $views->render('home')); } public function testRenderFromCache() @@ -92,4 +105,27 @@ public function testView() { $this->assertInstanceOf(StemplerView::class, $this->views->get('home')); } + + public function testCacheLocator() + { + $this->deleteCacheFiles(); + clearstatcache(); + + $this->views->withEnvironment( + $this->views->getEnvironment()->withDependency('value', function () { + return 'test-one'; + }) + )->compile('home'); + + $this->views->withEnvironment( + $this->views->getEnvironment()->withDependency('value', function () { + return 'test-two'; + }) + )->compile('home'); + + /** @var ViewCacheLocator $data */ + $viewCacheLocator = $this->container->get(ViewCacheLocator::class); + + $this->assertSame(2, count($viewCacheLocator->getFiles('home'))); + } } \ No newline at end of file