SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)

    PHPUnit and require_once'd files

    So, I'm working on a unit test for a function which loads a config file. This file creates an array which I then read. After it's read, that variable is unset (I don't want lingering globals or anything).

    Now, the problem is it seems PHPUnit only does a require_once once... which is a problem. =p

    So, if I do this:
    Code:
    require "config.php";
    var_dump(isset($config));
    it's always true.

    If I do:
    Code:
    require_once "config.php";
    var_dump(isset($config));
    it's true the first test, then false all others.

    Any ideas? I don't want to change a logical require_once to a require just for my unit tests. =S

    Thanks.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well...the problem is...you already included the file within the same process when you run the other test. Using "unset" on a variable from inside the file doesn't remove it from the included files. You may have to rethink how you access the config that doesn't require you to unset it. Or a way that allows you to rebuild the configuration after unsetting. Or just use "require".
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    664
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If I understand the problem correctly, you are basically running several tests inside of a PHPUnit_Framework_TestCase against your config code and getting tripped up by require_once?

    One possibility would be to put each test in their own TestCase. That should reset everything.

    But given that you seem to be dynamically generating config.php I would think that just using require in your code would suffice. Realistically speaking, how likely is it that the file ever would be included more than once?

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Darn, I was hoping there was some PHPUnit magic I was missing. =p

    I knew why it was happening, I just hoped PHPUnit had some way that it could correct this that I just didn't know about.

    The problem basically stems from the fact that I need to load the config file to properly test this method, but I need to create different instances of it to do my tests properly.

    I don't HAVE to unset the config, but I want to, so I avoid having lingering values left around. I could make the config file some kind of class, but I wanted to make it as easy as possible to edit (without the need to make it a text file that is frequently parsed).

    I guess I'll just leave it as require for now. It doesn't hurt anything if it's required more than once, it just seems kind of silly. However, in normal operation it would never happen, so it should be okay.

    Thanks.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    664
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just thinking: does the name of your generated config.php really matter? Assuming that this is something that is cached and pretty much hidden from normal view then you could simply generate a random name each time you generate it. Probably not worth the effort but it would get around the require_once issue.

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    //config.php
    function setVariables(){
        
    // set your variables, using global to make them... well, global
    }
    function 
    unsetVariables(){
        
    // unset your variables
    }
    setVariables();
    When you require_once that, it sets the variables. Then when you what them unset, just run the unsetVariables function and when you want them set again just use setVariables().
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    @samanime ; did you try @ahundiak ;'s suggestion of splitting your tests into seperate TestCases? That should solve the problem.

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    I didn't. I decided that the code for that portion wasn't so complex that I needed to go to such extremes to test it. =p

    The file isn't actually dynamically generated, it's just a simple file with dummy values I can test against. However, that part of the test was pretty simple, other than that. I ended up just leaving it as a require in the code since it wouldn't hurt anything if it was required multiple times.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •