Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Use Laravel’s Built-in SetUp Hooks for Application Test Traits

    Use Laravel’s Built-in SetUp Hooks for Application Test Traits

    August 6, 2024

    The Laravel framework provides a bunch of helpful test traits that make our lives easier, such as migrating databases, setting up Faker, removing middleware, and more. When writing feature tests in Laravel, the base TestCase class inherits the framework’s TestCase class. In this class, is the base setUp template is called in PHPUnit:

    protected function setUp(): void
    {
    $this->setUpTheTestEnvironment();
    }

    The setUpTheTestEnvironment() method—found in the TestCase’s InteractsWithTestCaseLifecycle trait—does a few things:

    Clear resolved Facade instances
    Refreshes the application
    Boots testing helper traits
    Sets the Model class event dispatcher instance
    Marks that the setup has run

    The part that I want to focus on is the setUpTraits() method, which recursively returns traits on the instance. This method includes some internal framework trait checks and calls methods on those traits if they’re used:

    if (isset($uses[RefreshDatabase::class])) {
    $this->refreshDatabase();
    }

    if (isset($uses[DatabaseMigrations::class])) {
    $this->runDatabaseMigrations();
    }

    // … etc

    At the end of the method, it loops through all the traits and checks for the existence of a setup/teardown method that matches the trait name:

    foreach ($uses as $trait) {
    if (method_exists($this, $method = ‘setUp’.class_basename($trait))) {
    $this->{$method}();
    }

    if (method_exists($this, $method = ‘tearDown’.class_basename($trait))) {
    $this->beforeApplicationDestroyed(fn () => $this->{$method}());
    }
    }

    If you had a MyUsefulHelper trait on a test class, the method_exists check would look for these methods:

    public function setUpMyUsefulHelper();
    public function tearDownMyUsefulHelper();

    What’s neat about the trait setup methods is that you can avoid boilerplate code anywhere you want to use this trait in a test:

    class MyTestCase
    {
    use MyUsefulHelper;

    // This method is not needed if a `setUpMyUsefulHelper` method is defined.
    – public function setUp: void
    – {
    – parent::setUp();
    –
    – $this->initMyUsefulHelper();
    – }
    }

    If your application has useful helpers, setup code, etc., using these hooks can clean up your code, and you don’t have to remember to manually initialize the trait’s setup. For example, perhaps you have factory code in multiple tests that sets up users, permissions, and roles. Your setup code could be moved into a reusable trait that automatically sets everything up before each test.

    Recently, I created a trait to interact with a Wiremock server that required some setup and teardown logic, such as defining custom mocked request/response pairs (and cleaning them up afterwards). I created an InteractsWithWiremock trait that automatically takes care of set up and reset automatically using Laravel’s automatic setUp hook.

    I love finding small nuggets like this in the Laravel framework code!

    The post Use Laravel’s Built-in SetUp Hooks for Application Test Traits appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleJudge rules Google violated antitrust laws, sparking speculation on how other ongoing antitrust investigations against tech companies will play out
    Next Article jQuery UI 1.14.0 Released (Reduced Legacy Browser Support)

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4610 – WordPress WP-Members Membership Plugin Stored Cross-Site Scripting Vulnerability

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    AI Chatbot Development Explained: Costs, Trends, and Future (2025)

    Web Development

    You can get Amazon’s new Echo Spot alarm clock at 40% off through Prime Day

    Development

    CVE-2025-4173 – SourceCodester Online Eyewear Shop SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    This AI Paper from Stanford Provides New Insights on AI Model Collapse and Data Accumulation

    Development
    GetResponse

    Highlights

    Artificial Intelligence

    Voice content moderation with AI: Everything you need to know

    November 25, 2024

    Voice content is booming, but it’s getting messier by the day. From toxic gaming chat…

    20 Best Figma UI Kits for Designers in 2025

    January 2, 2025

    CISA refutes claims it has been ordered to stop monitoring Russian cyber threats

    March 16, 2025

    CVE-2025-23178 – Apache HTTP Server SSL/TLS Channel Hijacking Vulnerability

    April 29, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.