Skip to content

Commit

Permalink
Download content assertions (#30)
Browse files Browse the repository at this point in the history
* Add assertions to test the contents of file downloads

* Fix styling

* Fix styling

* Change namespace

* Fix styling

* Update AssertionsTest.php

* Fix styling

---------

Co-authored-by: nuernbergerA <[email protected]>
  • Loading branch information
nuernbergerA and nuernbergerA authored Jan 22, 2024
1 parent 895c062 commit d7a946e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.3.2","version":"3.48.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces_position":true,"class_definition":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"const":"one","method":"one","property":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/ComponentName.php":"af0dc94880495710238f36e09974f56b","src\/MissingLivewireAssertionsServiceProvider.php":"001e0571136e8126eb93c4ddb1bbb53a","src\/CustomLivewireAssertionsMixin.php":"29787c6b606a0b7aa73285903c170c0b","tests\/View\/Components\/Button.php":"ed586764f2469ec4b6a76d0f0d96f6ba","tests\/resources\/views\/livewire-test-component-c.php":"6aef47d3280960b92b464e77db3063c2","tests\/resources\/views\/livewire-test-component-b.php":"ab1e1d7326d9e0a269a84c239503d679","tests\/resources\/views\/blade-test-component.php":"4b61e7dba267eb48b3fa25a14e0d086b","tests\/resources\/views\/livewire-test-component-a.php":"42c308f0b6f9578b688100a9a925f550","tests\/Components\/LivewireTestComponentB.php":"7ef74a34799d9cc730a738f40eeb2b40","tests\/Components\/LivewireTestComponentC.php":"d516104cd3b4eb72826b8a3eef9246fd","tests\/Components\/LivewireTestComponentA.php":"2306729210f9134b92a46d05c71dc566","tests\/TestCase.php":"d1c940f002919088c735f0ced367b898","tests\/AssertionsTest.php":"44ba26be5f608df2481c1736740688d2"}}
{"php":"8.3.2","version":"3.48.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces_position":true,"class_definition":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"const":"one","method":"one","property":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/ComponentName.php":"af0dc94880495710238f36e09974f56b","src\/MissingLivewireAssertionsServiceProvider.php":"001e0571136e8126eb93c4ddb1bbb53a","src\/CustomLivewireAssertionsMixin.php":"22184762d22411af16d2498a3f5e31b8","tests\/View\/Components\/Button.php":"ed586764f2469ec4b6a76d0f0d96f6ba","tests\/resources\/views\/livewire-test-component-c.php":"6aef47d3280960b92b464e77db3063c2","tests\/resources\/views\/livewire-test-component-b.php":"ab1e1d7326d9e0a269a84c239503d679","tests\/resources\/views\/blade-test-component.php":"4b61e7dba267eb48b3fa25a14e0d086b","tests\/resources\/views\/livewire-test-component-a.php":"42c308f0b6f9578b688100a9a925f550","tests\/Components\/LivewireTestComponentB.php":"7ef74a34799d9cc730a738f40eeb2b40","tests\/Components\/LivewireTestComponentC.php":"d516104cd3b4eb72826b8a3eef9246fd","tests\/Components\/LivewireTestComponentA.php":"2306729210f9134b92a46d05c71dc566","tests\/TestCase.php":"d1c940f002919088c735f0ced367b898","tests\/AssertionsTest.php":"85b24f7379c231b96776e7cd81e9631b","tests\/Components\/FileDownloadComponent.php":"92f6b79d4253193dc1503a0d84d6ac7e"}}
34 changes: 34 additions & 0 deletions src/CustomLivewireAssertionsMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,38 @@ public function assertDoNotSeeBefore(): Closure
return $this;
};
}

/**
* Assert that the stream download contains the given string
*/
public function assertFileDownloadedContains()
{
return function ($content) {
$downloadedContent = data_get($this->effects, 'download.content');

PHPUnit::assertStringContainsString(
$content,
base64_decode($downloadedContent)
);

return $this;
};
}

/**
* Assert that the stream download NOT contains the given string
*/
public function assertFileDownloadedNotContains()
{
return function ($content) {
$downloadedContent = data_get($this->effects, 'download.content');

PHPUnit::assertStringNotContainsString(
$content,
base64_decode($downloadedContent)
);

return $this;
};
}
}
17 changes: 17 additions & 0 deletions tests/AssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Christophrumpel\MissingLivewireAssertions\MissingLivewireAssertionsServiceProvider;
use Livewire\Livewire;
use Livewire\LivewireServiceProvider;
use Tests\Components\FileDownloadComponent;
use Tests\Components\LivewireTestComponentA;
use Tests\Components\LivewireTestComponentB;
use Tests\Components\LivewireTestComponentC;
Expand Down Expand Up @@ -294,4 +295,20 @@ public function it_checks_if_it_does_not_see_a_blade_directive(): void
Livewire::test(LivewireTestComponentC::class)
->assertDoesNotContainLivewireComponent(NonExistantLivewireTestComponent::class);
}

/** @test */
public function can_check_a_downloaded_file_contains()
{
Livewire::test(FileDownloadComponent::class)
->call('streamDownload', 'download.txt')
->assertFileDownloadedContains('alpine');
}

/** @test */
public function can_check_a_downloaded_file_does_not_contain()
{
Livewire::test(FileDownloadComponent::class)
->call('streamDownload', 'download.txt')
->assertFileDownloadedNotContains('vuejs');
}
}
20 changes: 20 additions & 0 deletions tests/Components/FileDownloadComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Components;

use Livewire\Component;

class FileDownloadComponent extends Component
{
public function streamDownload($filename = null, $headers = [])
{
return response()->streamDownload(function () {
echo 'alpinejs';
}, $filename, $headers);
}

public function render()
{
return '<div></div>';
}
}

0 comments on commit d7a946e

Please sign in to comment.