Open Journal Systems  3.3.0
AbstractConfigLoaderTest.php
1 <?php
2 
4 
8 class AbstractConfigLoaderTest extends \Guzzle\Tests\GuzzleTestCase
9 {
11  protected $loader;
12 
14  protected $cleanup = array();
15 
16  public function setUp()
17  {
18  $this->loader = $this->getMockBuilder('Guzzle\Service\AbstractConfigLoader')
19  ->setMethods(array('build'))
20  ->getMockForAbstractClass();
21  }
22 
23  public function tearDown()
24  {
25  foreach ($this->cleanup as $file) {
26  unlink($file);
27  }
28  }
29 
33  public function testOnlyLoadsSupportedTypes()
34  {
35  $this->loader->load(new \stdClass());
36  }
37 
42  public function testFileMustBeReadable()
43  {
44  $this->loader->load('fooooooo.json');
45  }
46 
51  public function testMustBeSupportedExtension()
52  {
53  $this->loader->load(dirname(__DIR__) . '/TestData/FileBody.txt');
54  }
55 
60  public function testJsonMustBeValue()
61  {
62  $filename = tempnam(sys_get_temp_dir(), 'json') . '.json';
63  file_put_contents($filename, '{/{./{}foo');
64  $this->cleanup[] = $filename;
65  $this->loader->load($filename);
66  }
67 
72  public function testPhpFilesMustReturnAnArray()
73  {
74  $filename = tempnam(sys_get_temp_dir(), 'php') . '.php';
75  file_put_contents($filename, '<?php $fdr = false;');
76  $this->cleanup[] = $filename;
77  $this->loader->load($filename);
78  }
79 
80  public function testLoadsPhpFileIncludes()
81  {
82  $filename = tempnam(sys_get_temp_dir(), 'php') . '.php';
83  file_put_contents($filename, '<?php return array("foo" => "bar");');
84  $this->cleanup[] = $filename;
85  $this->loader->expects($this->exactly(1))->method('build')->will($this->returnArgument(0));
86  $config = $this->loader->load($filename);
87  $this->assertEquals(array('foo' => 'bar'), $config);
88  }
89 
90  public function testCanCreateFromJson()
91  {
92  $file = dirname(__DIR__) . '/TestData/services/json1.json';
93  // The build method will just return the config data
94  $this->loader->expects($this->exactly(1))->method('build')->will($this->returnArgument(0));
95  $data = $this->loader->load($file);
96  // Ensure that the config files were merged using the includes directives
97  $this->assertArrayHasKey('includes', $data);
98  $this->assertArrayHasKey('services', $data);
99  $this->assertInternalType('array', $data['services']['foo']);
100  $this->assertInternalType('array', $data['services']['abstract']);
101  $this->assertInternalType('array', $data['services']['mock']);
102  $this->assertEquals('bar', $data['services']['foo']['params']['baz']);
103  }
104 
105  public function testUsesAliases()
106  {
107  $file = dirname(__DIR__) . '/TestData/services/json1.json';
108  $this->loader->addAlias('foo', $file);
109  // The build method will just return the config data
110  $this->loader->expects($this->exactly(1))->method('build')->will($this->returnArgument(0));
111  $data = $this->loader->load('foo');
112  $this->assertEquals('bar', $data['services']['foo']['params']['baz']);
113  }
114 
119  public function testCanRemoveAliases()
120  {
121  $file = dirname(__DIR__) . '/TestData/services/json1.json';
122  $this->loader->addAlias('foo.json', $file);
123  $this->loader->removeAlias('foo.json');
124  $this->loader->load('foo.json');
125  }
126 
127  public function testCanLoadArraysWithIncludes()
128  {
129  $file = dirname(__DIR__) . '/TestData/services/json1.json';
130  $config = array('includes' => array($file));
131  // The build method will just return the config data
132  $this->loader->expects($this->exactly(1))->method('build')->will($this->returnArgument(0));
133  $data = $this->loader->load($config);
134  $this->assertEquals('bar', $data['services']['foo']['params']['baz']);
135  }
136 
137  public function testDoesNotEnterInfiniteLoop()
138  {
139  $prefix = $file = dirname(__DIR__) . '/TestData/description';
140  $this->loader->load("{$prefix}/baz.json");
141  $this->assertCount(4, $this->readAttribute($this->loader, 'loadedFiles'));
142  // Ensure that the internal list of loaded files is reset
143  $this->loader->load("{$prefix}/../test_service2.json");
144  $this->assertCount(1, $this->readAttribute($this->loader, 'loadedFiles'));
145  // Ensure that previously loaded files will be reloaded when starting fresh
146  $this->loader->load("{$prefix}/baz.json");
147  $this->assertCount(4, $this->readAttribute($this->loader, 'loadedFiles'));
148  }
149 }
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Service
Definition: AbstractConfigLoaderTest.php:3