https://github.com/mpyw/mockery-pdo
[Experimental] BDD-style PDO Mocking Library for Mockery
https://github.com/mpyw/mockery-pdo
mockery pdo php
Last synced: 6 months ago
JSON representation
[Experimental] BDD-style PDO Mocking Library for Mockery
- Host: GitHub
- URL: https://github.com/mpyw/mockery-pdo
- Owner: mpyw
- License: mit
- Created: 2020-08-25T21:41:46.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-05T11:12:40.000Z (7 months ago)
- Last Synced: 2025-04-01T23:37:07.854Z (6 months ago)
- Topics: mockery, pdo, php
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 2
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mockery PDO [](https://github.com/mpyw/mockery-pdo/actions) [](https://coveralls.io/github/mpyw/mockery-pdo?branch=master)
> [!WARNING]
> **Experimental**BDD-style PDO Mocking Library for [`mockery/mockery`](https://github.com/mockery/mockery)
## Requirements
- PHP: `^8.2`
- Mockery: `^1.6.12`> [!NOTE]
> Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.## Installing
```bash
composer require mpyw/mockery-pdo:VERSION@alpha
```## Example
### SELECT
#### Basic
```php
$pdo = (new MockeryPDO())->mock();$pdo->shouldPrepare('select * from users where email = :email and active = :active')
->shouldBind()
->value('email', 'John')
->boolValue('active', true)
->shouldExecute()
->shouldFetchAllReturns([['id' => 1, 'name' => 'John', 'active' => 1]]);$this->assertInstanceOf(
PDOStatement::class,
$stmt = $pdo->prepare('select * from users where email = :email and active = :active')
);$this->assertTrue($stmt->bindValue('email', 'John'));
$this->assertTrue($stmt->bindValue('active', 'John', PDO::PARAM_BOOL));
$this->assertTrue($stmt->execute());$this->assertSame(
[['id' => 1, 'name' => 'John', 'active' => 1]],
$stmt->fetchAll()
);
```#### Bind values on `execute()` call
```php
$pdo = (new MockeryPDO())->mock();$pdo->shouldPrepare('select * from users where email = ? and active = ?')
->shouldExecute(['John', '1'])
->shouldFetchAllReturns([['id' => 1, 'name' => 'John', 'active' => 1]]);$this->assertInstanceOf(
PDOStatement::class,
$stmt = $pdo->prepare('select * from users where email = ? and active = ?')
);
$this->assertTrue($stmt->execute(['John', '1']));$this->assertSame(
[['id' => 1, 'name' => 'John', 'active' => 1]],
$stmt->fetchAll()
);
```#### Progressively fetch rows
```php
$pdo = (new MockeryPDO())->mock();$pdo->shouldPrepare('select * from users where email = :email and active = :active')
->shouldBind()
->value('email', 'John')
->boolValue('active', true)
->shouldExecute()
->shouldStartFetching()
->fetchReturns((object)['id' => 1, 'name' => 'John', 'active' => 1])
->with(PDO::FETCH_OBJ)
->fetchEnds();$this->assertInstanceOf(
PDOStatement::class,
$stmt = $pdo->prepare('select * from users where email = :email and active = :active')
);$this->assertTrue($stmt->bindValue('email', 'John'));
$this->assertTrue($stmt->bindValue('active', 'John', PDO::PARAM_BOOL));
$this->assertTrue($stmt->execute());$this->assertEquals((object)['id' => 1, 'name' => 'John', 'active' => 1], $stmt->fetch(PDO::FETCH_OBJ));
$this->assertFalse($stmt->fetch());
$this->assertFalse($stmt->fetch());
$this->assertFalse($stmt->fetch());
```### INSERT
```php
$pdo = (new MockeryPDO())->mock();$pdo->shouldPrepare('insert into users(email, active) values (?, ?)')
->shouldExecute(['John', '1'])
->shouldRowCountReturns(1);$this->assertInstanceOf(
PDOStatement::class,
$stmt = $pdo->prepare('insert into users(email, active) values (?, ?)')
);
$this->assertTrue($stmt->execute(['John', '1']));
$this->assertSame(1, $stmt->rowCount());
```