Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:44:22+00:00 2026-06-10T06:44:22+00:00

I am trying to write unit tests for a file import export module. One

  • 0

I am trying to write unit tests for a file import export module.

One of my method checks that a filename passed exists.

How would this be mocked and a test written to check if the file exists or not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:44:24+00:00Added an answer on June 10, 2026 at 6:44 am

    Unit tests are supposed to prove that a unit of code functions correctly in complete isolation. If your test depends on the file system to function correctly in order to pass, your test is suboptimal and could be lying to you on any given test run.

    Like any experiment, when you have more than one variable in play, you can’t be sure of your results. For PHP code that interacts with the file system, it’s best to mock the file system using a custom stream wrapper (usually vfsStream, but you can easily write your own stream wrapper if you really want to).

    $noTest < $testWithFileSystemDependency < $testThatMocksFileSystem

    Usually, this is accomplished by passing file paths into the methods that use them directly:

    <?php
    function myFunction($someFilePath) {
        // do stuff
    }
    

    In this way, you can mock the file system and pass in a testable dummy path that will behave how you have mocked it to behave.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write unit tests for a method that takes a String filename,
Im trying to write some unit tests for a couple of merthods that are
I'm trying to write a unit test that checks some xml parsing code. The
I'm trying to write unit tests for an application that reports on Entries in
I'm trying to write some unit tests for a JPA model that I've built
I'm trying to write a unit test for a method that calls a FileWriter.
I'm trying to write some unit tests for my NetBeans module, but i'm unable
I'm trying to write unit tests in MSTest and I've created two TestClasses. When
I am trying to write unit tests for a bit of code involving Events.
I am trying to write some unit tests and I was wondering if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.