Testing
Swap a fake Anydoc into application tests, assert which conversions ran, and run real conversions against faked disks and Firecrawl.
On this page
Swap a fake Anydoc into your application tests to skip conversions and assert which ones ran. To test real conversions, fake the storage disk or the HTTP client instead.
Faking Anydoc
Anydoc::fake() returns an Anydoc that reads no file, converts nothing, and sends no request to Firecrawl. It records each markdown() call instead, with or without .ocr(), and each document() call. In a #[laravel::test], swap it into the application with app.swap, before you send the requests that use it, and keep the handle it returns for your assertions:
use laravel::testing::prelude::*;
use laravel_anydoc::Anydoc;
pub async fn converts_the_uploaded_report(app: Application) {
let anydoc = app.swap(Anydoc::fake().with_markdown("# Report"));
app.post("/reports").await.assert_redirect();
anydoc.assert_converted_where(|conversion| conversion.ocr());
}Your handlers keep receiving Inject<Anydoc> and call the same methods; only the test changes. Tests that need the real conversion resolve the real Anydoc instead; see Real conversions.
Responses
The fake's markdown() returns "" unless you set its Markdown with with_markdown. It takes a &str, a String, or a closure that builds the Markdown from the Conversion:
use laravel::testing::prelude::*;
use laravel_anydoc::{Anydoc, Conversion, ConversionSource};
pub fn fake_anydoc(app: &Application) {
app.swap(Anydoc::fake().with_markdown(|conversion: &Conversion| match conversion.source() {
ConversionSource::Upload(name) => format!("# {name}"),
_ => String::from("# Document"),
}));
}document() returns the document you set with with_document, a Document or a closure that builds one from the Conversion. Without with_document, document() panics:
use laravel::testing::prelude::*;
use laravel_anydoc::Anydoc;
use laravel_anydoc::anydoc::model::Document;
pub fn fake_anydoc(app: &Application) {
app.swap(Anydoc::fake().with_document(Document::default()));
}Validation
The fake keeps the checks that need no I/O, so the same input fails in your tests as in production:
ANYDOC_MAX_FILE_SIZEstill applies to bytes and uploads, so a larger one fails withError::TooLargeand is not recorded. The fake reads no local or disk file, so it cannot check their size.disk(name)?anddefault_disk()?fail when the disk is not defined in the filesystems configuration. The fake does not build the disk, so an S3 disk needs no bucket or credentials in tests.
Assertions
Each recorded Conversion exposes:
| Method | Returns |
|---|---|
format() |
The format passed to .format(), if any. The fake detects nothing. |
ocr() |
Whether the conversion was started with .ocr(). |
output() |
Output::Markdown or Output::Document. |
source() |
ConversionSource::Path with the path as given, Upload with the upload's client name, Bytes with their length, or Disk with the disk's name and the path. |
The assertions follow the names of the framework's fakes and return the fake, so you can chain them:
use laravel::testing::prelude::*;
use laravel_anydoc::anydoc::Format;
use laravel_anydoc::{Anydoc, ConversionSource, Output};
pub async fn imports_the_csv(app: Application) {
let anydoc = app.swap(Anydoc::fake());
app.post("/imports").await.assert_redirect();
anydoc
.assert_converted_times(1)
.assert_converted_where(|conversion| conversion.format() == Some(Format::Csv))
.assert_not_converted_where(|conversion| conversion.output() == Output::Document);
assert_eq!(
anydoc.converted()[0].source(),
&ConversionSource::Disk { disk: String::from("s3"), path: String::from("imports/users.csv") },
);
}| Method | Asserts |
|---|---|
assert_converted_times(count) |
The fake recorded exactly count conversions. |
assert_converted_where(predicate) |
A recorded conversion matches the predicate. |
assert_not_converted_where(predicate) |
No recorded conversion matches the predicate. |
assert_nothing_converted() |
The fake recorded no conversions. |
converted() returns the recorded conversions in order. Calling it or an assertion on the real Anydoc panics with "Anydoc is not faked".
Real conversions
To run the real conversion in a test, resolve the real Anydoc from the application and fake what it reaches. For a storage disk, fake the disk with laravel::storage::fake before the application resolves any disk, and put the document on it:
use laravel::storage;
use laravel::testing::prelude::*;
use laravel_anydoc::Anydoc;
pub async fn converts_the_report(app: Application) -> laravel::Result<()> {
let disk = storage::fake("s3")?;
disk.put("report.csv", "name,score\nAda,10\n").await?;
let markdown = app.resolve::<Anydoc>().await.disk("s3")?.file("report.csv").markdown().await?;
assert!(markdown.contains("| Ada | 10 |"));
Ok(())
}OCR sends its request through laravel::http::client, so fake Firecrawl with client::fake():
use laravel::http::client;
use laravel::testing::prelude::*;
use laravel_anydoc::Anydoc;
pub async fn reads_the_scan(app: Application) -> laravel::Result<()> {
let http = client::fake().with(
"*/v2/parse",
client::response().json(serde_json::json!({"data": {"markdown": "# Scan"}, "success": true})),
);
let markdown = app.resolve::<Anydoc>().await.file("tests/fixtures/scan.pdf").ocr().markdown().await?;
assert_eq!(markdown, "# Scan\n");
http.assert_sent_count(1);
Ok(())
}