Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to unit test a ModelLayer class #12

Open
davincho opened this issue Feb 5, 2015 · 3 comments
Open

Best way to unit test a ModelLayer class #12

davincho opened this issue Feb 5, 2015 · 3 comments

Comments

@davincho
Copy link

davincho commented Feb 5, 2015

As already discussed in #8, only within a ModelLayer class it is possible to start and commit/rollback transactions, as for instance per definition Model methods should be atomic.

Now talking about PHPUnit, I would like to start in my setUp routine a transaction which will be rollbacked after a test completes to ensure data consistency among all tests.

What is the best way to achieve this? Or should I do something like:

/** @before */
public function setUp() {
  $db->getConnection()->executeAnonymousQuery('begin transaction')
}

/** @after */
public function tearDown() {
  $db->getConnection()->executeAnonymousQuery('rollback transaction')
}

I am using Silex by the way ...

Cheers

@davincho davincho closed this as completed Feb 6, 2015
@davincho davincho reopened this Feb 6, 2015
@chanmix51
Copy link
Member

per definition Model methods should be atomic.

That's true, so must be one query per model method. The model layer is intended to group several model's method calls into transactions.

Model's methods must be unit tested on their own so the model layer's test can be kept simple.

Why not nesting model's methods in transactions to rollback them after the test, it is a good idea.

@tlode
Copy link
Contributor

tlode commented Feb 6, 2015

I am using begin / rollback in tests in the way you described. But as my ModelLayer classes, which I want to test, use transactions itself, I need the ability that transactions can be nested through the use of savepoints. I achieve this through this trait, which overwrites the default transaction methods in ModelLayer.

But I am also interested in how others doing this. @chanmix51 said using transactions in tests is not a good idea. Instead one should use a separate schema for the tests and drop it afterwards. But since the schema is fixed in RowStructure::setRelation() by the cli model generator, how could this be circumvented?

@davincho
Copy link
Author

davincho commented Feb 7, 2015

Thanks @tlode for providing the link to the transcation trait - I am going to use it.

Since I am in the same situation like tlode, nesting of transcations would be really useful - a possible enhancement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants