# Hooks

Hooks are methods that allow you to perform scaffolding operations, free resources, or perform some other operation before or after a test is processed. The hooks that can be defined in AsyncUnit are:

* `Cspray\Labrador\AsyncUnit\Attribute\BeforeAll`
* `Cspray\Labrador\AsyncUnit\Attribute\BeforeEach`
* `Cspray\Labrador\AsyncUnit\Attribute\AfterEach`
* `Cspray\Labrador\AsyncUnit\Attribute\AfterAll`
* `Cspray\Labrador\AsyncUnit\Attribute\BeforeEachTest`\*
* `Cspray\Labrador\AsyncUnit\Attribute\AfterEachTest`\*

{% hint style="info" %}
Methods marked with a \* can only be defined on a `TestSuite`. Defining them on a `TestCase` is currently ignored and in future versions will result in a compilation error. All other attributes can be defined on methods belonging to either a `TestSuite` or a `TestCase`.
{% endhint %}

Based on whether you have defined an explicit `TestSuite` your hook lifecycle will have 2 "effective" orders. The order of hooks for the explicit `TestSuite` is followed for all suites, even the `ImplicitTestSuite`, however the implicit suite will *never* have a hook defined and thus is effectively always skipping the `TestSuite` specific hooks.

### Hook order for ImplicitTestSuite

If you are using the `ImplicitTestSuite` then the hook order is straightforward and, for the most part, aligns with what you'd be used to with PHPUnit or other testing frameworks. The following hook lifecycle applies to each `TestCase`.

1. `BeforeAll` methods defined are executed when a given `TestCase` has started processing. These methods will only be invoked 1 time per `TestCase`, even if multiple tests are defined. This should be a static class method.
2. `BeforeEach` methods defined on the `TestCase` are executed when a given test has started processing. These methods will be invoked 1 time, with a fresh `TestCase` instance, for every single test.
3. The test currently being processed will be executed.
4. `AfterEach` methods defined on the `TestCase` are executed when a given test has finished processing. These methods will be invoked 1 time, with a fresh `TestCase` instance, for every single test.
5. `AfterAll` methods defined on the `TestCase` are executed when *all* tests are finished executing for the given `TestCase`. These methods will only be invoked 1 time per `TestCase`, even if multiple tests are defined. This should be a static class method.

### Hook order for explicit TestSuite

If you have defined an explicit `TestSuite` then your hook order starts to become more advanced, and with advance functionality comes an increase in complexity. It is important before you start defining your own hooks on `TestSuite` you understand how they operate and interact with your tests. The following hook lifecycle applies to each `TestSuite`

1. `BeforeAll` methods defined on the `TestSuite` are executed when a given suite has started processing. These methods will only be invoked 1 timer per `TestSuite`. Only one `TestSuite` instance will be created for each suite processed so there is no need for this to be a static method.
2. `BeforeEach` methods defined on the `TestSuite` are executed before a given `TestCase` has started processing. These methods will be invoked 1 time for every `TestCase` associated to the suite.
3. `BeforeAll` methods defined on the `TestCase` are executed. Follows the same rules for the implicit use case.
4. `BeforeEachTest` methods defined on the `TestSuite` are executed before a given test has started. These methods will be invoked 1 time for every single test.
5. `BeforeEach` methods defined on the `TestCase` are executed. Follows the same rules for the implicit use case.
6. The test currently being processed will be executed.
7. `AfterEach` methods defined on the `TestCase` are executed. Follows the same rules for the implicit use case.
8. `AfterEachTest` methods defined on the `TestSuite` are executed after a given test has finished. These methods will be invoked 1 time for every single test.
9. `AfterAll` methods defined on the `TestCase` are executed . Follows the same rules for the implicit use case.
10. `AfterEach` methods defined on the `TestSuite` are executed after a given `TestCase` has finished processing. These methods will be invoked 1 time for every `TestCase` associated to the suite.
11. `AfterAll` methods defined on the `TestSuite` are executed when a given suite has finished processing. These methods will only be invoked 1 time per `TestSuite`. Only one `TestSuite` instance will be created for each suite processed so there is no need for this to be a static method.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.labrador-kennel.io/async-unit/reference/hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
