laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
extern crate self as app;

use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use laravel::Application;
use laravel::config::Env;
use laravel::container::Inject;
use laravel::testing::MakesHttpRequests;

pub mod testing {
    pub fn create() -> laravel::Application {
        super::application()
    }
}

struct Counter(AtomicUsize);

async fn memo() -> usize {
    laravel::once(|| async {
        laravel::resolve::<Counter>()
            .await
            .unwrap()
            .0
            .fetch_add(1, Ordering::SeqCst)
    })
    .await
}

async fn increment(_: Inject<Counter>) -> String {
    memo().await.to_string()
}

fn application() -> Application {
    Application::configure()
        .replace_env(Env::from_iter([
            ("APP_ENV", "testing"),
            ("APP_NAME", "Attribute tests"),
        ]))
        .with_services(|services| {
            services.instance(Counter(AtomicUsize::new(0)));
        })
        .with_routing(|routing, _| {
            routing.api(|routes| {
                routes.get("/counter", increment);
            });
        })
        .booted(|app| async move {
            app.resolve::<Counter>().await?.0.store(1, Ordering::SeqCst);
            Ok(())
        })
        .create()
}

#[laravel::test]
fn synchronous_unit_test() -> Result<(), String> {
    assert!(laravel::context::Context::current().is_none());
    assert!(tokio::runtime::Handle::try_current().is_err());
    assert_eq!("42".parse::<u8>().map_err(|error| error.to_string())?, 42);
    Ok(())
}

#[laravel::test]
async fn asynchronous_unit_test() -> laravel::Result<()> {
    assert!(laravel::context::Context::current().is_none());
    tokio::time::sleep(Duration::from_millis(1)).await;
    assert_eq!(tokio::spawn(async { 42 }).await?, 42);
    Ok(())
}

#[laravel::test]
async fn application_tests_boot_and_share_services(app: Application) -> laravel::Result<()> {
    assert_eq!(laravel::config().app.name, "Attribute tests");
    let counter = laravel::resolve::<Counter>().await.unwrap();
    assert_eq!(counter.0.load(Ordering::SeqCst), 1);
    app.get("/api/counter").await.assert_ok().assert_see("1");
    assert_eq!(counter.0.load(Ordering::SeqCst), 2);
    assert_eq!(app.resolve::<Counter>().await.0.load(Ordering::SeqCst), 2);
    Ok(())
}

#[laravel::test]
async fn services_can_be_swapped_in_the_test_body(app: Application) {
    let counter = app.swap(Counter(AtomicUsize::new(41)));

    app.get("/api/counter").await.assert_ok().assert_see("41");
    assert_eq!(counter.0.load(Ordering::SeqCst), 42);
}

#[laravel::test]
async fn each_test_has_a_fresh_application(_app: Application) {
    assert_eq!(
        laravel::resolve::<Counter>()
            .await
            .unwrap()
            .0
            .fetch_add(1, Ordering::SeqCst),
        1
    );
}

#[laravel::test]
fn synchronous_application_tests_are_scoped(_app: Application) {
    assert_eq!(laravel::config().app.name, "Attribute tests");
}

#[laravel::test(app = crate::application)]
async fn request_contexts_do_not_replace_the_test_context(app: Application) {
    assert_eq!(memo().await, 1);
    app.get("/api/counter").await.assert_ok().assert_see("2");
    app.get("/api/counter").await.assert_ok().assert_see("3");
    assert_eq!(memo().await, 1);
    assert_eq!(laravel::config().app.name, "Attribute tests");
    assert!(!laravel::context::Context::current().unwrap().is_request());
}

#[laravel::test(app = crate::application)]
fn a_named_factory_scopes_a_test_without_the_argument() {
    assert_eq!(laravel::config().app.name, "Attribute tests");
}

#[laravel::test]
#[should_panic(expected = "ordinary panic")]
async fn should_panic_is_preserved() {
    panic!("ordinary panic");
}

fn failing_boot() -> Application {
    Application::configure()
        .replace_env(Env::default())
        .booted(|_| async { Err(laravel::Error::from(std::io::Error::other("test boot failure"))) })
        .create()
}

#[laravel::test(app = failing_boot)]
#[should_panic(expected = "booting the application failed: test boot failure")]
async fn boot_failures_fail_before_the_test_body(_app: Application) {
    panic!("the test body must not run");
}

#[laravel::test]
#[ignore = "checks that the generated test retains ignore"]
async fn ignored_tests_do_not_run() {
    panic!("ignored test ran");
}

mod named_application {
    #[laravel::test]
    async fn application(_app: laravel::Application) {
        assert_eq!(laravel::config().app.name, "Attribute tests");
    }
}