05aa6d393ab4ae8a56b99c587636a22e5225ac72
[lhc/web/wiklou.git] / tests / phpunit / includes / site / FileBasedSiteLookupTest.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.25
21 *
22 * @ingroup Site
23 * @ingroup Test
24 *
25 * @covers FileBasedSiteLookup
26 * @group Site
27 *
28 * @author Katie Filbert < aude.wiki@gmail.com >
29 */
30 class FileBasedSiteLookupTest extends PHPUnit_Framework_TestCase {
31
32 use MediaWikiCoversValidator;
33
34 protected function setUp() {
35 $this->cacheFile = $this->getCacheFile();
36 }
37
38 protected function tearDown() {
39 unlink( $this->cacheFile );
40 }
41
42 public function testGetSites() {
43 $sites = $this->getSites();
44 $cacheBuilder = $this->newSitesCacheFileBuilder( $sites );
45 $cacheBuilder->build();
46
47 $cache = new FileBasedSiteLookup( $this->cacheFile );
48 $this->assertEquals( $sites, $cache->getSites() );
49 }
50
51 public function testGetSite() {
52 $sites = $this->getSites();
53 $cacheBuilder = $this->newSitesCacheFileBuilder( $sites );
54 $cacheBuilder->build();
55
56 $cache = new FileBasedSiteLookup( $this->cacheFile );
57
58 $this->assertEquals( $sites->getSite( 'enwiktionary' ), $cache->getSite( 'enwiktionary' ) );
59 }
60
61 private function newSitesCacheFileBuilder( SiteList $sites ) {
62 return new SitesCacheFileBuilder(
63 $this->getSiteLookup( $sites ),
64 $this->cacheFile
65 );
66 }
67
68 private function getSiteLookup( SiteList $sites ) {
69 $siteLookup = $this->getMockBuilder( SiteLookup::class )
70 ->disableOriginalConstructor()
71 ->getMock();
72
73 $siteLookup->expects( $this->any() )
74 ->method( 'getSites' )
75 ->will( $this->returnValue( $sites ) );
76
77 return $siteLookup;
78 }
79
80 private function getSites() {
81 $sites = [];
82
83 $site = new Site();
84 $site->setGlobalId( 'foobar' );
85 $sites[] = $site;
86
87 $site = new MediaWikiSite();
88 $site->setGlobalId( 'enwiktionary' );
89 $site->setGroup( 'wiktionary' );
90 $site->setLanguageCode( 'en' );
91 $site->addNavigationId( 'enwiktionary' );
92 $site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" );
93 $site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" );
94 $sites[] = $site;
95
96 return new SiteList( $sites );
97 }
98
99 private function getCacheFile() {
100 return tempnam( sys_get_temp_dir(), 'mw-test-sitelist' );
101 }
102
103 }