programmieren:testing:unittest:controller_tests
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| programmieren:testing:unittest:controller_tests [2026/08/25 16:42] – Add code blocks admin | programmieren:testing:unittest:controller_tests [2026/09/09 09:39] (current) – actually show controller tests admin | ||
|---|---|---|---|
| Line 53: | Line 53: | ||
| 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.1787668946.txt.gz · Last modified: by admin
