Skip to content

Commit a08e9db

Browse files
committed
docs(mockery): add integration guide for Mockery mocking library
1 parent f8bef4f commit a08e9db

3 files changed

Lines changed: 190 additions & 0 deletions

File tree

.vitepress/config.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ gtag('config', 'G-VYGDN3X0PR');`],
113113
text: 'Integrations',
114114
items: [
115115
{ text: 'Infection', link: '/docs/bridge/infection.md' },
116+
{ text: 'Mockery', link: '/docs/bridge/mockery.md' },
116117
],
117118
},
118119
{
@@ -189,6 +190,7 @@ gtag('config', 'G-VYGDN3X0PR');`],
189190
text: 'Интеграции',
190191
items: [
191192
{ text: 'Infection', link: '/ru/docs/bridge/infection.md' },
193+
{ text: 'Mockery', link: '/ru/docs/bridge/mockery.md' },
192194
],
193195
},
194196
{

docs/bridge/mockery.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
outline: [2, 3]
3+
llms: true
4+
llms_description: "Integration with Mockery mocking library: installation, setup at application and suite levels, automatic mock expectation verification after each test, status mapping (Passed, Risky, Failed)."
5+
---
6+
7+
# Mockery
8+
9+
[Mockery](https://github.com/mockery/mockery) is a popular PHP mocking library with an expressive API (`Mockery::mock()`, `expects()`, `spy()`). The `testo/bridge-mockery` package integrates it with Testo.
10+
11+
<plugin-info name="Mockery" class="\Testo\Bridge\Mockery\MockeryPlugin" />
12+
13+
## Installation
14+
15+
```bash
16+
composer require --dev mockery/mockery testo/bridge-mockery
17+
```
18+
19+
## Setup
20+
21+
Register the <class>\Testo\Bridge\Mockery\MockeryPlugin</class> in the `plugins` section — either at the application level (for all suites) or for a specific Test Suite (for a particular set of tests).
22+
23+
::: code-group
24+
```php [Application level]
25+
use Testo\Application\Config\ApplicationConfig;
26+
use Testo\Bridge\Mockery\MockeryPlugin;
27+
28+
return new ApplicationConfig(
29+
src: ['src'],
30+
suites: [ /** Suites **/ ],
31+
plugins: [
32+
new MockeryPlugin(),
33+
],
34+
);
35+
```
36+
37+
```php [Suite level]
38+
use Testo\Application\Config\SuiteConfig;
39+
use Testo\Bridge\Mockery\MockeryPlugin;
40+
41+
new SuiteConfig(
42+
name: 'Unit',
43+
location: ['tests/Unit'],
44+
plugins: [
45+
new MockeryPlugin(),
46+
],
47+
);
48+
```
49+
:::
50+
51+
## Mock Expectation Verification
52+
53+
The bridge automatically verifies mock expectations after each test and maps the result to Testo statuses — manual teardown is not required. The rule is straightforward: **if you declare an expectation and fulfill it, the test passes; if you don't, it fails.**
54+
55+
- **A fulfilled expectation** counts as an assertion. So a test whose only check is a mock stays <enum>\Testo\Core\Value\Status::Passed</enum> rather than <enum>\Testo\Core\Value\Status::Risky</enum> (Testo marks a passing test with no assertions as risky — a safeguard against forgotten assertions, see <plugin>Assert</plugin>).
56+
- **An unfulfilled expectation** fails the test (**Failed**) and shows up in the assertion history as a failure.
57+
58+
::: code-group
59+
```php [Passed]
60+
#[Test]
61+
public function notifies(): void
62+
{
63+
$mailer = Mockery::mock(Mailer::class);
64+
$mailer->expects('send')->once(); // expectation — and an assertion at that
65+
66+
(new Notifier($mailer))->notify('hi'); // send() is called once, fulfilling the expectation
67+
}
68+
```
69+
70+
```php [Risky]
71+
#[Test]
72+
public function notifies(): void
73+
{
74+
$mailer = Mockery::spy(Mailer::class);
75+
76+
(new Notifier($mailer))->notify('hi');
77+
// no expectations, no assertions — nothing to check → Risky
78+
}
79+
```
80+
81+
```php [Failed]
82+
#[Test]
83+
public function notifies(): void
84+
{
85+
$mailer = Mockery::mock(Mailer::class);
86+
$mailer->expects('send')->once(); // expect exactly one send() call
87+
88+
// …but notify() was never called → expectation unfulfilled → Failed
89+
}
90+
```
91+
:::
92+
93+
::: info
94+
The plugin only works with regular tests (for example, those marked with the <attr>\Testo\Test</attr> attribute). In inline tests, benchmarks, and other test types, mocks are not verified or cleaned up automatically — you'll need to handle that yourself, directly through Mockery.
95+
:::

ru/docs/bridge/mockery.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
outline: [2, 3]
3+
---
4+
5+
# Mockery
6+
7+
[Mockery](https://github.com/mockery/mockery) — популярная библиотека мокирования для PHP с выразительным API (`Mockery::mock()`, `expects()`, `spy()`). Пакет `testo/bridge-mockery` интегрирует её с Testo.
8+
9+
<plugin-info name="Mockery" class="\Testo\Bridge\Mockery\MockeryPlugin" />
10+
11+
## Установка
12+
13+
```bash
14+
composer require --dev mockery/mockery testo/bridge-mockery
15+
```
16+
17+
## Подключение
18+
19+
Зарегистрируйте <class>\Testo\Bridge\Mockery\MockeryPlugin</class> в секции `plugins` — на уровне приложения (для всех сьютов) или отдельного Test Suite (для конкретного набора тестов).
20+
21+
::: code-group
22+
```php [Уровень приложения]
23+
use Testo\Application\Config\ApplicationConfig;
24+
use Testo\Bridge\Mockery\MockeryPlugin;
25+
26+
return new ApplicationConfig(
27+
src: ['src'],
28+
suites: [ /** Suites **/ ],
29+
plugins: [
30+
new MockeryPlugin(),
31+
],
32+
);
33+
```
34+
35+
```php [Уровень Test Suite]
36+
use Testo\Application\Config\SuiteConfig;
37+
use Testo\Bridge\Mockery\MockeryPlugin;
38+
39+
new SuiteConfig(
40+
name: 'Unit',
41+
location: ['tests/Unit'],
42+
plugins: [
43+
new MockeryPlugin(),
44+
],
45+
);
46+
```
47+
:::
48+
49+
## Поведение проверок моков
50+
51+
Адаптер автоматически проверяет ожидания моков после каждого теста и связывает результат со статусами Testo — ручной teardown не нужен. Правило простое: **задекларировали ожидание и выполнили — хорошо, не выполнили — ошибка.**
52+
53+
- **Выполненное ожидание** засчитывается как проверка. Поэтому тест, где единственная проверка — мок, остаётся <enum>\Testo\Core\Value\Status::Passed</enum>, а не <enum>\Testo\Core\Value\Status::Risky</enum> (этим статусом Testo помечает успешный тест без единой проверки — защита от забытого ассерта, см. <plugin>Assert</plugin>).
54+
- **Невыполненное ожидание** проваливает тест (**Failed**) и попадает в историю проверок как неуспешное.
55+
56+
::: code-group
57+
```php [Passed]
58+
#[Test]
59+
public function notifies(): void
60+
{
61+
$mailer = Mockery::mock(Mailer::class);
62+
$mailer->expects('send')->once(); // ожидание — и есть проверка
63+
64+
(new Notifier($mailer))->notify('hi'); // send() вызывается один раз, выполняя ожидание
65+
}
66+
```
67+
68+
```php [Risky]
69+
#[Test]
70+
public function notifies(): void
71+
{
72+
$mailer = Mockery::spy(Mailer::class);
73+
74+
(new Notifier($mailer))->notify('hi');
75+
// ни ожидания, ни ассерта — проверять нечего → Risky
76+
}
77+
```
78+
79+
```php [Failed]
80+
#[Test]
81+
public function notifies(): void
82+
{
83+
$mailer = Mockery::mock(Mailer::class);
84+
$mailer->expects('send')->once(); // ждём ровно один вызов send()
85+
86+
// …но notify() так и не вызвали → ожидание не выполнено → Failed
87+
}
88+
```
89+
:::
90+
91+
::: info
92+
Плагин настроен только на обычные тесты (например, помеченные атрибутом <attr>\Testo\Test</attr>). Во встроенных тестах, бенчмарках и других моки автоматически не проверяются и не очищаются — этим придётся управлять самостоятельно средствами Mockery.
93+
:::

0 commit comments

Comments
 (0)