|
| 1 | +<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder; |
| 2 | + |
| 3 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
| 4 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
| 5 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile; |
| 6 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher; |
| 7 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
| 8 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
| 9 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
| 10 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; |
| 11 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; |
| 12 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; |
| 13 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource; |
| 14 | +use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
| 15 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 16 | +use Illuminate\Support\Collection; |
| 17 | + |
| 18 | +class WhereTest extends IntegrationTestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase; |
| 21 | + |
| 22 | + public function testWithQuery() |
| 23 | + { |
| 24 | + $books = (new Book) |
| 25 | + ->where(function ($query) { |
| 26 | + $query->where("id", ">", "1") |
| 27 | + ->where("id", "<", "5"); |
| 28 | + }) |
| 29 | + ->get(); |
| 30 | + $uncachedBooks = (new UncachedBook) |
| 31 | + ->where(function ($query) { |
| 32 | + $query->where("id", ">", "1") |
| 33 | + ->where("id", "<", "5"); |
| 34 | + }) |
| 35 | + ->get(); |
| 36 | + |
| 37 | + $this->assertEquals($books->pluck("id"), $uncachedBooks->pluck("id")); |
| 38 | + } |
| 39 | + |
| 40 | + public function testColumnsRelationshipWhereClauseParsing() |
| 41 | + { |
| 42 | + $author = (new Author) |
| 43 | + ->orderBy('name') |
| 44 | + ->first(); |
| 45 | + $authors = (new Author) |
| 46 | + ->where('name', '=', $author->name) |
| 47 | + ->get(); |
| 48 | + $key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-name_=_' . |
| 49 | + $author->name); |
| 50 | + $tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor']; |
| 51 | + |
| 52 | + $cachedResults = $this->cache() |
| 53 | + ->tags($tags) |
| 54 | + ->get($key)['value']; |
| 55 | + $liveResults = (new UncachedAuthor) |
| 56 | + ->where('name', '=', $author->name) |
| 57 | + ->get(); |
| 58 | + |
| 59 | + $this->assertEmpty($authors->diffKeys($cachedResults)); |
| 60 | + $this->assertEmpty($liveResults->diffKeys($cachedResults)); |
| 61 | + } |
| 62 | + |
| 63 | + private function processWhereClauseTestWithOperator(string $operator) |
| 64 | + { |
| 65 | + $author = (new Author)->first(); |
| 66 | + $authors = (new Author) |
| 67 | + ->where('name', $operator, $author->name) |
| 68 | + ->get(); |
| 69 | + $keyParts = [ |
| 70 | + 'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-name', |
| 71 | + '_', |
| 72 | + str_replace(' ', '_', strtolower($operator)), |
| 73 | + '_', |
| 74 | + $author->name, |
| 75 | + ]; |
| 76 | + $key = sha1(implode('', $keyParts)); |
| 77 | + $tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor']; |
| 78 | + |
| 79 | + $cachedResults = $this->cache() |
| 80 | + ->tags($tags) |
| 81 | + ->get($key)['value']; |
| 82 | + $liveResults = (new UncachedAuthor) |
| 83 | + ->where('name', $operator, $author->name) |
| 84 | + ->get(); |
| 85 | + |
| 86 | + $this->assertEmpty($authors->diffKeys($cachedResults)); |
| 87 | + $this->assertEmpty($liveResults->diffKeys($cachedResults)); |
| 88 | + } |
| 89 | + |
| 90 | + public function testWhereClauseParsingOfOperators() |
| 91 | + { |
| 92 | + $this->processWhereClauseTestWithOperator('='); |
| 93 | + $this->processWhereClauseTestWithOperator('!='); |
| 94 | + $this->processWhereClauseTestWithOperator('<>'); |
| 95 | + $this->processWhereClauseTestWithOperator('>'); |
| 96 | + $this->processWhereClauseTestWithOperator('<'); |
| 97 | + $this->processWhereClauseTestWithOperator('LIKE'); |
| 98 | + $this->processWhereClauseTestWithOperator('NOT LIKE'); |
| 99 | + } |
| 100 | +} |
0 commit comments