:heavy_check_mark: PHP Test Framework for Freedom, Truth, and Justice

Overview

Kahlan


Build Status Build Status License

Latest Stable Version Total Downloads Code Climate Coverage Status Coveralls Coverage Status Scrutinizer Coverage Status

Kahlan is a full-featured Unit & BDD test framework a la RSpec/JSpec which uses a describe-it syntax and moves testing in PHP one step forward.

Kahlan lets you stub or monkey patch your code directly like in Ruby or JavaScript without any required PECL-extensions.

Videos

IRC

chat.freenode.net (server) #kahlan (channel)

Documentation

See the full documentation here

Requirements

  • PHP 7.1+
  • Composer
  • phpdbg or Xdebug (only required for code coverage analysis)

Main Features

  • RSpec/JSpec syntax
  • Code Coverage metrics (xdebug or phpdbg required)
  • Handy stubbing system (mockery or prophecy are no longer needed)
  • Set stubs on your class methods directly (i.e allows dynamic mocking)
  • Ability to Monkey Patch your code (i.e. allows replacement of core functions/classes on the fly)
  • Check called methods on your classes/instances
  • Built-in Reporters (Terminal or HTML reporting through istanbul or lcov)
  • Built-in Exporters (Coveralls, Code Climate, Scrutinizer, Clover)
  • Extensible, customizable workflow

Syntax

<?php

describe("Example", function() {

    it("makes an expectation", function() {

         expect(true)->toBe(true);

    });

    it("expects methods to be called", function() {

        $user = new User();
        expect($user)->toReceive('save')->with(['validates' => false]);
        $user->save(['validates' => false]);

    });

    it("stubs a function", function() {

        allow('time')->toBeCalled()->andReturn(123);
        $user = new User();
        expect($user->save())->toBe(true)
        expect($user->created)->toBe(123);

    });

    it("stubs a class", function() {

        allow('PDO')->toReceive('prepare', 'fetchAll')->andReturn([['name' => 'bob']]);
        $user = new User();
        expect($user->all())->toBe([['name' => 'bob']]);

    });

});

Screenshots

Example of default reporting:

dot_reporter

Example of verbose reporting:

verbose_reporter

Example of code coverage on a specific scope:

code_coverage

Installation

via Composer

$ composer require --dev kahlan/kahlan

Note: Kahlan uses the Semantic Versioning and maintains a CHANGELOG to help you easily understand what's happening.

via Git clone

git clone git://github.com/kahlan/kahlan.git
cd kahlan
composer install
bin/kahlan              # to run specs or,
bin/kahlan --coverage=4 # to run specs with coverage info for namespaces, classes & methods (require xdebug)
Comments
  • Breakpoint; debugging tests

    Breakpoint; debugging tests

    Hi,

    Thanks for Kahlan brilliant project, however I have problems when it comes to debugging code I've written, breakpoints won't trigger in files outside of the file where the test is defined

    I understand that kahlan recreates code referenced in tests due to it's implementation etc and thats why the breakpoints aren't reached (because that file isn't actually being run)

    Is there anyway around this or anything I can do in my IDE so that breakpoints work whilst tests are being run?

    I'm using vscode

    opened by kale1d0code 1
  • Feature Request: Support focusing a specific test or suite with command line arguments

    Feature Request: Support focusing a specific test or suite with command line arguments

    It would be nice to be able to focus a single test or suite of test via command line arguments, rather then just having the fit(...), or fdescribe(...) notation. This feature exists in other BDD frameworks like Jest, and I think Pest, and could give us the ability to more easily create plugins for editors.

    feature request 
    opened by AlmightyTritan 0
  • Feature request: exclude patching by directory, not namespace

    Feature request: exclude patching by directory, not namespace

    Currently I can exclude namespaces to patch via:

    $this->commandLine()->set('exclude', [
        'com\example\someapp\etc'
    ]);
    

    However what I want to do is exclude the vendor directory entirely. I even think that's probably a more common use-case requirement for excluding? Dunno.

    A similar argument could be made for the include option, I think.

    It's not a big problem for me in the bigger scheme of things, as I can just include my own namespace, and that "should" work.

    feature request 
    opened by adamcameron 0
  • Feature request: improve skipping

    Feature request: improve skipping

    I'm porting my tests from PHPUnit. Today I've been looking at dealing with incomplete (markTestIncomplete) and skipped (markTestSkipped) tests.

    One can kinda mark a test incomplete at compile-time by using xdescribe and xit, but it would be good to have a runtime way of doing it as well, similar to PHPUnit. That said I think 95% of the time handling this at compile time is fine, so low impact.

    I note there is a skipIf method, but was surprised to see its implementation was void function skipIf(boolean condition), where I might have expected void function skipIf(string message, callback condition) to match the implementations of it and describe, and using the same analogy. At the very least void function skipIf(boolean condition[, string message]).

    I think a message is reasonably handy when skipping tests.

    I checked to see if there was a precedent in other similar frameworks:

    • Jasmine doesn't seem to have skip yet, based on https://github.com/jasmine/jasmine/issues/1709;
    • however Mocha does (https://mochajs.org/#inclusive-tests), simply as skip().
    • rspec handles skipping via in-code tags: https://relishapp.com/rspec/rspec-core/v/3-4/docs/pending-and-skipped-examples/skip-examples, with CLI options: https://relishapp.com/rspec/rspec-core/v/3-4/docs/command-line/tag-option
    feature request 
    opened by adamcameron 2
  • PHPStan configuration

    PHPStan configuration

    It would be nice to have a drop-in config for Kahlan for PHPStan. Currently, when run over a Kahlan project, PHPStan warns about:

    • The DSL functions not existing.
    • The $this keyword used in kahlan-config.php outside of a class context.

    Here's my current config for handling the first point:

    parameters:
      ignoreErrors:
        - /^Function context not found.$/
        - /^Function describe not found.$/
        - /^Function expect not found.$/
        - /^Function it not found.$/
    

    But there's probably a better way to fix the problem that doesn't involve "ignoring" these errors. If PHPStan can be configured to understand the functions it could actually pick up on real errors when using them. Are the DSL functions dynamically defined? That would explain why PHPStan complains.

    PHPStan uses Neon config files which allow you to include a definition from a vendor, which means that if you created a kahlan/phpstan-kahlan repo, users could require it, then add something like this to their PHPStan configuration:

    includes:
      - vendor/kahlan/phpstan-kahlan/kahlan.neon
    

    I just wrote a PHPStan integration for Phony, which is probably more complex than what's needed for Kahlan, but you can check it out here anyway.

    feature request 
    opened by ezzatron 7
Releases(5.2.2)
Owner
Kahlan
PHP Test Framework for Freedom, Truth, and Justice
Kahlan
Event driven BDD test framework for PHP

The highly extensible, highly enjoyable, PHP testing framework. Read more at peridot-php.github.io or head over to the wiki. Building PHAR Peridot's p

Peridot 327 Jan 5, 2023
BDD test framework for PHP

BDD test framework for PHP, inspired by Jasmine and RSpec. Features a familiar syntax, and a watch command to automatically re-run specs during develo

Daniel St. Jules 286 Nov 12, 2022
vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

null 1.4k Dec 23, 2022
The Phoronix Test Suite is the most comprehensive testing and benchmarking platform

The Phoronix Test Suite is the most comprehensive testing and benchmarking platform available for Linux, Solaris, macOS, Windows, and BSD operating systems.

Phoronix Test Suite 1.9k Jan 7, 2023
A Composer script to run a 'test' or 'spec' Composer script against multiple PHP versions.

composer-multitest composer-multitest is a Composer script that runs a test or spec Composer script against multiple PHP versions managed by PHPBrew o

Raphael Stolt 5 Aug 27, 2019
PHP Test Generator - A CLI tool which generates unit tests

This project make usages of PHPStan and PHPParser to generate test cases for a given PHP File.

Alexander Schranz 7 Dec 3, 2022
⛽ Pest plugin to test Laravel applications powered by Octane.

⛽ Laravel Octane (Pest Plugin) Pest plugin to test Laravel applications powered by Octane. Install Via Composer composer require --dev cerbero/pest-pl

Andrea Marco Sartori 21 Apr 5, 2022
Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slowness of constantly restarting the testing environment.

Magic Test for Laravel Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slownes

null 400 Jan 5, 2023
A sample RESTful API in Laravel with PHPunit test.

Laravel PHP Framework URL | URI | Action |

Fasil 9 Jul 11, 2020
PHPUnit Application Architecture Test

PHPUnit Application Architecture Test Idea: write architecture tests as well as feature and unit tests Installation Install via composer composer requ

null 19 Dec 11, 2022
Patchstack Test task Laravel&Vue CRUD

Patchstack Test Task - Laravel & Vue CRUD SPA Written with Laravel and Vue2 using mix. Installation Clone this repository Run "composer update" comman

Crypto Rookie 3 Aug 25, 2022
Prevent none-test output in your Pest tests.

Pest Plugin Silence Often, when writing tests, we echo and dump test code to debug and check everything is working correctly. It can be easy to forget

Worksome 5 Feb 23, 2022
Test requests in Laravel without all the boilerplate.

Request Factories Test requests in Laravel without all the boilerplate. ?? Psst. Although our examples use Pest PHP, this works just as well in PHPUni

Worksome 391 Jan 1, 2023
The modern, simple and intuitive PHP unit testing framework.

atoum PHP version atoum version 5.3 -> 5.6 1.x -> 3.x 7.2 -> 8.x 4.x (current) A simple, modern and intuitive unit testing framework for PHP! Just lik

atoum 1.4k Nov 29, 2022
PHP unit testing framework with built in mocks and stubs. Runs in the browser, or via the command line.

Enhance PHP A unit testing framework with mocks and stubs. Built for PHP, in PHP! Quick Start: Just add EnhanceTestFramework.php and you are ready to

Enhance PHP 67 Sep 12, 2022
SimpleTest is a framework for unit testing, web site testing and mock objects for PHP

SimpleTest SimpleTest is a framework for unit testing, web site testing and mock objects for PHP. Installation Downloads All downloads are stored on G

SimpleTest 147 Jun 20, 2022
The most powerful and flexible mocking framework for PHPUnit / Codeception.

AspectMock AspectMock is not an ordinary PHP mocking framework. With the power of Aspect Oriented programming and the awesome Go-AOP library, AspectMo

Codeception Testing Framework 777 Dec 12, 2022
Full-stack testing PHP framework

Codeception Modern PHP Testing for everyone Codeception is a modern full-stack testing framework for PHP. Inspired by BDD, it provides an absolutely n

Codeception Testing Framework 4.6k Jan 7, 2023
AST based PHP Mutation Testing Framework

Infection - Mutation Testing framework Please read documentation here: infection.github.io Twitter: @infection_php Discord: https://discord.gg/ZUmyHTJ

Infection - Mutation Testing Framework for PHP 1.8k Jan 2, 2023