Git Product home page Git Product logo

kahlan-pdo's Introduction

Kahlan PDO

Build Status SensioLabsInsight

๐Ÿ—„ Provides functionality to work with PDO, reset a database and load fixtures within Kahlan

Requirements

  • PHP >= 7.0
  • Kahlan ^3.0

Installation

composer require brunty/kahlan-pdo --dev

Setup

Folders:

  1. Create a folder to store your database related files (/spec/db is suggested)
  2. Create a folder to store your SQL files to load into the database (/spec/db/sql is suggested)
  3. Within the folder where your database related files will be stored created a file reset.php which will be the file you'll use to reset your DB whenever you call the function to do so.
  4. Within the folder where your database related files will be stored, created a fixtures folder
  5. Create fixtures as you need for your tests in the fixtures folder.

Config:

In kahlan-config.php setup the path to your db folder and if you wish to use the \Brunty\Kahlan\PDO\sql() helper function, add the path to a directory that will hold SQL files as follows:

\Kahlan\box('db.path', __DIR__ . '/spec/db');
\Kahlan\box('db.path.sql', __DIR__ . '/spec/db/sql');

Usage

<?php

use function Brunty\Kahlan\PDO\reset;
use function Brunty\Kahlan\PDO\fixture;
use function Brunty\Kahlan\PDO\db;

describe('SqliteThingRepository', function() {

    beforeEach(function() {
        reset(); // reset our database before each test
    });

    it('gets all things from the database', function() {
        fixture('things'); // load fixtures inside this test

        // do stuff
        $stmt = db()->query('SELECT * FROM Things');
        $things = $stmt->fetchAll();
        // run assertions
    });
});

Using the \Brunty\Kahlan\PDO\reset() function without a parameter will create an in-memory database in SQLite, but you can pass the DSN, username and password to it and it'll use those instead.

With loading fixtures, you can then create a file within /spec/db/fixtures and call the name of that file (without the .php extension) to do whatever you might need to load data into the database.

For example:

\Brunty\Kahlan\PDO\load('things'); would load the file: /spec/db/fixtures/things.php into the database.

You could setup objects yourself with something like Faker or you could just load SQL into the database directly:

In /spec/db/fixtures/things.php:

<?php

\Brunty\Kahlan\PDO\sql('things');

The helper function \Brunty\Kahlan\PDO\db() returns the instance of PDO that is in the Kahlan box.

The helper function \Brunty\Kahlan\PDO\sql() loads SQL from the file given (in the \Kahlan\box('db.path.sql') directory, without the .sql extension) into the PDO instance in the Kahlan box.

Contributing

This started as a small personal project.

Although this project is small, openness and inclusivity are taken seriously. To that end a code of conduct (listed in the contributing guide) has been adopted.

Contributor Guide

kahlan-pdo's People

Contributors

brunty avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.