Code Monkey home page Code Monkey logo

mockery-pdo's Introduction

Mockery PDO Build Status Coverage Status Scrutinizer Code Quality

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

mockery-pdo's People

Contributors

mpyw avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

hexium310

mockery-pdo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.