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 7557805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:13:42+00:00 2026-05-30T12:13:42+00:00

I have doctrine2 setup with ZF 1.11, I am having difficulty learning the best

  • 0

I have doctrine2 setup with ZF 1.11, I am having difficulty learning the best way to unit test controllers. I would like to be able to test actions in controllers without writing to the database within my unit tests however I am finding it impossible to actually do so.

For example say I have the following hypothetical action within my controller.

public function redirectUserAction() {
    $registry = Zend_Registry::getInstance();
    $em = $registry->entitymanager;
    $user = $em->getRepository('Application_Model_User')
               ->findOneBy(array('email' => 'test@test.com'));
    if(is_object($user)) {
        $this->_redirect('/');
    }
}

I want to test that if there is a user available with the email address test@test.com it will redirect to / so at the moment I have the following test.

public function testUserAvaliableShouldRedirectIndex() {
    $registry = Zend_Registry::getInstance();
    $em = $registry->entitymanager;
    $user = new Application_Model_User();
    $user->setEmail('test@test.com');
    $em->persist($user);
    $em->flush();
    $this->dispatch('/index/redirect-user');
    $this->assertRedirectTo('/'); 
    $em->remove($user);
    $em->flush(); 
}

This seems to me like a very messy way of writing tests. I was under the impression that with doctrine I wouldn’t need to actually flush to the database because my objects would be stored in the persistence layer but at the moment my tests fail without flushing.

If anyone knows a better way I could write tests or can help me see where I am going wrong I would be very grateful.

Thanks

  • 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-05-30T12:13:44+00:00Added an answer on May 30, 2026 at 12:13 pm

    Instead of creating and deleting the user in testUserAvaliableShouldRedirectIndex() you could use setUp() and tearDown() to create and delete the user. This way you can use that user throughout all actions that expect an existing user, like your redirectUserAction(), loginAction(), etc. instead of having to do what you currently do for each of these actions.

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

Sidebar

Related Questions

I am looking for some, I have the following table setup in my test
I have setup a doctrine listener which is fired on different database actions and
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a simple table like following: class SnookerCurrentInfo extends Doctrine_Record { public function
i am on windows/apache/mysql/php setup. i have recently reinstalled PHP. but i found that
I have a created a simple doctrine2/zend skeleton project and am trying to get
I am trying to learn about unit testing in Zend Framework. I have installed
I have a basic M:N setup with three tables: candidate, position, and candidate_position. Here's
I'm using Symfony2 and Doctrine2 to create a blog engine. I will have three
Is there a way to use Doctrine using the model classes I've already setup

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.