2017 © Pedro Peláez
 

library mockslimclient

web scraper for Slim framework Mock. (for UnitTesting)

image

uzulla/mockslimclient

web scraper for Slim framework Mock. (for UnitTesting)

  • Tuesday, May 27, 2014
  • by uzulla
  • Repository
  • 1 Watchers
  • 3 Stars
  • 445 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 2 % Grown

The README.md

mock_slim_client

Client for a Mock Slim. (for PHPUnit), (*1)

Slim frameworkにはMockがありますが、POSTをエミュレートしたりするには比較的複雑な手順を踏む必要があります。 これでは、自動テストに使いづらいので良い感じにアクセスをシミュレートするようにしました。, (*2)

req()でhtmlを取得できます。また、req_dom()をつかうとphp-html-parserインスタンスを返しますので、別途でパースせずに検証に使いやすいと思います。, (*3)

実際の使い方はexampleと、サンプルコードを見てください。, (*4)

注意

元来SlimのMockはあまりこのような用途を想定していないのか、特にPOST時のデータの扱いが微妙ですが、そこはなんとかソレっぽく動作するようにしてあります。, (*5)

結果として、今後のSlimの設計変更によっては動作しなくなるかも知れません。その場合ISSUEなどで報告をお待ちしております。, (*6)

install

use Composer, (*7)

{
    "require": {
        "uzulla/mockslimclient": "dev-master"
    }
}

example

in PHPUnit test, (*8)

<?php
class myTest extends \PHPUnit\Framework\TestCase
{
    use \Uzulla\MockSlimClient; // use by trait

    // over ride \Uzulla\MockSlimClient::registrationRoute()
    static function registrationRoute($app)
    {
        $app->get('/', function() use ($app){
            //...
        });
        // or \myApp::registrationRoute($app);
    }

    // over ride \Uzulla\MockSlimClient::createSlim()
    static function createSlim()
    {
        return new \Slim\Slim([
            'templates.path' => __DIR__.'/../../sample_app/templates'
        ]);
    }    

    // sample test case.    
    public function testConfirmPost()
    {
        // get http://dummy/ html.
        $raw_html = $this->req('/');

        // get paquettg/php-html-parser instance
        $dom = $this->req_dom('/post/form');
        $this->assertTrue(!!$dom->find('input[name=name]'));

        // get CSRF token.
        $this->req('/');
        $csrf_token = $_SESSION['csrf_token'];

        $test_name = 'testname';
        $test_body = 'testbody';

        // build post data.
        $input = http_build_query([
            'nickname'=>$test_name,
            'body'=>$test_body,
            'csrf_token'=>$csrf_token
        ]);

        // do post.
        $dom = $this->req_dom('/post/confirm', 'POST', $input);
        $this->assertEquals($test_name, $dom->find('div.nickname-preview', 0)->text);
        $this->assertEquals($test_name, $dom->find('input[name=nickname]', 0)->value);
    }
}

use sample code

check sample webapp.

$ cd sample_app
$ php -S localhost:5000

open http://localhost:5000/, (*9)

execute sample phpunit test

$ cd sample_test
$ php ../vendor/bin/phpunit 
PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from /Users/uzulla/dev/mock_slim_client/sample_test/phpunit.xml

..

Time: 91 ms, Memory: 4.50Mb

OK (2 tests, 2 assertions)

license

MIT, (*10)

see also

The Versions

27/05 2014

dev-master

9999999-dev https://github.com/uzulla/mock_slim_client

web scraper for Slim framework Mock. (for UnitTesting)

  Sources   Download

MIT

The Requires

 

The Development Requires

test phpunit php unittest slim

27/05 2014

v1.0.1

1.0.1.0 https://github.com/uzulla/mock_slim_client

web scraper for Slim framework Mock. (for UnitTesting)

  Sources   Download

MIT

The Requires

 

The Development Requires

test phpunit php unittest slim

09/01 2014

v1.0.0

1.0.0.0 https://github.com/uzulla/mock_slim_client

web scraper for Slim framework Mock. (for UnitTesting)

  Sources   Download

MIT

The Requires

 

The Development Requires

test phpunit php unittest slim