1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8require_once __DIR__ . '/../../language/CollectFiles.php';
9use org\bovigo\vfs\vfsStream;
10use org\bovigo\vfs\vfsStreamFile;
11use org\bovigo\vfs\vfsStreamDirectory;
12
13class Language_CollectFilesTest extends TikiTestCase
14{
15	protected function setUp()
16	{
17		$this->obj = new Language_CollectFiles;
18
19		// setup a mock filesystem with directories and files
20		$root = vfsStream::setup('root');
21		$dir1 = new vfsStreamDirectory('dir1');
22		$dir2 = new vfsStreamDirectory('dir2');
23		$root->addChild($dir1);
24		$root->addChild($dir2);
25
26		$dir1->addChild(new vfsStreamFile('file1.tpl'));
27		$dir2->addChild(new vfsStreamFile('file2.php'));
28		$dir2->addChild(new vfsStreamFile('file3.php'));
29		$dir2->addChild(new vfsStreamFile('file4.txt'));
30	}
31
32	public function testSetExcludeDirs_shouldRaiseExceptionForInvalidDir()
33	{
34		$dirs = ['invalidDir'];
35		$this->expectException('Language_Exception');
36		$this->obj->setExcludeDirs($dirs);
37	}
38
39	public function testSetExcludeDirsAndGetExcludeDir_shouldSetProperty()
40	{
41		$dirs = ['fixtures'];
42		$cwd = getcwd();
43		chdir(__DIR__);
44		$expectedResult = [getcwd() . '/' . 'fixtures'];
45		$this->obj->setExcludeDirs($dirs);
46		$this->assertEquals($expectedResult, $this->obj->getExcludeDirs());
47		chdir($cwd);
48	}
49
50	public function testIncludeFilesDirs_shouldRaiseExceptionForInvalidFile()
51	{
52		$dirs = ['invalidFile'];
53		$this->expectException('Language_Exception');
54		$this->obj->setIncludeFiles($dirs);
55	}
56
57	public function testIncludeFilesDirsAndGetIncludeFiles_shouldSetProperty()
58	{
59		$dirs = [__DIR__ . '/fixtures'];
60		$this->obj->setIncludeFiles($dirs);
61		$this->assertEquals($dirs, $this->obj->getIncludeFiles());
62	}
63
64	public function testRun_shouldMergeArrays()
65	{
66		$obj = $this->getMockBuilder('Language_CollectFiles')
67					->setMethods(['scanDir', 'getIncludeFiles'])
68					->getMock();
69		$obj->expects($this->once())->method('scanDir')->will($this->returnValue(['lib/test.php', 'tiki-test.php']));
70		$obj->expects($this->once())->method('getIncludeFiles')->will($this->returnValue(['tiki-test.php', 'tiki-index.php']));
71
72		$this->assertEquals(['lib/test.php', 'tiki-test.php', 'tiki-index.php'], $obj->run('.'));
73	}
74
75	public function testScanDir_shouldRaiseExceptionForInvalidDir()
76	{
77		$this->expectException('Language_Exception');
78		$this->obj->scanDir('invalidDir');
79	}
80
81	public function testScanDir_shouldReturnFiles()
82	{
83		$expectedResult = ['vfs://root/dir1/file1.tpl', 'vfs://root/dir2/file2.php', 'vfs://root/dir2/file3.php'];
84		$this->assertEquals($expectedResult, $this->obj->scanDir(vfsStream::url('root')));
85	}
86
87	public function testScanDir_shouldIgnoreExcludedDirs()
88	{
89		$obj = $this->getMockBuilder('Language_CollectFiles')
90					->setMethods(['getExcludeDirs'])
91					->getMock();
92
93		$obj->expects($this->exactly(5))->method('getExcludeDirs')->will($this->returnValue(['vfs://root/dir1']));
94		$expectedResult = ['vfs://root/dir2/file2.php', 'vfs://root/dir2/file3.php'];
95		$this->assertEquals($expectedResult, $obj->scanDir(vfsStream::url('root')));
96	}
97
98	public function testScanDir_shouldAcceptIncludedFiles()
99	{
100		$obj = $this->getMockBuilder('Language_CollectFiles')
101					->setMethods(['getExcludeDirs', 'getIncludeFiles'])
102					->getMock();
103
104		$obj->expects($this->exactly(3))->method('getExcludeDirs')->will($this->returnValue(['vfs://root/dir2']));
105		$obj->expects($this->exactly(2))->method('getIncludeFiles')->will($this->returnValue(['vfs://root/dir2/file3.php']));
106
107		$expectedResult = ['vfs://root/dir1/file1.tpl', 'vfs://root/dir2/file3.php'];
108		$this->assertEquals($expectedResult, $obj->scanDir(vfsStream::url('root')));
109	}
110}
111