diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | 119 |
1 files changed, 85 insertions, 34 deletions
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index aee67259..5d6a29fe 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | |||
@@ -2,12 +2,14 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; | 3 | namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; |
4 | 4 | ||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | ||
6 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; | 5 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; |
7 | use PHPUnit_Framework_TestCase; | 6 | use Monolog\Handler\TestHandler; |
7 | use Monolog\Logger; | ||
8 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
9 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
8 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; | 10 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; |
9 | 11 | ||
10 | class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | 12 | class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase |
11 | { | 13 | { |
12 | /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ | 14 | /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ |
13 | protected $builder; | 15 | protected $builder; |
@@ -15,16 +17,16 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
15 | public function testBuildConfigExists() | 17 | public function testBuildConfigExists() |
16 | { | 18 | { |
17 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ | 19 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ |
18 | $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') | 20 | $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') |
19 | ->disableOriginalConstructor() | 21 | ->disableOriginalConstructor() |
20 | ->getMock(); | 22 | ->getMock(); |
21 | 23 | ||
22 | $grabySiteConfig = new GrabySiteConfig(); | 24 | $grabySiteConfig = new GrabySiteConfig(); |
23 | $grabySiteConfig->requires_login = true; | 25 | $grabySiteConfig->requires_login = true; |
24 | $grabySiteConfig->login_uri = 'http://example.com/login'; | 26 | $grabySiteConfig->login_uri = 'http://www.example.com/login'; |
25 | $grabySiteConfig->login_username_field = 'login'; | 27 | $grabySiteConfig->login_username_field = 'login'; |
26 | $grabySiteConfig->login_password_field = 'password'; | 28 | $grabySiteConfig->login_password_field = 'password'; |
27 | $grabySiteConfig->login_extra_fields = ['field' => 'value']; | 29 | $grabySiteConfig->login_extra_fields = ['field=value']; |
28 | $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; | 30 | $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; |
29 | 31 | ||
30 | $grabyConfigBuilderMock | 32 | $grabyConfigBuilderMock |
@@ -32,27 +34,52 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
32 | ->with('example.com') | 34 | ->with('example.com') |
33 | ->will($this->returnValue($grabySiteConfig)); | 35 | ->will($this->returnValue($grabySiteConfig)); |
34 | 36 | ||
37 | $logger = new Logger('foo'); | ||
38 | $handler = new TestHandler(); | ||
39 | $logger->pushHandler($handler); | ||
40 | |||
41 | $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') | ||
42 | ->disableOriginalConstructor() | ||
43 | ->getMock(); | ||
44 | $siteCrentialRepo->expects($this->once()) | ||
45 | ->method('findOneByHostAndUser') | ||
46 | ->with('example.com', 1) | ||
47 | ->willReturn(['username' => 'foo', 'password' => 'bar']); | ||
48 | |||
49 | $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') | ||
50 | ->disableOriginalConstructor() | ||
51 | ->getMock(); | ||
52 | $user->expects($this->once()) | ||
53 | ->method('getId') | ||
54 | ->willReturn(1); | ||
55 | |||
56 | $token = new UsernamePasswordToken($user, 'pass', 'provider'); | ||
57 | |||
58 | $tokenStorage = new TokenStorage(); | ||
59 | $tokenStorage->setToken($token); | ||
60 | |||
35 | $this->builder = new GrabySiteConfigBuilder( | 61 | $this->builder = new GrabySiteConfigBuilder( |
36 | $grabyConfigBuilderMock, | 62 | $grabyConfigBuilderMock, |
37 | ['example.com' => ['username' => 'foo', 'password' => 'bar']] | 63 | $tokenStorage, |
64 | $siteCrentialRepo, | ||
65 | $logger | ||
38 | ); | 66 | ); |
39 | 67 | ||
40 | $config = $this->builder->buildForHost('example.com'); | 68 | $config = $this->builder->buildForHost('www.example.com'); |
41 | 69 | ||
42 | self::assertEquals( | 70 | $this->assertSame('example.com', $config->getHost()); |
43 | new SiteConfig([ | 71 | $this->assertSame(true, $config->requiresLogin()); |
44 | 'host' => 'example.com', | 72 | $this->assertSame('http://www.example.com/login', $config->getLoginUri()); |
45 | 'requiresLogin' => true, | 73 | $this->assertSame('login', $config->getUsernameField()); |
46 | 'loginUri' => 'http://example.com/login', | 74 | $this->assertSame('password', $config->getPasswordField()); |
47 | 'usernameField' => 'login', | 75 | $this->assertSame(['field' => 'value'], $config->getExtraFields()); |
48 | 'passwordField' => 'password', | 76 | $this->assertSame('//div[@class="need-login"]', $config->getNotLoggedInXpath()); |
49 | 'extraFields' => ['field' => 'value'], | 77 | $this->assertSame('foo', $config->getUsername()); |
50 | 'notLoggedInXpath' => '//div[@class="need-login"]', | 78 | $this->assertSame('bar', $config->getPassword()); |
51 | 'username' => 'foo', | 79 | |
52 | 'password' => 'bar', | 80 | $records = $handler->getRecords(); |
53 | ]), | 81 | |
54 | $config | 82 | $this->assertCount(1, $records, 'One log was recorded'); |
55 | ); | ||
56 | } | 83 | } |
57 | 84 | ||
58 | public function testBuildConfigDoesntExist() | 85 | public function testBuildConfigDoesntExist() |
@@ -67,19 +94,43 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
67 | ->with('unknown.com') | 94 | ->with('unknown.com') |
68 | ->will($this->returnValue(new GrabySiteConfig())); | 95 | ->will($this->returnValue(new GrabySiteConfig())); |
69 | 96 | ||
70 | $this->builder = new GrabySiteConfigBuilder($grabyConfigBuilderMock, []); | 97 | $logger = new Logger('foo'); |
98 | $handler = new TestHandler(); | ||
99 | $logger->pushHandler($handler); | ||
71 | 100 | ||
72 | $config = $this->builder->buildForHost('unknown.com'); | 101 | $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') |
102 | ->disableOriginalConstructor() | ||
103 | ->getMock(); | ||
104 | $siteCrentialRepo->expects($this->once()) | ||
105 | ->method('findOneByHostAndUser') | ||
106 | ->with('unknown.com', 1) | ||
107 | ->willReturn(null); | ||
108 | |||
109 | $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') | ||
110 | ->disableOriginalConstructor() | ||
111 | ->getMock(); | ||
112 | $user->expects($this->once()) | ||
113 | ->method('getId') | ||
114 | ->willReturn(1); | ||
115 | |||
116 | $token = new UsernamePasswordToken($user, 'pass', 'provider'); | ||
117 | |||
118 | $tokenStorage = new TokenStorage(); | ||
119 | $tokenStorage->setToken($token); | ||
73 | 120 | ||
74 | self::assertEquals( | 121 | $this->builder = new GrabySiteConfigBuilder( |
75 | new SiteConfig([ | 122 | $grabyConfigBuilderMock, |
76 | 'host' => 'unknown.com', | 123 | $tokenStorage, |
77 | 'requiresLogin' => false, | 124 | $siteCrentialRepo, |
78 | 'username' => null, | 125 | $logger |
79 | 'password' => null, | ||
80 | 'extraFields' => [], | ||
81 | ]), | ||
82 | $config | ||
83 | ); | 126 | ); |
127 | |||
128 | $config = $this->builder->buildForHost('unknown.com'); | ||
129 | |||
130 | $this->assertFalse($config); | ||
131 | |||
132 | $records = $handler->getRecords(); | ||
133 | |||
134 | $this->assertCount(1, $records, 'One log was recorded'); | ||
84 | } | 135 | } |
85 | } | 136 | } |