programmieren:testing:unittest:controller_tests
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| programmieren:testing:unittest:controller_tests [2026/08/25 16:17] – created admin | programmieren:testing:unittest:controller_tests [2026/09/09 09:39] (current) – actually show controller tests admin | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| * Preparing session data by getting that certain entry from the test database | * Preparing session data by getting that certain entry from the test database | ||
| * Adding the whole user entity we got in the previous step to the session with an " | * Adding the whole user entity we got in the previous step to the session with an " | ||
| + | |||
| + | Kinda looks like this: | ||
| + | <code php> | ||
| + | [ | ||
| + | // Superuser | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ], | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | protected function getSuperuserSessionData(): | ||
| + | { | ||
| + | /** @var UsersTable $usersTable */ | ||
| + | $usersTable = $this-> | ||
| + | |||
| + | $sessionData = $usersTable-> | ||
| + | |||
| + | return $sessionData; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code php> | ||
| + | protected function loginAs($sessionData): | ||
| + | { | ||
| + | if (!method_exists($this, | ||
| + | throw new RuntimeException(' | ||
| + | } | ||
| + | |||
| + | $this-> | ||
| + | ' | ||
| + | ]); | ||
| + | } | ||
| + | </ | ||
| Only then can you request the controller like normal. | Only then can you request the controller like normal. | ||
| When controller actions include saving things, it makes sense to compare the expected value to the view variable and to the value that was saved to the database. | When controller actions include saving things, it makes sense to compare the expected value to the view variable and to the value that was saved to the database. | ||
| + | |||
| + | This would kinda look like this for an index endpoint for accounts table: | ||
| + | |||
| + | <code php> | ||
| + | public function testIndex(): | ||
| + | { | ||
| + | $accountsTable = $this-> | ||
| + | $accountCount = $accountsTable-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | $accounts = $this-> | ||
| + | |||
| + | $account = simplifyDateTimesInArray($accounts[0]-> | ||
| + | |||
| + | self:: | ||
| + | self:: | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ], | ||
| + | ], $account); | ||
| + | self:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Notice the '' | ||
| + | |||
| + | This is what the same looks like for an add post endpoint | ||
| + | |||
| + | <code php> | ||
| + | public function testAddPost(): | ||
| + | { | ||
| + | $accountsTable = $this-> | ||
| + | $data = [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | /** @var Account $account */ | ||
| + | $account = $accountsTable-> | ||
| + | |||
| + | self:: | ||
| + | self:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Here you just test if the entry you added actually corresponds to the data that was input. | ||
| + | |||
| + | And this is what a delete endpoint test looks like | ||
| + | |||
| + | <code php> | ||
| + | public function testDelete(): | ||
| + | { | ||
| + | $id = 1; | ||
| + | $accountsTable = $this-> | ||
| + | |||
| + | /** @var Account $account */ | ||
| + | $account = $accountsTable-> | ||
| + | self:: | ||
| + | |||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | |||
| + | $this-> | ||
| + | $this-> | ||
| + | |||
| + | /** @var Account $account */ | ||
| + | $account = $accountsTable-> | ||
| + | self:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Here you test beforehand if the entry with the selected ID exists (it should), then you call the delete endpoint and check the entry again, now it shouldnt exist. | ||
| + | |||
| + | For all endpoints you also check if it correctly redirects you (if it should do that). | ||
| + | |||
| + | You also check every endpoint as unauthenticated (unless it should be available as unauthenticated) and check if it correctly redirects to the login. | ||
programmieren/testing/unittest/controller_tests.1787667474.txt.gz · Last modified: by admin
