Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mockery Pdo Laravel Package

mpyw/mockery-pdo

Experimental BDD-style PDO mocking library built on mockery/mockery. Define expectations for prepare/bind/execute and return rows via fetch/fetchAll in a fluent API, making PDO-based code easy to test on PHP 8.2+.

View on GitHub
Deep Wiki
Context7

Mockery PDO Build Status Coverage Status

[!WARNING] Experimental

BDD-style PDO Mocking Library for 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

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());
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor