mod console;
#[cfg(feature = "database")]
mod database;
use laravel_core::testing::fake_stack;
mod json;
pub mod prelude;
mod queue;
mod request;
mod response;
mod workflow;
pub use self::console::{MakesConsoleRequests, TestCommand, TestCommandResult};
#[cfg(feature = "database")]
pub use self::database::{DatabaseAssertion, InteractsWithDatabase};
pub(crate) use self::fake_stack::{FakeGuard, FakeStack};
pub use self::json::AssertableJson;
pub use self::queue::{RunsJobs, TestCallback, TestJob, TestJobResult};
pub use self::request::{MakesHttpRequests, TestRequest};
pub use self::response::TestResponse;
pub use self::workflow::{RunsWorkflows, TestRun, TestWorkflow};
use std::future::Future;
use std::sync::Arc;
use crate::Error;
use crate::event::{Event, Listener};
use crate::foundation::Application;
fn find_error<E: std::error::Error + Send + Sync + 'static>(error: &Error) -> Option<&E> {
error
.downcast_ref::<E>()
.or_else(|| error.chain().find_map(|cause| cause.downcast_ref::<E>()))
}
#[doc(hidden)]
pub fn __run<F: Future>(future: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("the test runtime couldn't be created")
.block_on(future)
}
#[doc(hidden)]
pub fn __seed_test(app: &Application, name: &str) {
#[cfg(feature = "database")]
app.seed_faker_default(name);
#[cfg(not(feature = "database"))]
let _ = (app, name);
}
impl Application {
#[track_caller]
pub fn travel(&self, amount: impl Into<jiff::ZonedDateTimeArithmetic>) -> &Self {
let Ok(time) = crate::clock::utc(self.clock().now()).checked_add(amount) else {
panic!("tests can only travel to times between 1970 and 2554");
};
self.travel_to(time)
}
#[track_caller]
pub fn travel_to(&self, time: impl Into<jiff::Timestamp>) -> &Self {
self.clock().stop_at(Some(time.into()));
self
}
pub fn freeze_time(&self) -> &Self {
self.travel_to(crate::clock::utc(self.clock().now()))
}
#[expect(
clippy::missing_panics_doc,
reason = "the start of a second of the clock's own time is always a valid timestamp"
)]
pub fn freeze_second(&self) -> &Self {
let second = crate::clock::utc(self.clock().now()).timestamp().as_second();
self.travel_to(jiff::Timestamp::from_second(second).expect("a whole second of a valid time is valid"))
}
pub fn travel_back(&self) -> &Self {
self.clock().stop_at(None);
self
}
}
impl Application {
pub fn swap<T: Send + Sync + 'static>(&self, service: T) -> Arc<T> {
let service = Arc::new(service);
self.services().insert(Arc::clone(&service));
service
}
pub fn swap_as<T: ?Sized + Send + Sync + 'static>(&self, service: Arc<T>) -> &Self {
self.services().insert(service);
self
}
}
impl Application {
pub async fn listener<E, Args>(&self, listener: impl Listener<E, Args>, event: E) -> crate::Result<()>
where
E: Send + Sync + 'static,
{
self.scope(async || listener.call(Event::__new(Arc::new(event))).await)
.await
}
}