ResourceLoaderImageModule: Add basic tests
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 27 Mar 2015 17:08:11 +0000 (18:08 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Sun, 29 Mar 2015 16:29:51 +0000 (18:29 +0200)
Bug: T86334
Change-Id: Ie98bf5dec2d4d4c309ed5075b3191da3017f5ad4

tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php
new file mode 100644 (file)
index 0000000..f98f5fc
--- /dev/null
@@ -0,0 +1,160 @@
+<?php
+
+/**
+ * @group ResourceLoader
+ */
+class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase {
+
+       public static function providerGetModules() {
+               $commonVariants = array(
+                       'invert' => array(
+                               'color' => '#FFFFFF',
+                               'global' => true,
+                       ),
+                       'primary' => array(
+                               'color' => '#598AD1',
+                       ),
+                       'constructive' => array(
+                               'color' => '#00C697',
+                       ),
+                       'destructive' => array(
+                               'color' => '#E81915',
+                       ),
+               );
+
+               $commonImageData = array(
+                       'advanced' => 'advanced.svg',
+                       'remove' => array(
+                               'image' => 'remove.svg',
+                               'variants' => array( 'destructive' ),
+                       ),
+                       'next' => array(
+                               'image' => array(
+                                       'ltr' => 'next.svg',
+                                       'rtl' => 'prev.svg'
+                               ),
+                       ),
+                       'help' => array(
+                               'image' => array(
+                                       'ltr' => 'help-ltr.svg',
+                                       'rtl' => 'help-rtl.svg',
+                                       'lang' => array(
+                                               'he' => 'help-ltr.svg',
+                                       )
+                               ),
+                       ),
+                       'bold' => array(
+                               'image' => array(
+                                       'default' => 'bold-a.svg',
+                                       'lang' => array(
+                                               'en' => 'bold-b.svg',
+                                               'de' => 'bold-f.svg',
+                                       )
+                               ),
+                       )
+               );
+
+               return array(
+                       array(
+                               array(
+                                       'class' => 'ResourceLoaderImageModule',
+                                       'prefix' => 'oo-ui',
+                                       'variants' => array(
+                                               'icon' => $commonVariants,
+                                       ),
+                                       'images' => array(
+                                               'icon' => $commonImageData,
+                                       ),
+                               ),
+                               '.oo-ui-icon-advanced {
+       ...
+}
+.oo-ui-icon-advanced-invert {
+       ...
+}
+.oo-ui-icon-remove {
+       ...
+}
+.oo-ui-icon-remove-invert {
+       ...
+}
+.oo-ui-icon-remove-destructive {
+       ...
+}
+.oo-ui-icon-next {
+       ...
+}
+.oo-ui-icon-next-invert {
+       ...
+}
+.oo-ui-icon-help {
+       ...
+}
+.oo-ui-icon-help-invert {
+       ...
+}
+.oo-ui-icon-bold {
+       ...
+}
+.oo-ui-icon-bold-invert {
+       ...
+}',
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider providerGetModules
+        * @covers ResourceLoaderImageModule::getStyles
+        */
+       public function testGetStyles( $module, $expected ) {
+               $module = new ResourceLoaderImageModuleTestable( $module );
+               $styles = $module->getStyles( $this->getResourceLoaderContext() );
+               $this->assertEquals( $expected, $styles['all'] );
+       }
+}
+
+class ResourceLoaderImageModuleTestable extends ResourceLoaderImageModule {
+       /**
+        * Replace with a stub to make test cases easier to write.
+        */
+       protected function getCssDeclarations( $primary, $fallback ) {
+               return array( '...' );
+       }
+
+       /**
+        * Return mock ResourceLoaderImages that don't call file_get_contents and such.
+        */
+       public function getImages() {
+               $images = parent::getImages();
+               foreach ( $images as $name => &$image ) {
+                       $image = new ResourceLoaderImageWrapper( $image );
+               }
+               return $images;
+       }
+}
+
+/**
+ * Wraps a ResourceLoaderImage not to call file_get_contents and such.
+ */
+class ResourceLoaderImageWrapper extends ResourceLoaderImage {
+       public function __construct( ResourceLoaderImage $image ) {
+               $this->image = $image;
+       }
+
+       public function getUrl( ResourceLoaderContext $context, $script, $variant, $format ) {
+               return null;
+       }
+
+       public function getDataUri( ResourceLoaderContext $context, $variant, $format ) {
+               return null;
+       }
+
+       public function __call( $method, $arguments ) {
+               return call_user_func_array( array( $this->image, $method ), $arguments );
+       }
+
+       public function __get( $name ) {
+               return $this->image->$name;
+       }
+}