Skip to content

Commit

Permalink
Adjust expectations for dispatchCommit method
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Jan 5, 2024
1 parent 654952d commit 5ddefca
Showing 1 changed file with 69 additions and 67 deletions.
136 changes: 69 additions & 67 deletions tests/Git/GitEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class GitEventTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

public $user;

public function setUp(): void
{
parent::setUp();
Expand All @@ -32,13 +34,13 @@ public function setUp(): void
Config::set('statamic.git', $defaultConfig);
Config::set('statamic.git.enabled', true);

$this->actingAs(
User::make()
->id('chewbacca')
->email('[email protected]')
->data(['name' => 'Chewbacca'])
->makeSuper()
);
$this->user = User::make()
->id('chewbacca')
->email('[email protected]')
->data(['name' => 'Chewbacca'])
->makeSuper();

$this->actingAs($this->user);

Config::set('filesystems.disks.test', [
'driver' => 'local',
Expand All @@ -53,8 +55,8 @@ public function setUp(): void
/** @test */
public function it_doesnt_commit_when_git_is_disabled()
{
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->never();

Config::set('statamic.git.enabled', false);

Expand All @@ -67,8 +69,8 @@ public function it_doesnt_commit_when_git_is_disabled()
/** @test */
public function it_doesnt_commit_when_automatic_is_disabled()
{
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->never();

Config::set('statamic.git.automatic', false);

Expand All @@ -81,8 +83,8 @@ public function it_doesnt_commit_when_automatic_is_disabled()
/** @test */
public function it_doesnt_commit_ignored_events()
{
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once();

Config::set('statamic.git.ignored_events', [
\Statamic\Events\CollectionSaved::class,
Expand All @@ -97,8 +99,8 @@ public function it_doesnt_commit_ignored_events()
/** @test */
public function it_doesnt_commit_when_event_subscriber_is_disabled()
{
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once();

$collection = Facades\Collection::make('pages');

Expand All @@ -112,7 +114,7 @@ public function it_doesnt_commit_when_event_subscriber_is_disabled()
/** @test */
public function it_commits_when_custom_addon_events_are_registered()
{
Git::shouldReceive('dispatchCommit')->with('Pun saved')->once();
Git::shouldReceive('dispatchCommit')->with('Pun saved', $this->user)->once();
Git::makePartial();

Git::listen(PunSaved::class);
Expand All @@ -127,8 +129,8 @@ public function it_commits_when_custom_addon_events_are_registered()
/** @test */
public function it_commits_when_blueprint_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Blueprint saved')->once();
Git::shouldReceive('dispatchCommit')->with('Blueprint deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Blueprint saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Blueprint deleted', $this->user)->once();

$blueprint = Facades\Blueprint::make('post');

Expand All @@ -139,8 +141,8 @@ public function it_commits_when_blueprint_is_saved_and_deleted()
/** @test */
public function it_commits_when_fieldset_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Fieldset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Fieldset deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Fieldset saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Fieldset deleted', $this->user)->once();

$fieldset = Facades\Fieldset::make('address');

Expand All @@ -151,8 +153,8 @@ public function it_commits_when_fieldset_is_saved_and_deleted()
/** @test */
public function it_commits_when_collection_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Collection saved')->once();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once();

$collection = Facades\Collection::make('pages');

Expand All @@ -167,8 +169,8 @@ public function it_commits_when_entry_is_saved_and_deleted()
Events\CollectionSaved::class,
]);

Git::shouldReceive('dispatchCommit')->with('Entry saved')->once();
Git::shouldReceive('dispatchCommit')->with('Entry deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Entry deleted', $this->user)->once();

$collection = Facades\Collection::make('pages');
$collection->save();
Expand All @@ -190,8 +192,8 @@ public function it_commits_when_tracked_revisions_are_saved_and_deleted()

Config::set('statamic.revisions.path', base_path('content/revisions'));

Git::shouldReceive('dispatchCommit')->with('Revision saved')->once();
Git::shouldReceive('dispatchCommit')->with('Revision deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Revision saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Revision deleted', $this->user)->once();

$collection = Facades\Collection::make('pages');
$collection->save();
Expand All @@ -207,8 +209,8 @@ public function it_commits_when_tracked_revisions_are_saved_and_deleted()
/** @test */
public function it_commits_when_navigation_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Navigation saved')->once();
Git::shouldReceive('dispatchCommit')->with('Navigation deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Navigation saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Navigation deleted', $this->user)->once();

$nav = Facades\Nav::make()->handle('footer');

Expand All @@ -219,8 +221,8 @@ public function it_commits_when_navigation_is_saved_and_deleted()
/** @test */
public function it_commits_when_a_navigation_tree_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Navigation tree saved')->once();
Git::shouldReceive('dispatchCommit')->with('Navigation tree deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Navigation tree saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Navigation tree deleted', $this->user)->once();

$nav = Facades\Nav::make()->handle('footer');
$tree = $nav->makeTree('en');
Expand All @@ -236,8 +238,8 @@ public function it_commits_when_a_collection_tree_is_saved_and_deleted()
Events\CollectionSaved::class,
]);

Git::shouldReceive('dispatchCommit')->with('Collection tree saved')->once();
Git::shouldReceive('dispatchCommit')->with('Collection tree deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Collection tree saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Collection tree deleted', $this->user)->once();

$collection = Facades\Collection::make('pages')->structureContents(['max_depth' => 10])->save();
$tree = $collection->structure()->makeTree('en');
Expand All @@ -249,8 +251,8 @@ public function it_commits_when_a_collection_tree_is_saved_and_deleted()
/** @test */
public function it_commits_when_taxonomy_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Taxonomy saved')->once();
Git::shouldReceive('dispatchCommit')->with('Taxonomy deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Taxonomy saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Taxonomy deleted', $this->user)->once();

$taxonomy = Facades\Taxonomy::make('topics');

Expand All @@ -265,8 +267,8 @@ public function it_commits_when_term_is_saved_and_deleted()
Events\TaxonomySaved::class,
]);

Git::shouldReceive('dispatchCommit')->with('Term saved')->once();
Git::shouldReceive('dispatchCommit')->with('Term deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Term saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Term deleted', $this->user)->once();

$taxonomy = Facades\Taxonomy::make('topics');
$taxonomy->save();
Expand All @@ -283,8 +285,8 @@ public function it_commits_when_term_is_saved_and_deleted()
/** @test */
public function it_commits_when_global_set_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Global Set saved')->once();
Git::shouldReceive('dispatchCommit')->with('Global Set deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Global Set saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Global Set deleted', $this->user)->once();

$set = Facades\GlobalSet::make('main');
$set->addLocalization($set->makeLocalization(Facades\Site::default()->handle()));
Expand All @@ -296,8 +298,8 @@ public function it_commits_when_global_set_is_saved_and_deleted()
/** @test */
public function it_commits_when_form_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Form saved')->once();
Git::shouldReceive('dispatchCommit')->with('Form deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Form saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Form deleted', $this->user)->once();

$form = Facades\Form::make('main');

Expand All @@ -312,8 +314,8 @@ public function it_commits_when_form_submission_is_saved_and_deleted()
Events\FormSaved::class,
]);

Git::shouldReceive('dispatchCommit')->with('Submission saved')->once();
Git::shouldReceive('dispatchCommit')->with('Submission deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Submission saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Submission deleted', $this->user)->once();

$form = Facades\Form::make('contact');

Expand All @@ -328,8 +330,8 @@ public function it_commits_when_form_submission_is_saved_and_deleted()
/** @test */
public function it_commits_when_user_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('User saved')->once();
Git::shouldReceive('dispatchCommit')->with('User deleted')->once();
Git::shouldReceive('dispatchCommit')->with('User saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('User deleted', $this->user)->once();

$user = Facades\User::make();

Expand All @@ -340,8 +342,8 @@ public function it_commits_when_user_is_saved_and_deleted()
/** @test */
public function it_commits_when_user_role_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Role saved')->once();
Git::shouldReceive('dispatchCommit')->with('Role deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Role saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Role deleted', $this->user)->once();

$role = Facades\Role::make()->handle('author');

Expand All @@ -352,8 +354,8 @@ public function it_commits_when_user_role_is_saved_and_deleted()
/** @test */
public function it_commits_when_user_group_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('User group saved')->once();
Git::shouldReceive('dispatchCommit')->with('User group deleted')->once();
Git::shouldReceive('dispatchCommit')->with('User group saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('User group deleted', $this->user)->once();

$group = Facades\UserGroup::make()->handle('admin');

Expand All @@ -364,7 +366,7 @@ public function it_commits_when_user_group_is_saved_and_deleted()
/** @test */
public function it_commits_when_default_user_preferences_are_saved()
{
Git::shouldReceive('dispatchCommit')->with('Default preferences saved')->once();
Git::shouldReceive('dispatchCommit')->with('Default preferences saved', $this->user)->once();

Facades\Preference::default()->set('foo', 'bar')->save();

Expand All @@ -374,8 +376,8 @@ public function it_commits_when_default_user_preferences_are_saved()
/** @test */
public function it_commits_when_asset_container_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Asset container saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset container deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Asset container saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Asset container deleted', $this->user)->once();

$container = Facades\AssetContainer::make()->handle('assets');

Expand All @@ -386,8 +388,8 @@ public function it_commits_when_asset_container_is_saved_and_deleted()
/** @test */
public function it_commits_when_asset_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Asset deleted', $this->user)->once();

$asset = $this->makeAsset()->data(['bar' => 'baz']);

Expand All @@ -398,7 +400,7 @@ public function it_commits_when_asset_is_saved_and_deleted()
/** @test */
public function it_commits_when_asset_is_uploaded()
{
Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();

$this->makeAsset()->upload(
UploadedFile::fake()->create('asset.txt')
Expand All @@ -408,7 +410,7 @@ public function it_commits_when_asset_is_uploaded()
/** @test */
public function it_commits_when_asset_is_reuploaded()
{
Git::shouldReceive('dispatchCommit')->with('Asset reuploaded')->once();
Git::shouldReceive('dispatchCommit')->with('Asset reuploaded', $this->user)->once();

$file = Mockery::mock(ReplacementFile::class);
$file->shouldReceive('extension')->andReturn('txt');
Expand All @@ -420,7 +422,7 @@ public function it_commits_when_asset_is_reuploaded()
/** @test */
public function it_commits_when_asset_is_moved()
{
Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();

$asset = tap($this->makeAsset()->data(['bar' => 'baz']))->saveQuietly();

Expand All @@ -430,7 +432,7 @@ public function it_commits_when_asset_is_moved()
/** @test */
public function it_commits_when_asset_is_renamed()
{
Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();

$asset = tap($this->makeAsset()->data(['bar' => 'baz']))->saveQuietly();

Expand All @@ -442,7 +444,7 @@ public function it_commits_only_once_when_asset_is_replaced()
{
$originalAsset = tap($this->makeAsset())->saveQuietly();

Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();

$newAsset = $this->makeAsset()->upload(
UploadedFile::fake()->create('asset.txt')
Expand All @@ -456,13 +458,13 @@ public function it_commits_when_replaced_asset_is_deleted()
{
$originalAsset = tap($this->makeAsset())->saveQuietly();

Git::shouldReceive('dispatchCommit')->with('Asset saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once();

$newAsset = $this->makeAsset()->upload(
UploadedFile::fake()->create('asset.txt')
);

Git::shouldReceive('dispatchCommit')->with('Asset deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Asset deleted', $this->user)->once();

// Replace with `$deleteOriginal = true`
$newAsset->replace($originalAsset, true);
Expand All @@ -471,8 +473,8 @@ public function it_commits_when_replaced_asset_is_deleted()
/** @test */
public function it_commits_when_asset_folder_is_saved_and_deleted()
{
Git::shouldReceive('dispatchCommit')->with('Asset folder saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset folder deleted')->once();
Git::shouldReceive('dispatchCommit')->with('Asset folder saved', $this->user)->once();
Git::shouldReceive('dispatchCommit')->with('Asset folder deleted', $this->user)->once();

$folder = $this
->makeAsset()
Expand Down Expand Up @@ -531,8 +533,8 @@ public function it_batches_term_references_changes_into_one_commit()
->saveQuietly();
}

Git::shouldReceive('dispatchCommit')->with('Term references updated')->once(); // Ensure references updated event gets fired
Git::shouldReceive('dispatchCommit')->with('Entry saved')->never(); // Ensure individual entry saved events do not get fired
Git::shouldReceive('dispatchCommit')->with('Term references updated', $this->user)->once(); // Ensure references updated event gets fired
Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->never(); // Ensure individual entry saved events do not get fired

$term->slug('leia-updated')->save();
}
Expand Down Expand Up @@ -579,15 +581,15 @@ public function it_batches_asset_references_changes_into_one_commit()
->saveQuietly();
}

Git::shouldReceive('dispatchCommit')->with('Asset references updated')->once(); // Ensure references updated event gets fired
Git::shouldReceive('dispatchCommit')->with('Entry saved')->never(); // Ensure individual entry saved events do not get fired
Git::shouldReceive('dispatchCommit')->with('Asset references updated', $this->user)->once(); // Ensure references updated event gets fired
Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->never(); // Ensure individual entry saved events do not get fired

$newAsset->replace($originalAsset);
}

private function makeAsset($path = 'asset.txt')
{
Git::shouldReceive('dispatchCommit')->with('Asset container saved')->once();
Git::shouldReceive('dispatchCommit')->with('Asset container saved', $this->user)->once();

$container = Facades\AssetContainer::make()->handle('assets')->disk('test');
$container->save();
Expand Down

0 comments on commit 5ddefca

Please sign in to comment.