GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — codeception.github.com/docs/modules/Db.markdown at master · captbaritone/codeception.github.com

Expand file tree

/

Db.markdown

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
182 lines (116 loc) · 3.87 KB
/

Db.markdown

Copy path

File metadata and controls

182 lines (116 loc) · 3.87 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions
layout doc
title Db Module - Codeception - Documentation

Db Module

For additional reference, please review the source

Works with SQL database.

The most important function of this module is cleaning database before each test. That's why this module was added into global configuration file: codeception.yml. To have your database properly cleaned you should configure it to access the database. Also provides actions to perform checks in database.

In order to have your database populated with data you need a raw SQL dump. Just put it in {% highlight yaml %} tests/_data {% endhighlight %} dir (by default) and specify path to it in config. Next time after database is cleared all your data will be restored from dump. Don't forget to include CREATE TABLE statements into it.

Supported and tested databases are:

  • MySQL
  • SQLite (only file)
  • PostgreSQL

Supported but not tested.

  • MSSQL
  • Orcale

Connection is done by database Drivers, which are stored in Codeception\Util\Driver namespace. Check out drivers if you get problems loading dumps and cleaning databases.

Status

  • Maintainer: davert
  • stability:
    • Mysql: stable
    • SQLite: stable
    • Postgres: beta
    • MSSQL: alpha
    • Oracle: alpha
  • Contact: [email protected]

Please review the code of non-stable modules and provide patches if you have issues.

Config

  • dsn required - PDO DSN
  • user required - user to access database
  • password required - password
  • dump - path to database dump.
  • populate: true - should the dump be loaded before test suite is started.
  • cleanup: true - should the dump be reloaded after each test

Example

modules:
   enabled: [Db]
   config:
      Db:
         dsn: 'mysql:host=localhost;dbname=testdb'
         user: 'root'
         password: ''
         dump: 'tests/_data/dump.sql'
         populate: true
         cleanup: false

Public Properties

  • dbh - contains PDO connection.
  • driver - contains Connection Driver. See list all available drivers

Actions

dontSeeInDatabase

Effect is opposite to ->seeInDatabase

Checks if there is no record with such column values in database. Provide table name and column values.

Example:

{% highlight php %}

dontSeeInDatabase('users', array('name' => 'Davert', 'email' => '[email protected]')); {% endhighlight %} Will generate: {% highlight yaml %} sql SELECT COUNT(*) FROM `users` WHERE `name` = 'Davert' AND `email` = '[email protected]' {% endhighlight %} Fails if such user was found. * param $table * param array $criteria #### getName __not documented__ #### grabFromDatabase Fetches a single column value from a database. Provide table name, desired column and criteria. Example: {% highlight php %} grabFromDatabase('users', 'email', array('name' => 'Davert')); {% endhighlight %} * version 1.1 * param $table * param $column * param array $criteria * return mixed #### haveInDatabase Inserts SQL record into database. This record will be erased after the test. {% highlight php %} haveInDatabase('users', array('name' => 'miles', 'email' => '[email protected]')); ?>

{% endhighlight %}

  • param $table
  • param array $data
  • return integer $id

seeInDatabase

Checks if a row with given column values exists. Provide table name and column values.

Example:

{% highlight php %}

seeInDatabase('users', array('name' => 'Davert', 'email' => '[email protected]')); {% endhighlight %} Will generate: {% highlight yaml %} sql SELECT COUNT(*) FROM `users` WHERE `name` = 'Davert' AND `email` = '[email protected]' {% endhighlight %} Fails if no such user found. * param $table * param array $criteria
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.