PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] How deal with related fixtures

 March 15, 2022     cakephp, cakephp-3.0, phpunit     No comments   

Issue

I am using phpunit to test my CakePHP application, however, I have created two fixtures:- A and B. A gets inserted first and B second, but B also has a a_id field that I need to fill from the first fixture.

A good example of what I mean exists in the documentation: https://book.cakephp.org/3.0/en/development/testing.html#loading-fixtures-in-your-test-cases whereby articles and comments fixtures are shown, but it is never shown how comments is built.

How do I do this with CakePHP fixtures?


Solution

Fixtures are not related to each other. You need to build your associations manually in the data they contain. Fixtures are just data used in the tests as the book explains it as well. The data will get inserted into the test DB no matter if they're related or not. They'll only fail if you put constrains on the DB tables it generates and the FKs aren't present.

When testing code that depends on models and the database, one can use fixtures as a way to generate temporary data tables loaded with sample data that can be used by the test. The benefit of using fixtures is that your test has no chance of disrupting live application data. In addition, you can begin testing your code prior to actually developing live content for an application.

So if you have comments create the records there with the FKs of your articles.



Answered By - floriank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing