$30 off During Our Annual Pro Sale. View Details »

PHPT

 PHPT

Criando testes para a linguagem PHP com .phpt

Ivan Rosolen

August 31, 2012
Tweet

More Decks by Ivan Rosolen

Other Decks in Technology

Transcript

  1. PHPT
    PHPMS Conf 2012

    View Slide

  2. IVAN ROSOLEN
    @ivanrosolen
    http://about.me/ivanrosolen
    Co-fundador do PHPSP
    Equipe de inovação da ARIZONA
    Desenvolvedor 10+ anos

    View Slide

  3. PHPT
    • Apenas um teste por aquivo .phpt
    • Testes executados em processos separados
    • Diferentes configurações do PHP para cada teste

    View Slide

  4. • Bugs - bug.phpt
    • Funções
    Comportamento básico - _basic.phpt
    Erro - _error.phpt
    Variações - _variation.phpt
    • Extensões - .phpt
    Nomenclatura

    View Slide

  5. --TEST--
    Uma linha para descrever o teste
    --FILE--
    código PHP (isso mesmo PHP!)
    --EXPECT--
    Resultado esperado (acerto ou erro)
    Seções Obrigatórias

    View Slide

  6. --TEST--
    [--DESCRIPTION--]
    [--CREDITS--]
    [--SKIPIF--]
    [--REQUEST--]
    [--POST-- | --PUT-- | --POST_RAW-- | --GZIP_POST-- | --DEFLATE_POST-- | --GET--]
    [--COOKIE--]
    [--STDIN--]
    [--INI--]
    [--ARGS--]
    [--ENV--]
    --FILE-- | --FILEEOF-- | --FILE_EXTERNAL-- | --REDIRECTTEST--
    [--HEADERS--]
    [--CGI--]
    [--XFAIL--]
    [--EXPECTHEADERS--]
    --EXPECT-- | --EXPECTF-- | --EXPECTREGEX--
    [--CLEAN--]

    View Slide

  7. --EXPECTF--
    %s string
    %d number
    %c single char
    %w whitespace
    %i integer
    %f floats
    %x hexadecimal
    %e directory sep.

    View Slide

  8. Baixar o source da versão do PHP desejada
    ./configure (com parâmetros necessários)
    make
    Como Fazer
    make test
    tar -xvf php-5.4.5.tar.gz

    View Slide

  9. make test TESTS=diretorio/
    Testando
    make test TESTS=diretorio/*.phpt
    make test TESTS=diretorio/nome.phpt
    Teoricamente .zip do PHP win roda os testes com o php.exe
    nmake test

    View Slide

  10. Exemplos

    View Slide

  11. --TEST--
    Xuplau
    --FILE--

    --EXPECT--
    Xuplau

    View Slide

  12. --TEST--
    Xuplau Error
    --FILE--
    --EXPECTF--
    Parse error: syntax error, unexpected '%s' (T_ENCAPSED_AND_WHITESPACE) in %s.php on line %d

    View Slide

  13. --TEST--
    Xuplau Error
    --FILE--
    --EXPECTREGEX--
    Parse error: (syntax|parse) error, unexpected .* \(T_ENCAPSED_AND_WHITESPACE\) in .* on line \d

    View Slide

  14. --TEST--
    Apagar TMP Files
    --FILE--
    $tmp = dirname(__FILE__) . '/foo.tmp';
    $fp = fopen($tmp, 'w');
    fwrite($fp, 'bar');
    fclose($fp);
    ?>
    --CLEAN--
    $tmp = dirname(__FILE__) . '/foo.tmp';
    unlink($tmp)
    ?>
    --EXPECT--

    View Slide

  15. --TEST--
    Skipif teste!
    --SKIPIF--
    if ( substr(PHP_OS, 0, 3 ) != 'WIN') die('Teste apenas para Unix');
    ?>
    --FILE--
    --EXPECT--

    View Slide

  16. --TEST--
    Testing imagerectangle() of GD library
    --CREDITS--
    Ivan Rosolen
    #testfest PHPSP on 2009-06-30
    --SKIPIF--
    if ( ! extension_loaded('gd') ) die( 'skip GD not present; skipping test' );
    ?>
    --FILE--
    // Create a image
    $image = imagecreatetruecolor( 100, 100 );
    // Draw a rectangle
    imagerectangle( $image, 0, 0, 50, 50, imagecolorallocate($image, 255, 255, 255) );
    ob_start();
    imagepng( $image, null, 9 );
    $img = ob_get_contents();
    ob_end_clean();
    echo md5(base64_encode($img));
    ?>
    --EXPECT--
    282aaecfdd50091821d63093d9bb1bb9

    View Slide

  17. --TEST--
    Testing wrong param passing imagefilledellipse() of GD library
    --CREDITS--
    Ivan Rosolen
    #phpsptestfest PHPSP on 2010-05-29
    --SKIPIF--
    if (!extension_loaded("gd")) die("skip GD not present");
    ?>
    --FILE--
    // Create a image
    $image = imagecreatetruecolor(400, 300);
    // choose a color for the ellipse
    $ellipse = imagecolorallocate($image, 255, 255, 255);
    // draw the white ellipse
    imagefilledellipse( 'wrong param', 200, 150, 300, 200, $ellipse);
    ?>
    --EXPECTF--
    Warning: imagefilledellipse() expects parameter 1 to be resource, %s given in %s on line %d

    View Slide

  18. *.exp
    *.log
    *.out
    *.php
    *.phpt
    *.sh
    Deu erro!
    *.diff

    View Slide

  19. Que Teste eu Faço?
    http://gcov.php.net

    View Slide

  20. PHPT +
    • Suas extensões
    • Seus Projetos
    https://github.com/vrana/notorm

    View Slide

  21. • Clonar git
    https://github.com/php/php-src
    • Pull Request
    • Avisar na lista do QA
    Quality Assurance list - [email protected]
    Enviando Testes

    View Slide

  22. http://gcov.php.net
    http://qa.php.net/write-test.php
    Links
    https://wiki.php.net/qa/testfest-2010/windows-test-environment
    Para Windows ( não testei muito ainda )
    http://pastebin.com/PPBr05kQ
    http://pastebin.com/CxFtXUwS
    http://php.net/mailing-lists.php

    View Slide

  23. Dúvidas?

    View Slide