I'm trying to determine if my approach to testing is ok or completely wrong.
When I looked up how to test database code in Symfony3:
http://symfony.com/doc/current/testing/database.html
They say I should mock stuff. However I'm not doing this.
For example my tests for API - update post:
- Accessing the container
- Adding post to test DB
- Preparing new data to update
- Hitting API endpoint for update
- Compare results
I'm not mocking anything. I saw on github that people use it and then I saw that on stack overflow many people don't even mention it.
Since I've written a lot of code I will just point you to specific file:
(I know that this code is missing proper assertions but that's not the point)
/** @test */
public function test_logged_user_can_edit_story()
{
$this->insertFixtures('SingleUser');
$user = $this->entityManager->getRepository(User::class)->find(1);
$story = $this->factory(new Story(), [
'title' => 'Title',
'teaser' => 'test',
'content' => 1,
'user' => 1,
'status' => 1,
'view_count' => 1,
'favourite_count' => 0,
'created_at' => new \DateTime(),
'updated_at' => new \DateTime()
]);
$this->entityManager->persist($story);
$this->entityManager->flush();
$this->actAsLoggedUser($user);
$data = [
'story_form' => [
'title' => 'Test Title',
'teaser' => 'Test Teaser - intro to the story that should also by displayed in story index',
'content' => 'Test Content this is some long, long, long tet content that should be at least 50
characters long to pass the test',
]
];
$this->client->request('GET', $this->apiEndpoint . '/1', $data);
$story = $this->entityManager->getRepository(User::class)->find(1);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
I've created a bunch of helpers and test automation process as u can see in my repo:
- faster way to load data
- faster way to act as logged user(this will have JWT version in the future as well)
What I'm trying to learn is how to approach database and functional testing in general.
By looking at provided repository and pointed file(from where you can trace my testing logic), could someone put me on the right track here, or let me know what should I rethink?
Maybe my approach is completely wrong and I should start from scratch?
Thank you in advance
P.S Please remember that's work in progress so a lot of things will not be perfect.