SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CakePHP: problem with fixtures, test vs. live data

    I worry that this question is a little specific for this forum, but I'm desperate, so here goes...

    I have a problem with a CakePHP unit test retrieving REAL data, where I need
    to use fixture data. I'll explain by way of example.

    - Model Widgets has a method abc().

    - Widget->abc() retrieves data from its own model, but also queries data from another model, Shapes. The 2 models are NOT joined by any table relationships. The Shapes model is invoked by calling App::import('Model','Shapes').

    - Fixtures exist for both the Widget and Shape models. Both fixtures are included at the top of the Widget test case class.

    - When I run unit tests on Widget->abc(), it dutifully retrieves data from the test_suite_widgets table created by the fixture. However, when Widget->abc() retrieves data from the Shapes model, that data comes from the LIVE database. I know this because I can see all of the SQL queries being executed.

    I hope this all makes sense. Any ideas?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fattyjules View Post
    The Shapes model is invoked by calling App::import('Model','Shapes')
    Is there a way of injecting the class name so you can use a mock of Shapes? Something like...

    PHP Code:
    //outside the method
    $shapes_class 'MockShapes';

    //inside the method being tested
    App::import('Model'$shapes_class
    Zealotry is contingent upon 100 posts and addiction 200?

  3. #3
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks auricle. I didn't really understand your suggestion, but it got me thinking. I tried a few things, and came up with a solution that worked. I hope someone else finds this useful.

    I put the following code in Widget::abc();

    Code:
            
    App::import('Model','Shape');
    $this->Shape= new Shape();
    
    if($this->useDbConfig=='test_suite') {
         $this->Place->useDbConfig = 'test_suite';
         $this->Place->tablePrefix = 'test_suite_';
    }
    It's not an elegant solution - you have to put code into model to explicitly deal with test cases - but it works.

  4. #4
    SitePoint Addict fattyjules's Avatar
    Join Date
    Dec 2005
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops - change 'Place' to 'Shape' in the code snippet above.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Zealotry is contingent upon 100 posts and addiction 200?

  6. #6
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you creating 'Widget' model object in your test case? Can you post your test case here?


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
  •