]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; | |
4 | ||
5 | use Monolog\Handler\TestHandler; | |
6 | use Monolog\Logger; | |
7 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | |
8 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; | |
9 | use PHPUnit_Framework_TestCase; | |
10 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; | |
11 | ||
12 | class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |
13 | { | |
14 | /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ | |
15 | protected $builder; | |
16 | ||
17 | public function testBuildConfigExists() | |
18 | { | |
19 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ | |
20 | $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') | |
21 | ->disableOriginalConstructor() | |
22 | ->getMock(); | |
23 | ||
24 | $grabySiteConfig = new GrabySiteConfig(); | |
25 | $grabySiteConfig->requires_login = true; | |
26 | $grabySiteConfig->login_uri = 'http://example.com/login'; | |
27 | $grabySiteConfig->login_username_field = 'login'; | |
28 | $grabySiteConfig->login_password_field = 'password'; | |
29 | $grabySiteConfig->login_extra_fields = ['field=value']; | |
30 | $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; | |
31 | ||
32 | $grabyConfigBuilderMock | |
33 | ->method('buildForHost') | |
34 | ->with('example.com') | |
35 | ->will($this->returnValue($grabySiteConfig)); | |
36 | ||
37 | $logger = new Logger('foo'); | |
38 | $handler = new TestHandler(); | |
39 | $logger->pushHandler($handler); | |
40 | ||
41 | $this->builder = new GrabySiteConfigBuilder( | |
42 | $grabyConfigBuilderMock, | |
43 | ['example.com' => ['username' => 'foo', 'password' => 'bar']], | |
44 | $logger | |
45 | ); | |
46 | ||
47 | $config = $this->builder->buildForHost('example.com'); | |
48 | ||
49 | $this->assertEquals( | |
50 | new SiteConfig([ | |
51 | 'host' => 'example.com', | |
52 | 'requiresLogin' => true, | |
53 | 'loginUri' => 'http://example.com/login', | |
54 | 'usernameField' => 'login', | |
55 | 'passwordField' => 'password', | |
56 | 'extraFields' => ['field' => 'value'], | |
57 | 'notLoggedInXpath' => '//div[@class="need-login"]', | |
58 | 'username' => 'foo', | |
59 | 'password' => 'bar', | |
60 | ]), | |
61 | $config | |
62 | ); | |
63 | ||
64 | $records = $handler->getRecords(); | |
65 | ||
66 | $this->assertCount(1, $records, 'One log was recorded'); | |
67 | } | |
68 | ||
69 | public function testBuildConfigDoesntExist() | |
70 | { | |
71 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ | |
72 | $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') | |
73 | ->disableOriginalConstructor() | |
74 | ->getMock(); | |
75 | ||
76 | $grabyConfigBuilderMock | |
77 | ->method('buildForHost') | |
78 | ->with('unknown.com') | |
79 | ->will($this->returnValue(new GrabySiteConfig())); | |
80 | ||
81 | $logger = new Logger('foo'); | |
82 | $handler = new TestHandler(); | |
83 | $logger->pushHandler($handler); | |
84 | ||
85 | $this->builder = new GrabySiteConfigBuilder( | |
86 | $grabyConfigBuilderMock, | |
87 | [], | |
88 | $logger | |
89 | ); | |
90 | ||
91 | $config = $this->builder->buildForHost('unknown.com'); | |
92 | ||
93 | $this->assertFalse($config); | |
94 | ||
95 | $records = $handler->getRecords(); | |
96 | ||
97 | $this->assertCount(1, $records, 'One log was recorded'); | |
98 | } | |
99 | } |