Answer the question
In order to leave comments, you need to log in
Why test code?
Here I write for example a simple code.
I need to implement the saving of news in the database.
I go to phpmyadmin, see the structure of the table.
yeah, we know the structure of the columns and their names.
I am preparing a presentation
, etc. ...
let it be so for the speed of understanding.
In the controller, we validate the data, if they come, then we call the model
public function insertPages(Array $array) {
$params = [
"title" => $array['title'],
"url" => $array['url'],
"content" => $array['content'],
"keywords" => $array['keywords'],
"description" => $array['description'],
];
$sql = "INSERT INTO {$this->table} (`title`,`url`, `content`, `keywords`, `description`) VALUES (:title, :url, :content, :keywords, :description)";
if( !parent::save( $params, $sql ) ) {
throw new \Exception('Ошибка добавления статьи');
}
}
Answer the question
In order to leave comments, you need to log in
What is there to test and why? if it fails, we'll get an exception. We know the names of the columns. The data in the controller is validated.
What is there to test and why? if it fails, we'll get an exception. We know the names of the columns. The data in the controller is validated.
Usually in tiny projects (up to 200tr) tests are superfluous: such projects are made and forgotten.
If the project is supported, then the tests are required. You must be sure that changing the code will not spoil the main functionality.
The easiest way to get started on the web is with functional tests.
I recommend either integrating phpunit into your framework, or selenium, etc.
A separate question is whether you really need tests.
But if you really want to, then you can, for example, test that after calling insertPages you really have a page inserted.
To do this, mock the database, perform an insert, then get this record from the database, compare, checking whether what you wrote down and what was written matches.
What for? Well, for example, tomorrow some "Vasya" on the project will change the "content" field in the database to "content_id" and not normally refactor the code.
You can also check what will happen to invalid data, they should throw an exception, and so on.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question