Skip to content

Instantly share code, notes, and snippets.

@henriqueutsch
Last active August 29, 2015 14:26
Show Gist options
  • Save henriqueutsch/4b8d424ae9b082ff40be to your computer and use it in GitHub Desktop.
Save henriqueutsch/4b8d424ae9b082ff40be to your computer and use it in GitHub Desktop.
Testes no Windows rodando páginas PHP - Teste com ruby, test/unit e mechanize
<?php
include "browser.php";
date_default_timezone_set('America/Sao_Paulo');
$campos[] = '';
$valores[] = '';
foreach ($_POST as $key => $value) {
if(gettype($value) == 'array')
{
$temp="";
foreach($value as $check) {
$temp = "{$temp},{$check}";
$temp = ltrim ($temp, ',');
}
$value = $temp;
}
// echo $key . ' => ' . utf8_decode($value);
$campos[] = $key;
$valores[] = $value;
// echo "<br>";
}
array_pop($valores);
array_pop($campos);
// print_r($valores);
// echo "<br>";
// print_r($campos);
// echo "<br>";
$campos2 = implode(",", $campos);
$campos2 = ltrim ($campos2, ',');
$valores2 = implode("','", $valores);
$valores2 = ltrim ($valores2, "',");
echo "Email enviado";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mechanize</title>
</head>
<body>
<form action="envia.php" method="POST">
<label for="">Nome:</label><br>
<input type="text" name="nome"><br>
<label for="">Telefone:</label><br>
<input type="text" name="telefone"><br>
<label for="">E-mail:</label><br>
<input type="text" name="email"><br>
<label for="">Mensagem:</label><br>
<input type="text" name="mensagem"><br>
<input type="submit" value="ENVIAR">
</form>
</body>
</html>
Ruby 2.1.6
gem install mechanize
Rodar direto no sublime text
https://github.com/ehamiter/ST2-Color-Console
https://github.com/maltize/sublime-text-2-ruby-tests
require 'mechanize'
require 'Test/Unit'
class LoginTest < Test::Unit::TestCase
def test_login
mechanize = Mechanize.new
page = mechanize.get('http://127.0.0.1/mechanize/index.php')
form = page.forms.first
form['nome'] = 'Henrique Utsch Test'
form['telefone'] = '3111111111'
form['email'] = '[email protected]'
# msg = LoremIpsum.lorem_ipsum(paragraphs: 4)
msg = 'teste'
form['mensagem'] = msg
page = form.submit
assert_equal 'Email enviado', page.content
# p page.content
end
end
@Zizaco
Copy link

Zizaco commented Jul 30, 2015

Outra opção:

<?php
use Laracasts\Integrated\Extensions\Goutte as IntegrationTest;

class ExampleTest extends IntegrationTest {

    protected $baseUrl = 'http://127.0.0.1';

    public function testLogin()
    {
        $this->visit('mechanize/index.php')
            ->submitForm('ENVIAR', [
                'nome' => 'Henrique Utsch Test',
                'telefone' => '3111111111',
                'email' => '[email protected]'
                'mensagem' => 'teste'
            ])
            ->andSee('Email enviado')
            ->onPage('mechanize/envia.php');
    }
}

https://laracasts.com/series/intuitive-integration-testing


Para rodar no windows:

http://stackoverflow.com/questions/25549177/change-composer-global-path-windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment