|
8 | 8 | use Statamic\Eloquent\Entries\Entry; |
9 | 9 | use Statamic\Eloquent\Entries\EntryModel; |
10 | 10 | use Statamic\Facades; |
| 11 | +use Statamic\Facades\Collection as CollectionFacade; |
| 12 | +use Statamic\Facades\Entry as EntryFacade; |
11 | 13 | use Tests\TestCase; |
12 | 14 |
|
13 | 15 | class EntryTest extends TestCase |
@@ -59,6 +61,70 @@ public function it_saves_to_entry_model() |
59 | 61 | $this->assertEquals(collect($model->toArray())->except(['updated_at'])->all(), collect($entry->toModel()->toArray())->except('updated_at')->all()); |
60 | 62 | } |
61 | 63 |
|
| 64 | + /** @test */ |
| 65 | + public function it_stores_computed_values() |
| 66 | + { |
| 67 | + $collection = Collection::make('blog')->title('blog')->routes([ |
| 68 | + 'en' => '/blog/{slug}', |
| 69 | + ])->save(); |
| 70 | + |
| 71 | + CollectionFacade::computed('blog', 'shares', function ($entry, $value) { |
| 72 | + return 150; |
| 73 | + }); |
| 74 | + |
| 75 | + $model = new EntryModel([ |
| 76 | + 'slug' => 'the-slug', |
| 77 | + 'data' => [ |
| 78 | + 'foo' => 'bar', |
| 79 | + ], |
| 80 | + ]); |
| 81 | + |
| 82 | + $entry = (new Entry()) |
| 83 | + ->collection('blog') |
| 84 | + ->slug('the-slug') |
| 85 | + ->data([ |
| 86 | + 'foo' => 'bar', |
| 87 | + ]); |
| 88 | + |
| 89 | + $entry->save(); |
| 90 | + |
| 91 | + $this->assertEquals(150, $entry->model()->data['shares']); |
| 92 | + } |
| 93 | + |
| 94 | + /** @test */ |
| 95 | + public function it_defers_to_the_live_computed_value_instead_of_the_stored_value() |
| 96 | + { |
| 97 | + $collection = Collection::make('blog')->title('blog')->routes([ |
| 98 | + 'en' => '/blog/{slug}', |
| 99 | + ])->save(); |
| 100 | + |
| 101 | + CollectionFacade::computed('blog', 'shares', function ($entry, $value) { |
| 102 | + return ! isset($value) ? 150 : 100; |
| 103 | + }); |
| 104 | + |
| 105 | + $model = new EntryModel([ |
| 106 | + 'slug' => 'the-slug', |
| 107 | + 'data' => [ |
| 108 | + 'foo' => 'bar', |
| 109 | + ], |
| 110 | + ]); |
| 111 | + |
| 112 | + $entry = (new Entry()) |
| 113 | + ->collection('blog') |
| 114 | + ->slug('the-slug') |
| 115 | + ->data([ |
| 116 | + 'foo' => 'bar', |
| 117 | + ]); |
| 118 | + |
| 119 | + $entry->save(); |
| 120 | + |
| 121 | + $this->assertEquals(150, $entry->model()->data['shares']); |
| 122 | + |
| 123 | + $freshEntry = EntryFacade::query()->where('slug', 'the-slug')->first(); |
| 124 | + |
| 125 | + $this->assertEquals(100, $freshEntry->shares); |
| 126 | + } |
| 127 | + |
62 | 128 | /** @test */ |
63 | 129 | public function it_propagates_entry_if_configured() |
64 | 130 | { |
|
0 commit comments