-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[WIP] Adding a testing/database cookbook #2407
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
Making some minor bugfixes to the testing/database cookbook. Added di…
…fferent code examples
- Loading branch information
commit 00d24fa324130a0a46cf26614727789f0e8cf54b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,16 +59,16 @@ easy to pass a mock object within a test:: | |
->method('getBonus') | ||
->will($this->returnValue(1100)); | ||
|
||
// Now, mock tthe repository so it returns the mock of the employee | ||
// Now, mock the repository so it returns the mock of the employee | ||
$employeeRepository = $this->getMockBuilder('\Doctrine\ORM\EntityRepository') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$employeeRepository->expects($this->once()) | ||
->method('__call') | ||
->method('find') | ||
->will($this->returnValue($employee)); | ||
|
||
// Last, mock the EntityManager to return the mock of the repository | ||
$entityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager') | ||
$entityManager = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$entityManager->expects($this->once()) | ||
|
@@ -83,14 +83,6 @@ easy to pass a mock object within a test:: | |
We are building our mocks from the inside out, first creating the employee which | ||
gets returned by the ``Repository`` which itself gets returned by the ``EntityManager``. | ||
This way, no real class is involved in testing. | ||
|
||
.. note:: | ||
|
||
As a ``Repository`` doesn't have a ``find()`` method but uses the magic method | ||
``__call``, you cannot define a mock method on ``find()`` but must instead use | ||
``__call``. It's possible to implement the ``find()`` method in your ``Repository`` | ||
and use it when mocking. In this case, make sure to mock your ``Repository`` | ||
class instead of the generic ``EntityRepository``. | ||
|
||
Changing database settings for functional tests | ||
----------------------------------------------- | ||
|
@@ -100,15 +92,43 @@ Most of the time you want to use a dedicated database connection to make sure | |
not to overwrite data you entered when developing the application and also | ||
to be able to clear the database before every test. | ||
|
||
To do this, you can specify the database configuration inside your ``app/config/config_test.yml``:: | ||
To do this, you can specify a database configuration which overwrites the default | ||
configuration: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config_test.yml | ||
doctrine: | ||
# ... | ||
dbal: | ||
host: localhost | ||
dbname: testdb | ||
user: testdb | ||
password: testdb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add other formats as well |
||
|
||
Make sure that your database runs on localhost and has the defined database and | ||
user credentials set up. | ||
.. code-block:: xml | ||
|
||
<!-- app/config/config_test.xml --> | ||
<doctrine:config> | ||
<doctrine:dbal | ||
host="localhost" | ||
dbname="testdb" | ||
user="testdb" | ||
password="testdb" | ||
> | ||
</doctrine:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config_test.php | ||
$configuration->loadFromExtension('doctrine', array( | ||
'dbal' => array( | ||
'host' => 'localhost', | ||
'dbname' => 'testdb', | ||
'user' => 'testdb', | ||
'password' => 'testdb', | ||
), | ||
)); | ||
|
||
Make sure that your database runs on localhost and has the defined database and | ||
user credentials set up. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be: