From afd75879d7d4eb70423f2ab374f28309ad57b217 Mon Sep 17 00:00:00 2001 From: George Kelly Date: Fri, 4 Oct 2019 08:26:56 +0100 Subject: [PATCH] Allow Storage::put to accept a Psr StreamInterface Remove brackets arround URL php artisan serve (#30168) To allow opening the development URL in Visual Studio Code with ctrl + click the bracket after the URL needs to be removed. This patch wil remove the brackets around the url. add test for sorted middlewares (#30166) [6.x] Apply limit to database rather than collection (#30148) * Apply limit to database rather than collection For HasInDatabase.php * Fix tests * Add to SoftDeleted trait as well * Update HasInDatabase.php * Update SoftDeletedInDatabase.php [6.x] Allow to use scoped macro in nested queries (#30127) * Allow to use scoped macro in nested queries. * Use newQueryWithoutRelationships --- composer.json | 1 + src/Illuminate/Database/Eloquent/Builder.php | 2 +- src/Illuminate/Filesystem/FilesystemAdapter.php | 5 +++++ .../Foundation/Console/ServeCommand.php | 2 +- .../Testing/Constraints/HasInDatabase.php | 10 ++++++---- .../Constraints/SoftDeletedInDatabase.php | 10 ++++++---- tests/Database/DatabaseEloquentBuilderTest.php | 13 ++++++++++++- tests/Filesystem/FilesystemAdapterTest.php | 17 +++++++++++++++++ .../FoundationInteractsWithDatabaseTest.php | 7 +++++-- tests/Routing/RoutingSortedMiddlewareTest.php | 16 +++++++++++++++- 10 files changed, 69 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 0681ffc8498e..7c87d9748334 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "opis/closure": "^3.1", "psr/container": "^1.0", "psr/simple-cache": "^1.0", + "psr/http-message": "^1.0", "ramsey/uuid": "^3.7", "swiftmailer/swiftmailer": "^6.0", "symfony/console": "^4.3.4", diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 9b88196972f9..a28db3fc99c9 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -223,7 +223,7 @@ public function whereKeyNot($id) public function where($column, $operator = null, $value = null, $boolean = 'and') { if ($column instanceof Closure) { - $column($query = $this->model->newModelQuery()); + $column($query = $this->model->newQueryWithoutRelationships()); $this->query->addNestedWhereQuery($query->getQuery(), $boolean); } else { diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index b907440a4b8d..d3f2a315214d 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -20,6 +20,7 @@ use League\Flysystem\FileNotFoundException; use League\Flysystem\FilesystemInterface; use PHPUnit\Framework\Assert as PHPUnit; +use Psr\Http\Message\StreamInterface; use RuntimeException; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -203,6 +204,10 @@ public function put($path, $contents, $options = []) return $this->putFile($path, $contents, $options); } + if ($contents instanceof StreamInterface) { + return $this->driver->putStream($path, $contents->detach(), $options); + } + return is_resource($contents) ? $this->driver->putStream($path, $contents, $options) : $this->driver->put($path, $contents, $options); diff --git a/src/Illuminate/Foundation/Console/ServeCommand.php b/src/Illuminate/Foundation/Console/ServeCommand.php index 8b6511c04b30..b931ea227e71 100644 --- a/src/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Illuminate/Foundation/Console/ServeCommand.php @@ -42,7 +42,7 @@ public function handle() { chdir(public_path()); - $this->line("Laravel development server started: host()}:{$this->port()}>"); + $this->line("Laravel development server started: http://{$this->host()}:{$this->port()}"); passthru($this->serverCommand(), $status); diff --git a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php index b88f34222872..35bf40be721b 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php @@ -75,16 +75,18 @@ public function failureDescription($table): string */ protected function getAdditionalInfo($table) { - $results = $this->database->table($table)->get(); + $query = $this->database->table($table); + + $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; } - $description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT); + $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT); - if ($results->count() > $this->show) { - $description .= sprintf(' and %s others', $results->count() - $this->show); + if ($query->count() > $this->show) { + $description .= sprintf(' and %s others', $query->count() - $this->show); } return $description; diff --git a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php index 2e0d738b279c..f99b9311fb9d 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php @@ -88,16 +88,18 @@ public function failureDescription($table): string */ protected function getAdditionalInfo($table) { - $results = $this->database->table($table)->get(); + $query = $this->database->table($table); + + $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; } - $description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT); + $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT); - if ($results->count() > $this->show) { - $description .= sprintf(' and %s others', $results->count() - $this->show); + if ($query->count() > $this->show) { + $description .= sprintf(' and %s others', $query->count() - $this->show); } return $description; diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 0783124c5f22..802424a66d44 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -643,7 +643,7 @@ public function testNestedWhere() $nestedRawQuery = $this->getMockQueryBuilder(); $nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery); $model = $this->getMockModel()->makePartial(); - $model->shouldReceive('newModelQuery')->once()->andReturn($nestedQuery); + $model->shouldReceive('newQueryWithoutRelationships')->once()->andReturn($nestedQuery); $builder = $this->getBuilder(); $builder->getQuery()->shouldReceive('from'); $builder->setModel($model); @@ -667,6 +667,17 @@ public function testRealNestedWhereWithScopes() $this->assertEquals(['bar', 9000], $query->getBindings()); } + public function testRealNestedWhereWithScopesMacro() + { + $model = new EloquentBuilderTestNestedStub; + $this->mockConnectionForModel($model, 'SQLite'); + $query = $model->newQuery()->where('foo', '=', 'bar')->where(function ($query) { + $query->where('baz', '>', 9000)->onlyTrashed(); + })->withTrashed(); + $this->assertSame('select * from "table" where "foo" = ? and ("baz" > ? and "table"."deleted_at" is not null)', $query->toSql()); + $this->assertEquals(['bar', 9000], $query->getBindings()); + } + public function testRealNestedWhereWithMultipleScopesAndOneDeadScope() { $model = new EloquentBuilderTestNestedStub; diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 12c5078fe0cf..5debda0c2b59 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -2,13 +2,16 @@ namespace Illuminate\Tests\Filesystem; +use GuzzleHttp\Psr7\Stream; use Illuminate\Contracts\Filesystem\FileExistsException; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\FilesystemAdapter; use InvalidArgumentException; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; +use Mockery as m; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\StreamInterface; use Symfony\Component\HttpFoundation\StreamedResponse; class FilesystemAdapterTest extends TestCase @@ -26,6 +29,7 @@ protected function tearDown(): void { $filesystem = new Filesystem(new Local(dirname($this->tempDir))); $filesystem->deleteDir(basename($this->tempDir)); + m::close(); } public function testResponse() @@ -218,4 +222,17 @@ public function testStreamInvalidResourceThrows() $filesystemAdapter = new FilesystemAdapter($this->filesystem); $filesystemAdapter->writeStream('file.txt', 'foo bar'); } + + public function testPutWithStreamInterface() + { + file_put_contents($this->tempDir.'/foo.txt', 'some-data'); + $spy = m::spy($this->filesystem); + + $filesystemAdapter = new FilesystemAdapter($spy); + $stream = new Stream(fopen($this->tempDir.'/foo.txt', 'r')); + $filesystemAdapter->put('bar.txt', $stream); + + $spy->shouldHaveReceived('putStream'); + $this->assertEquals('some-data', $filesystemAdapter->get('bar.txt')); + } } diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 0947bdc665d0..5a3d7c7c0191 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -71,10 +71,11 @@ public function testSeeInDatabaseFindsManyNotMatchingResults() $this->expectExceptionMessage('Found: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.'); $builder = $this->mockCountBuilder(0); + $builder->shouldReceive('count')->andReturn(0, 5); $builder->shouldReceive('take')->andReturnSelf(); $builder->shouldReceive('get')->andReturn( - collect(array_fill(0, 5, 'data')) + collect(array_fill(0, 3, 'data')) ); $this->assertDatabaseHas($this->table, $this->data); @@ -150,11 +151,13 @@ protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at { $builder = m::mock(Builder::class); + $builder->shouldReceive('limit')->andReturnSelf(); + $builder->shouldReceive('where')->with($this->data)->andReturnSelf(); $builder->shouldReceive('whereNotNull')->with($deletedAtColumn)->andReturnSelf(); - $builder->shouldReceive('count')->andReturn($countResult); + $builder->shouldReceive('count')->andReturn($countResult)->byDefault(); $this->connection->shouldReceive('table') ->with($this->table) diff --git a/tests/Routing/RoutingSortedMiddlewareTest.php b/tests/Routing/RoutingSortedMiddlewareTest.php index 1e566c172856..e5877a844ce7 100644 --- a/tests/Routing/RoutingSortedMiddlewareTest.php +++ b/tests/Routing/RoutingSortedMiddlewareTest.php @@ -44,10 +44,24 @@ public function testMiddlewareCanBeSortedByPriority() $this->assertEquals([], (new SortedMiddleware(['First'], []))->all()); $this->assertEquals(['First'], (new SortedMiddleware(['First'], ['First']))->all()); $this->assertEquals(['First', 'Second'], (new SortedMiddleware(['First', 'Second'], ['Second', 'First']))->all()); + } + public function testItDoesNotMoveNonStringValues() + { $closure = function () { - // + return 'foo'; }; + + $closure2 = function () { + return 'bar'; + }; + + $this->assertEquals([2, 1], (new SortedMiddleware([1, 2], [2, 1]))->all()); $this->assertEquals(['Second', $closure], (new SortedMiddleware(['First', 'Second'], ['Second', $closure]))->all()); + $this->assertEquals(['a', 'b', $closure], (new SortedMiddleware(['a', 'b'], ['b', $closure, 'a']))->all()); + $this->assertEquals([$closure2, 'a', 'b', $closure, 'foo'], (new SortedMiddleware(['a', 'b'], [$closure2, 'b', $closure, 'a', 'foo']))->all()); + $this->assertEquals([$closure, 'a', 'b', $closure2, 'foo'], (new SortedMiddleware(['a', 'b'], [$closure, 'b', $closure2, 'foo', 'a']))->all()); + $this->assertEquals(['a', $closure, 'b', $closure2, 'foo'], (new SortedMiddleware(['a', 'b'], ['a', $closure, 'b', $closure2, 'foo']))->all()); + $this->assertEquals([$closure, $closure2, 'foo', 'a'], (new SortedMiddleware(['a', 'b'], [$closure, $closure2, 'foo', 'a']))->all()); } }