Merge "Added clarifying comments to IContextSource"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteListFileCacheTest.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 SiteListFileCache
26 * @group Site
27 *
28 * @licence GNU GPL v2+
29 * @author Katie Filbert < aude.wiki@gmail.com >
30 */
31 class SiteListFileCacheTest extends PHPUnit_Framework_TestCase {
32
33 protected function setUp() {
34 $this->cacheFile = $this->getCacheFile();
35 }
36
37 protected function tearDown() {
38 unlink( $this->cacheFile );
39 }
40
41 public function testGetSites() {
42 $sites = $this->getSites();
43 $cacheBuilder = $this->newSiteListFileCacheBuilder( $sites );
44 $cacheBuilder->build();
45
46 $cache = new SiteListFileCache( $this->cacheFile );
47 $this->assertEquals( $sites, $cache->getSites() );
48 }
49
50 public function testGetSite() {
51 $sites = $this->getSites();
52 $cacheBuilder = $this->newSiteListFileCacheBuilder( $sites );
53 $cacheBuilder->build();
54
55 $cache = new SiteListFileCache( $this->cacheFile );
56
57 $this->assertEquals( $sites->getSite( 'enwiktionary' ), $cache->getSite( 'enwiktionary' ) );
58 }
59
60 private function newSiteListFileCacheBuilder( SiteList $sites ) {
61 return new SiteListFileCacheBuilder(
62 $this->getSiteSQLStore( $sites ),
63 $this->cacheFile
64 );
65 }
66
67 private function getSiteSQLStore( SiteList $sites ) {
68 $siteSQLStore = $this->getMockBuilder( 'SiteSQLStore' )
69 ->disableOriginalConstructor()
70 ->getMock();
71
72 $siteSQLStore->expects( $this->any() )
73 ->method( 'getSites' )
74 ->will( $this->returnValue( $sites ) );
75
76 return $siteSQLStore;
77 }
78
79 private function getSites() {
80 $sites = array();
81
82 $site = new Site();
83 $site->setGlobalId( 'foobar' );
84 $sites[] = $site;
85
86 $site = new MediaWikiSite();
87 $site->setGlobalId( 'enwiktionary' );
88 $site->setGroup( 'wiktionary' );
89 $site->setLanguageCode( 'en' );
90 $site->addNavigationId( 'enwiktionary' );
91 $site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" );
92 $site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" );
93 $sites[] = $site;
94
95 return new SiteList( $sites );
96 }
97
98 private function getCacheFile() {
99 return tempnam( sys_get_temp_dir(), 'mw-test-sitelist' );
100 }
101
102 }