symfony /tests/Symfony/Tests/Components/DomCrawler/LinkTest.php

Language PHP Lines 77
MD5 Hash a868df1423a2cdc75e8bb9a15a7b598e
Repository https://github.com/come/symfony.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Tests\Components\DomCrawler;

use Symfony\Components\DomCrawler\Link;

class LinkTest extends \PHPUnit_Framework_TestCase
{
    public function testConstructor()
    {
        $dom = new \DOMDocument();
        $dom->loadHTML('<html><div><div></html>');

        $node = $dom->getElementsByTagName('div')->item(0);

        try {
            new Link($node);
            $this->fail('__construct() throws a \LogicException if the node is not an "a" tag');
        } catch (\Exception $e) {
            $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the node is not an "a" tag');
        }
    }

    public function testGetters()
    {
        $dom = new \DOMDocument();
        $dom->loadHTML('<html><a href="/foo">foo</a></html>');

        $node = $dom->getElementsByTagName('a')->item(0);
        $link = new Link($node);

        $this->assertEquals('/foo', $link->getUri(), '->getUri() returns the URI of the link');
        $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
        $this->assertEquals('get', $link->getMethod(), '->getMethod() returns the method of the link');

        $link = new Link($node, 'post');
        $this->assertEquals('post', $link->getMethod(), '->getMethod() returns the method of the link');

        $link = new Link($node, 'get', 'http://localhost', '/bar/');
        $this->assertEquals('http://localhost/foo', $link->getUri(), '->getUri() returns the absolute URI of the link');
        $this->assertEquals('/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');

        $dom = new \DOMDocument();
        $dom->loadHTML('<html><a href="foo">foo</a></html>');
        $node = $dom->getElementsByTagName('a')->item(0);

        $link = new Link($node, 'get', 'http://localhost', '/bar/');
        $this->assertEquals('http://localhost/bar/foo', $link->getUri(), '->getUri() returns the absolute URI of the link for relative hrefs');
        $this->assertEquals('/bar/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');

        $dom = new \DOMDocument();
        $dom->loadHTML('<html><a href="http://login.foo.com/foo">foo</a></html>');
        $node = $dom->getElementsByTagName('a')->item(0);

        $link = new Link($node, 'get', 'http://www.foo.com');
        $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

        $link = new Link($node, 'get');
        $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

        $link = new Link($node, 'get', null, '/bar/');
        $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

        $link = new Link($node, 'get','http://www.foo.com','/bar/');
        $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
    }
}
Back to Top