mpyw / mockery-pdo
BDD-style PDO Mocking Library for Mockery
Installs: 5 108
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 6
Forks: 1
Open Issues: 0
Requires
- php: ^7.3 || ^8.0
- ext-pdo: *
- mockery/mockery: ^1.3.3 || ^1.4.2
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-21 19:53:00 UTC
README
Warning
Experimental
BDD-style PDO Mocking Library for mockery/mockery
Requirements
- PHP:
^7.3 || ^8.0
- Mockery:
^1.3.3 || ^1.4.2
Installing
composer require mpyw/mockery-pdo:VERSION@alpha
Example
SELECT
Basic
$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
$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
$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
$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());