Esempio n. 1
0
function test_test_before_each_assert_method()
{
    global $val;
    assert_null($val);
    $val = 400;
    assert_equal(&$val, 10, '$val should be 10, not 400');
}
Esempio n. 2
0
   function test_http_ua_accepts()
   {
     $env = env();

     $env['SERVER']['HTTP_ACCEPT'] = null;
     assert_true(http_ua_accepts('text/plain'));

     $env['SERVER']['HTTP_ACCEPT'] = 'text/html';
     assert_true(http_ua_accepts('html'));

     $env['SERVER']['HTTP_ACCEPT'] = 'text/*; application/json';     
     assert_true(http_ua_accepts('html'));
     assert_true(http_ua_accepts('text/html'));
     assert_true(http_ua_accepts('text/plain'));
     assert_true(http_ua_accepts('application/json'));
     
     assert_false(http_ua_accepts('image/png'));
     assert_false(http_ua_accepts('png'));
     
     assert_true(defined('TESTS_DOC_ROOT'), "Undefined 'TESTS_DOC_ROOT' constant");
     
     $response =  test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: image/png"));
     assert_equal("Oops", $response);
     
     $response =  test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: text/html"));
     assert_equal("<h1>HTML</h1>", $response);
     
     $response =  test_request(TESTS_DOC_ROOT.'05-content_negociation.php', 'GET', false, array(), array("Accept: application/json"));
     assert_equal("json", $response);
   }
Esempio n. 3
0
function test_output_render()
{
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render0', 'GET');
    assert_equal($response, "Lorem ipsum dolor sit amet.");
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render1', 'GET');
    assert_equal($response, "Lorem % ipsum dolor sit amet.");
}
Esempio n. 4
0
 function test_array_path_to_name()
 {
     $expect = array('foo' => 'foo', 'foo.bar' => 'foo[bar]', 'foo.bar.baz' => 'foo[bar][baz]');
     foreach ($expect as $in => $out) {
         assert_equal($out, array_path_to_name($in));
     }
 }
Esempio n. 5
0
function assert_no_difference($expression, $lambda)
{
    $expression = 'return ' . $expression . ';';
    $value = eval($expression);
    $lambda();
    assert_equal($value, eval($expression));
}
Esempio n. 6
0
 public function test_interface_delegation()
 {
     parse_and_eval(dirname(__FILE__) . '/interface_delegation_test_interface.phpx');
     parse_and_eval(dirname(__FILE__) . '/interface_delegation_test_class.phpx');
     $a = new EIDTC_Addressable();
     assert_equal("message sent to foo@bar.com", $a->send_to("*****@*****.**"));
 }
Esempio n. 7
0
 public function test_auto_quote_array()
 {
     $out = $this->db->auto_quote_array(array('s:s1' => 'foobar', 'str:s2' => 'bar', 'string:s3' => 'moose', 'b:b1' => true, 'bool:b2' => false, 'boolean:b3' => true, 'f:f1' => 1.23, 'float:f2' => 2.46, 'i:i1' => 100, 'int:i2' => 120, 'integer:i3' => 140, 'x:x1' => 'sadasd', 'binary:x2' => '1315ft4g'));
     $expect = array('s1' => $this->db->quote_string('foobar'), 's2' => $this->db->quote_string('bar'), 's3' => $this->db->quote_string('moose'), 'b1' => $this->db->quote_boolean(true), 'b2' => $this->db->quote_boolean(false), 'b3' => $this->db->quote_boolean(true), 'f1' => $this->db->quote_float(1.23), 'f2' => $this->db->quote_float(2.46), 'i1' => $this->db->quote_integer(100), 'i2' => $this->db->quote_integer(120), 'i3' => $this->db->quote_integer(140), 'x1' => $this->db->quote_binary('sadasd'), 'x2' => $this->db->quote_binary('1315ft4g'));
     foreach ($expect as $k => $v) {
         assert_equal($v, $out[$k]);
     }
 }
Esempio n. 8
0
 public function test_mixins()
 {
     parse_and_eval(dirname(__FILE__) . '/mixin_test_mixins.phpx');
     parse_and_eval(dirname(__FILE__) . '/mixin_test_classes.phpx');
     $thing = new EMT_Foo();
     assert_equal('foo', $thing->foo());
     assert_equal('bar', EMT_Foo::bar());
     assert_equal('baz', EMT_Foo::BAZ);
 }
Esempio n. 9
0
 public function test_annotations()
 {
     parse_and_eval(dirname(__FILE__) . '/annotation_test_class.phpx');
     assert_equal(array('model' => true), phpx\annotations_for('ExamplesAnnotationTestClass'));
     assert_equal(array('model' => true), phpx\annotations_for(new ExamplesAnnotationTestClass()));
     assert_equal(array('foo' => 'bar'), phpx\annotations_for('ExamplesAnnotationTestClass', '$baz'));
     assert_equal(array('foo' => 'bar'), phpx\annotations_for(new ExamplesAnnotationTestClass(), '$baz'));
     assert_equal(array('access' => array('admin', 'public')), phpx\annotations_for('ExamplesAnnotationTestClass', 'find_all'));
     assert_equal(array('access' => array('admin', 'public')), phpx\annotations_for(new ExamplesAnnotationTestClass(), 'find_all'));
 }
Esempio n. 10
0
function test_test_before_each_assert_method()
{
    global $val;
    assert_null($val);
    $val = 400;
    assert_true(true);
    // run before_each_assert_in_test() first
    // so now
    assert_equal($val, 10, '$val should be 10, not 400');
}
Esempio n. 11
0
function test_file_path()
{
    $p = "/one/two/three";
    assert_equal(file_path('/one', 'two', 'three'), $p);
    assert_equal(file_path('/one', '/two', 'three'), $p);
    assert_equal(file_path('/one', 'two', '///three'), $p);
    assert_equal(file_path('/one', 'two', 'three/'), $p . '/');
    assert_equal(file_path('/one', 'two', 'three//'), $p . '/');
    assert_equal(file_path('/one', '\\two', '\\three//'), $p . '/');
}
Esempio n. 12
0
 public function test_mixins()
 {
     phpx\register_macro('ecet_greet', 'ECET_GreetMacro');
     parse_and_eval(dirname(__FILE__) . '/class_eval_test_class.phpx');
     assert_equal(15, ExamplesClassEvalTestClass::add(10, 5));
     $foo = new ExamplesClassEvalTestClass();
     $foo->set_foo('hello world');
     assert_equal('HELLO WORLD', $foo->get_foo());
     assert_equal('Hello Jason', $foo->greet('Jason'));
 }
Esempio n. 13
0
function test_http_redirect()
{
    $url = TESTS_DOC_ROOT . '09-redirect.php?/';
    $response = test_request($url, 'GET');
    assert_equal($response, '/redirected');
    $response = test_request($url . '&key1=value1', 'GET');
    assert_equal($response, '/redirected&key1=value1');
    $response = test_request($url . '&key1=value1&key2=value2', 'GET');
    assert_equal($response, '/redirected&key1=value1&key2=value2');
}
Esempio n. 14
0
 public function test_equal()
 {
     assert_equal(1, 1);
     assert_equal(1, "1");
     assert_fails(function () {
         assert_equal(1, 2);
     });
     assert_fails(function () {
         assert_equal(1, "2");
     });
 }
Esempio n. 15
0
 public function test_mixins()
 {
     parse_and_eval(dirname(__FILE__) . '/hierarchy_test_1.phpx');
     parse_and_eval(dirname(__FILE__) . '/hierarchy_test_2.phpx');
     parse_and_eval(dirname(__FILE__) . '/hierarchy_test_3.phpx');
     $class_def = \phpx\Library::get_class_definition('\\HHT3');
     $hierarchy = $class_def->get_class_hierarchy();
     assert_equal(3, count($hierarchy));
     assert_equal('\\HHT1', $hierarchy[0]->get_qualified_name());
     assert_equal('\\HHT2', $hierarchy[1]->get_qualified_name());
     assert_equal('\\HHT3', $hierarchy[2]->get_qualified_name());
 }
Esempio n. 16
0
 public function test_auto_registration()
 {
     phpx\Macro::register('TestMacro2');
     $def = new phpx\ClassDef('MacroTestAutoRegClass');
     $def->test_macro_1('foo', 'bar');
     $def->test_macro_1('baz', 'bleem');
     $def->finalise();
     eval($def->to_php());
     $instance = new MacroTestAutoRegClass();
     assert_equal('bar', $instance->foo());
     assert_equal('bleem', $instance->baz());
 }
Esempio n. 17
0
 public function test_pattern_matching()
 {
     parse_and_eval(dirname(__FILE__) . '/pattern_matching_test_class.phpx');
     $i = new ExamplesPatternMatchingTestClass();
     assert_equal('foo:render', $i->render_template('foo'));
     assert_equal('bar:display', $i->display_template('bar'));
     assert_equal('people', $i->find_people());
     assert_equal('managers', $i->find_managers());
     assert_equal('all:1', ExamplesPatternMatchingTestClass::find_all(1));
     assert_equal('one:2', ExamplesPatternMatchingTestClass::find_one(2));
     assert_equal('foo', ExamplesPatternMatchingTestClass::foo());
     assert_equal('bar', ExamplesPatternMatchingTestClass::bar());
 }
Esempio n. 18
0
function test_request_uri()
{
    # TODO test with webbrick + CGIHandler (http://microjet.ath.cx/webrickguide/html/CGIHandler.html)
    # TODO request_uri must be also tested in a browser...
    assert_equal(request_uri(), "/");
    $path = dirname(__FILE__) . "/helpers/show_request_uri.php";
    $cmd = "php -f {$path}";
    assert_equal(exec($cmd, $res), "/");
    assert_equal(exec($cmd . " test", $res), "/test");
    assert_equal(exec($cmd . " /test", $res), "/test");
    assert_equal(exec($cmd . " /my-test/", $res), "/my-test");
    assert_not_equal(exec($cmd . " /my-test/?", $res), "/my-test");
    assert_not_equal(exec($cmd . " /my-test?var=1", $res), "/my-test");
}
Esempio n. 19
0
function test_output_render()
{
    $lorem = "Lorem ipsum dolor sit amet.";
    $q_lorem = preg_quote($lorem);
    # Testing standard rendering with sprint string
    assert_equal(render($lorem), $lorem);
    assert_equal(render($lorem, null, array('unused')), $lorem);
    assert_equal(render("Lorem %s dolor sit amet.", null, array('ipsum')), $lorem);
    assert_equal(render("Lorem %s dolor sit amet.", null, array('var1' => 'ipsum')), $lorem);
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render0', 'GET');
    assert_equal($response, $lorem);
    $response = test_request(URL_FOR_OUTPUT_TEST . '/render1', 'GET');
    assert_equal($response, $lorem);
    # Testing rendering with a view (inline function case)
    $view = '_test_output_html_hello_world';
    $html = render($view);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, null, array($lorem));
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, null, array('lorem' => $lorem));
    assert_match("/{$q_lorem}/", $html);
    # Testing layout option
    $layout = '_test_output_html_my_layout';
    $html = render($lorem, $layout);
    assert_match("/{$q_lorem}/", $html);
    assert_match("/<title>Page title<\\/title>/", $html);
    # Testing layout + view (inline function case)
    $html = render($view, $layout);
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, $layout, array('lorem' => $lorem));
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_match("/{$q_lorem}/", $html);
    # Testing layout + view (template files case)
    $views_dir = dirname(__FILE__) . '/apps/views/';
    option('views_dir', $views_dir);
    $view = 'hello_world.html.php';
    $layout = 'layouts/default.html.php';
    $html = render($view, $layout);
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_no_match("/{$q_lorem}/", $html);
    $html = render($view, $layout, array('lorem' => $lorem));
    assert_match("/<title>Page title<\\/title>/", $html);
    assert_match("/Hello World/", $html);
    assert_match("/{$q_lorem}/", $html);
}
Esempio n. 20
0
 public function test_forward_declarations()
 {
     phpx\Forward::before('ExamplesForwardTestClass', function ($class) {
         $class->define_public_instance_method('forward_1', 'return "forward_1";');
     });
     phpx\Forward::before('ExamplesForwardTestClass')->define_public_instance_method('forward_2', 'return "forward_2";');
     phpx\Forward::after('ExamplesForwardTestClass', function ($class) {
         $class->define_public_instance_method('after_1', 'return "after_1";');
     });
     phpx\Forward::after('ExamplesForwardTestClass')->define_public_instance_method('after_2', 'return "after_2";');
     parse_and_eval(dirname(__FILE__) . '/forward_test_class.phpx');
     $item = new ExamplesForwardTestClass();
     assert_equal('forward_1', $item->forward_1());
     assert_equal('forward_2', $item->forward_2());
     assert_equal('after_1', $item->after_1());
     assert_equal('after_2', $item->after_2());
 }
Esempio n. 21
0
    public function test_annotation_parsing()
    {
        $string = <<<ANNOTATION
    /**
     * Implicit true:
     * :super_user
     *
     * :count = 100
     * :extensions = [1,2,3]
     * :extensions[] = 4
     * :access = { "jason": "allow", "captain hook": "deny" }
     */
ANNOTATION;
        $parser = new phpx\AnnotationParser();
        $annotes = $parser->parse($string);
        ensure($annotes['super_user']);
        assert_equal(100, $annotes['count']);
        assert_equal(array(1, 2, 3, 4), $annotes['extensions']);
        assert_equal(array('jason' => 'allow', 'captain hook' => 'deny'), $annotes['access']);
    }
Esempio n. 22
0
 public function test_mixins()
 {
     parse_and_eval(dirname(__FILE__) . '/inheritable_attribute_test_1.phpx');
     parse_and_eval(dirname(__FILE__) . '/inheritable_attribute_test_2.phpx');
     parse_and_eval(dirname(__FILE__) . '/inheritable_attribute_test_3.phpx');
     $i1 = new IAT1Test1();
     assert_equal(1, $i1->get_static('a'));
     assert_equal(1, $i1->get_static('b'));
     assert_equal(1, $i1->get_static('c'));
     assert_equal(array('a' => 1, 'b' => 1, 'c' => 1), $i1->get_static('d'));
     $i2 = new IAT1Test2();
     assert_equal(1, $i2->get_static('a'));
     assert_equal(2, $i2->get_static('b'));
     assert_equal(2, $i2->get_static('c'));
     assert_equal(array('a' => 1, 'b' => 2, 'c' => 2), $i2->get_static('d'));
     $i3 = new IAT1Test3();
     assert_equal(1, $i3->get_static('a'));
     assert_equal(2, $i3->get_static('b'));
     assert_equal(3, $i3->get_static('c'));
     assert_equal(array('a' => 1, 'b' => 2, 'c' => 3), $i3->get_static('d'));
 }
Esempio n. 23
0
function assert_select($html, $selector, $opts = array())
{
    $selected_html = select_elements($selector, $html);
    if ($opts["count"]) {
        assert_equal(true, count($selected_html) === $opts['count'], "Failed to match count");
    }
    if ($opts["minimum"]) {
        assert_equal(true, count($selected_html) >= $opts['minimum'], "Failed minimum count");
    }
    if ($opts["maximum"]) {
        assert_equal(true, count($selected_html) <= $opts['maximum'], "Failed maximum count");
    }
    if ($opts["text"]) {
        $text_found = false;
        foreach ($selected_html as $element) {
            if ($element["text"] === $opts["text"]) {
                $text_found = true;
                assert_equal($element["text"], $opts["text"], "Failed to match text");
                break;
            }
        }
        if ($text_found === false) {
            fail("Failed to match text");
        }
    }
    if ($opts["match"]) {
        $text_match_found = false;
        foreach ($selected_html as $element) {
            if (preg_match($opts["match"], $element["text"])) {
                $text_match_found = true;
                assert_equal(true, preg_match($opts["match"], $element["text"]), "Failed to match regular expression");
                break;
            }
        }
        if ($text_match_found === false) {
            fail("Failed to match regular expression");
        }
    }
}
Esempio n. 24
0
function test_router_find_route()
{
    route("get", "/index", "my_index_func");
    route("get", "/new", "my_new_func");
    route("post", "/create", "my_create_func");
    route("get", "/edit/:id", "my_edit_func");
    route("put", "/update/:id", "my_update_func");
    route("delete", "/delete/:id", "my_delete_func");
    route("get", "^/list/(\\d+)", "my_list_func");
    $routes = route("get", "/*.jpg/:size", "my_jpeg");
    assert_length_of($routes, 8);
    $r = route_find("GET", "/unkown");
    assert_false($r);
    $r = route_find("GET", "/delete");
    assert_false($r);
    $r = route_find("POST", "/create");
    assert_equal($r["function"], "my_create_func");
    $r = route_find("GET", "/edit");
    assert_equal($r["function"], "my_edit_func");
    $r = route_find("GET", "/edit/120");
    assert_equal($r["function"], "my_edit_func");
    assert_equal($r["params"]["id"], 120);
    $r = route_find("GET", "/limonade.jpg/thumb", 'my_jpeg');
    assert_equal($r["function"], "my_jpeg");
    assert_equal($r["params"][0], "limonade");
    assert_equal($r["params"]["size"], "thumb");
    route("get", "/index/*", "my_index_func2");
    $routes = route("delete", "/delete/:id/:confirm", "my_delete_func2");
    assert_length_of($routes, 10);
    $r = route_find("GET", "/index");
    assert_equal($r["function"], "my_index_func");
    $r = route_find("GET", "/index/ok");
    assert_equal($r["function"], "my_index_func2");
    $r = route_find("DELETE", "/delete");
    assert_equal($r["function"], "my_delete_func");
    $r = route_find("DELETE", "/delete/120");
    assert_equal($r["function"], "my_delete_func");
    $r = route_find("DELETE", "/delete/120/ok");
    assert_equal($r["function"], "my_delete_func2");
    $r = route_find("GET", "/list/120");
    assert_equal($r["function"], "my_list_func");
    /* testing parameterized functions */
    $extra_p = array(123, 'id' => 123, 'name' => 'abc');
    route("get", "/no/cat/:id", "my_p_func");
    route("get", "/with/cat/:id", "my_p_func", array('params' => $extra_p));
    $routes = route("get", "/indexed/cat/*", "my_p_func", array('params' => $extra_p));
    $r = route_find("GET", "/no/cat/21");
    assert_equal($r["function"], "my_p_func");
    assert_equal($r["params"]["id"], 21);
    $r = route_find("GET", "/with/cat/21");
    assert_equal($r["function"], "my_p_func");
    assert_equal($r["params"]["id"], 21);
    assert_equal($r["params"]["name"], "abc");
    $r = route_find("GET", "/indexed/cat/21");
    assert_equal($r["function"], "my_p_func");
    assert_equal($r["params"][0], 21);
    assert_equal($r["params"]["id"], 123);
    assert_equal($r["params"]["name"], "abc");
    /* testing route with special characters */
    route("get", "/mañana/:when", "my_special_func");
    $r = route_find("GET", "/mañana/123");
    assert_equal($r["function"], "my_special_func");
    assert_equal($r["params"]["when"], 123);
    $r = route_find("GET", "/mañana/après demain");
    assert_equal($r["function"], "my_special_func");
    assert_equal($r["params"]["when"], "après demain");
}
Esempio n. 25
0
 function test_get()
 {
     assert_matches('#google#', $this->response);
     assert_equal(200, $this->response->headers['Status-Code']);
 }
Esempio n. 26
0
/**
 * Wraps the common pattern of having a map of input => expected output that you
 * wish to check against some function.
 * 
 * @param $data_array map of input value => expected output
 * @param $lambda each input value will be passed to this function and compared
 *        against expected output.
 */
function assert_output($data_array, $lambda)
{
    foreach ($data_array as $input => $expected_output) {
        assert_equal($expected_output, $lambda($input));
    }
}
Esempio n. 27
0
 public function test_literal_or_object_returns_wrapping_value()
 {
     $object = phpx\literal_or_object("foreach");
     ensure($object instanceof phpx\Literal);
     assert_equal("foreach", $object->to_php());
 }
Esempio n. 28
0
 function test_array_url_encode_with_omit()
 {
     $expect = 'a[b][c]=d&a[b][e]=f&a[g]=h';
     $expect = str_replace(array('[', ']'), array('%5B', '%5D'), $expect);
     assert_equal($expect, array_url_encode($this->array, 'i'));
 }
Esempio n. 29
0
 function test_should_return_response_body_when_calling_toString()
 {
     ob_start();
     echo $this->response;
     assert_equal($this->response->body, ob_get_clean());
 }
Esempio n. 30
0
function test_output_autorender()
{
    $response = test_request(TESTS_DOC_ROOT . '02-outputs.php/autorender', 'GET');
    assert_equal($response, 'AUTORENDERED OUTPUT for empty_controller');
}